How to click on a file in Linux and have a Bottles Windows program open and load that file.
aka “How to do file associations in Linux for Windows programs running in a Bottle”
aka “How to run a Windows program from a file association in Linux and have the program open the file automatically.”
The instructions below are based on my experience with MS Word 2007 running in a Bottle. I posted a detailed description of the problem a few days back, here.
These instructions will allow you to double-click on a data file - in a Linux directory - associated with a Windows program (e.g. a docx file, or an xlsx file, etc.) to open that program through Bottles, passing the data file’s name as an argument to the progam (which normally causes the program to open the file).
Please note that this solution will NOT work if the file’s name or path contains SPACES.
I believe this solution can be adapted to resolve that issue, however, and if anyone does so, please let me know.
-
Create your Bottle as usual and make sure you can start the Windows program from the Bottle.
-
In Bottles, click on the 3-dot menu of the program and select “Add Desktop entry”.
-
Find the new app in your Linux application launcher. In Kubuntu, it gets created in the “Lost & Found” folder (you can move it elsewhere).
-
Now you need to edit the command that launches the app. Right click on the app and choose the option that allows you to edit it. In Kubuntu, it’s called “Edit Application”.
-
Find the command that starts the app. It looks like this:
flatpak run --command=bottles-cli com.usebottles.bottles run -p WINWORD -b 'Office2007'
You will need to change the -p argument to -e. The -e argument takes the fully-qualified file name of the Windows program in the Linux directory structure.
My new command looks like this:
flatpak run --command=bottles-cli com.usebottles.bottles run -b 'Office2007' -e '/home/eduardo/.var/app/com.usebottles.bottles/data/bottles/bottles/Office2007/drive_c/Program Files/Microsoft Office/Office12/WINWORD.EXE'
- Here’s where it gets tricky. Bottles has a -a argument that you can add to the end of the command to pass arguments to the Windows program. If you simply add -a “test” to the end of the command, your Windows program will be started and given an argument “test”. It doesn’t work for file paths, though, because it doesn’t replace or escape the slashes. For the whole thing to work, we need to pre-process the slahes on the final argument. But we can’t do that using bash string replace, because these commands are run under a dash shell. So I decided to use sed. My final command looks like this:
flatpak run --command=bottles-cli com.usebottles.bottles run -b 'Office2007' -e '/home/eduardo/.var/app/com.usebottles.bottles/data/bottles/bottles/Office2007/drive_c/Program Files/Microsoft Office/Office12/WINWORD.EXE' $(printf '%s' "%F" | sed 's/\//-a z:\\\\/' | sed 's/\//\\\\/g')
… the first sed substitutes the first / in the path with -a z:\\
while the second sed replaces every other instance of /
with \\
… also notice that if %F is empty (as when we click on the app itself and not on an associated file), the whole printf results in an empty string.
-
Now all that’s left is to associate the program with the MIME file extension. In Kubuntu, I simply right-click on a Word document (.docx), choose “Open with” then “Other application”, find the application I just created, and mark the checkbox “Remeber application association for all files of type”.
-
Done. From now on, whenever you double-click on a docx file anywhere in Linux, Word will open and load that file.
Let me know if you use this; I welcome any additional insights.
Cheers.