How to associate all file types within Wine with its corresponding native application?

This is easily done for a single file type, as answered in How to associate a file type within Wine with a native application?, by creating a .reg for the desired filetype. But this is for AVI only. I use some wine apps (uTorrent, Soulseek, Eudora, to name a few) that can launch a wide range of files. Email attachments, for example, can be JPG, DOC, PDF, PPS... its impossible (and not desirable) to track down all possible file types that one may receive in an email or download in a torrent.

So I neeed a solution to be more generic and broad. I need the file association to honor whatever native app is currently configured. And I want this to be done for all file types configured in my system.

I've already figured out how to make the solution generic. Simply replacing the launched app in .reg for winebrowser, like this:

[HKEY_CLASSES_ROOT\.pdf]
@="PDFfile"
"Content Type"="application/pdf"
[HKEY_CLASSES_ROOT\PDFfile\Shell\Open\command]
@="C:\\windows\\system32\\winebrowser.exe \"%1\""

Ive tested this and it works correctly. Since winebrowser uses xdg-open as a backend, and converts my windows path to a Unix one, the correct (Linux) app is launched.

So I need a "batch" updater to wine's registry, sort of a wine-update-associations script that I can run whenever a new app is installed. Maybe a tool that can:

  • List all Mime Types types in my system that have a default, installed app associated
  • Extract all the needed info (glob, mime type, etc)
  • Generate the .REG file in the above format

The tricky part is: i've searched a LOT to find info about how association is done in Ubuntu 10.10 onwards, and documentation is scarce and confusing, to say the least. Freedesktop.org has no complete spec, and even Gnome docs are obsolete. So far I've gathered 4 files that contain association info, but im clueless on which (or why) to use, or how to use them to generate the .reg file:

~/.local/share/applications/mimeapps.list
~/.local/share/applications/miminfo.cache
/usr/share/applications/miminfo.cache
/etc/gnome/defaults.list

Any help, script or explanation would be greatly appreciated!

Thanks!


Years later, I've made a small utility that scans MIME database (both system and user) and register all known native mime-types in Windows registry.

It uses xdg-open to open a file if there is a default (native) application for that mime type, otherwise uses packagekit to search for a package that can handle that file (just like what Nautilus does). So my initial requirement of registering only extensions that have an installed, native application was not needed anymore. However, an early version of the script did filter only such types. The snippet that made it possible was:

perl -e '
    use strict; use warnings;
    use File::MimeInfo::Magic; use File::MimeInfo::Applications;
    while (my $line = <STDIN>) {
      chomp($line);
      my ($ext, $mime) = (split/\t/, $line);
      my ($def, @apps) = mime_applications_all($mime);
      print "$line\n" if ($def || @apps)
    }'

By default my script only registers native types that have no handler in windows registry, but it can also override such associations (so, for example, jpeg files are opened in native viewer instead of the default Gecko wine browser). It can also ignore some extensions even if they have no handler in windows.

It tries its best to be winemenubuilder-friendly, meaning all associations it creates is not published as native associations (or as x-wine-extension mimetypes) by winemenubuilder, which would be ugly and potentially cause loops. This is very tricky and not yet perfect, specially with mixed-case extensions (.C and .c for example)

That said, I hope this script is helful for everyone:

https://github.com/MestreLion/wine-tools/blob/master/wine-import-extensions

Improvements welcome!


EDIT:

There is a wine bug about this - which is more of an improvement than a bug. The point is to have ShellExecute call xdg-open, and if not found, look for gnome and kde defaults. You should be able to apply the patch and finally have the magic :-). This solution is cleaner as it does not need messing with the registry.

To be more complete here is how to patch and compile wine from source.

END EDIT

I update the wine registry with the script below to add a list of common file types.
You can extend the list to add more types.
It makes use of /usr/bin/gnome-open in the gstart.exe file so it won't work for non-gnome desktops as is.

Put this in conf_wine.sh:

#!/bin/bash

SRC=~
WINE=~/.wine
REG=$WINE/system.reg
GSTART=gstart.exe
GSTART_TARGET=$WINE/drive_c
EXE_TARGET=$WINE/drive_c/windows
FNKEY=/tmp/"key"$(date +%F_%H-%M-%S)".reg"

[ -e $FNKEY ] && { echo "temporary key file exists..try again"; exit 1; }

echo "copying gstart.exe"
cp $SRC/$GSTART $GSTART_TARGET
chmod +x $GSTART_TARGET

echo "backing up the registry"
cp $REG $REG.$(date +%F_%H-%M-%S).old

echo "setting new wine registry keys"
for i in http doc docx ppt pptx xls xlsx odt ods xml txt pdf odt svg zip ; do {
    echo "setting $i"
key='[HKEY_CLASSES_ROOT\.'$i']
@="'$i'file"
"Content Type"="application/'$i'"
[HKEY_CLASSES_ROOT\'$i'file\Shell\Open\command]
@="C:\\gstart.exe \"%1\""'
    echo "$key" > $FNKEY
    regedit $FNKEY
}
done

echo "done"

The gstart.exe is a bash script..and is the bridge to both worlds:

#!/bin/bash

OPEN_HANDLER=/usr/bin/gnome-open
# logging, optional
LOG=$HOME/.wine/gstart.exe-log.$(id -u -n)
echo "[ $(date) ] $# argument(s) received: '$@'" > $LOG

# convert the path
RESULT=$(winepath "$@" 2> /dev/null)
echo "$OPEN_HANDLER $RESULT" >> $LOG
TMP=$TMPDIR
TEMP=$TMPDIR

# finally open the file
$OPEN_HANDLER "$RESULT"

Notes:

  1. copy gstart.exe in your current working directory before running conf_wine.sh as it will copy it to the .wine folder.
  2. the folder locations can be changed, e.g. gstart.exe doesn't have to sit in c:\.
  3. does no magic: new types have to be added manually. You could improve it to read the Linux files (mimeapps.list, ..) and update the wine registry if then needed.
  4. tested to work at least in wine1.4.

Wine FAQ: How do I associate a native program with a file type in Wine?