How to pass file as argument in .desktop file for wine application

Solution 1:

This is best I can so far (Notepad example)

[Desktop Entry]
Name=Notepad
Exec=sh -c "echo %f | sed 's/^\//z:\//' | xargs wine /home/zetah/.wine/drive_c/windows/notepad.exe"
Type=Application
Terminal=false
Categories=Wine
MimeType=text/plain

I expect better answers considering I'm beginner

Solution 2:

For the first question, you can take a look at the Desktop Entry Specification - The Exec key

Solution 3:

An improvement on zetah's answer. This works for passing multiple files to the program. The %f is changed to %F (upper case) to pass all the files dropped, replacing the ^ (for the beginning of the line) with enough of the path name to make it more likely unique, the 'g' at the end of the sed command to cause the replacement to repeat for all files passed and finally the $ instead of the / so that the \ escape characters are not necessary.

Exec=sh -c "echo %F | sed 's$/home$z:/home$g' | xargs wine /home/owner/.wine/dosdevices/c:/Progs/Boxer/b.exe"

I also moved the program to be executed to a path where the pathname has no spaces, so I moved my boxer editor from:

/home/owner/.wine/dosdevices/c:/Program Files/Boxer Text Editor/

to:

/home/owner/.wine/dosdevices/c:/Progs/Boxer/

Solution 4:

There were lots of hackish solutions posted in this thread. Here's a much better solution that deals with multiple files and with spaces in filenames.

Create the .desktop file as you usually do, and simply use this Exec line:

Exec=/usr/bin/env bash -c "printf 'z:%%q\\n' %F | WINEPREFIX='/home/<your username>/.wine' xargs wine 'C:\\Path\\to the executable\\app.exe'"

Edit: If your application can handle multiple files in a single instance (for example a multi-tabbed PDF viewer) and you want the current instance to be auto-focused whenever you launch a new file then you need to do the following:

  • Add this line to the .desktop file: StartupWMClass=app.exe

  • Use this Exec command:

    Exec=/usr/bin/env bash -c "(printf 'z:%%q\\n' %F | WINEPREFIX='/home/<your username>/.wine' xargs wine 'C:\\Path\\to the executable\\app.exe' &); xdotool windowactivate \$(xdotool search --sync --onlyvisible --class \"app.exe\")"
    

Solution 5:

An Exec line with a pipe of winepath to xargs, used with proper quoting and their -0 options, works flawlessly in Ubuntu 14.04 for opening single and multiple files.

Exec=/usr/bin/env sh -c "winepath -w -0 %F | WINEPREFIX='/home/your_user_name/.wine' xargs -0 wine 'C:\\\\Program Files\\\\Some Vendor\\\\SomeProgram.exe'"