Wine – Associate All File Types with Native Applications in Wine

file associationmime-typescriptswine

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!

Best Answer

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!