tags:

views:

416

answers:

2

Hi,

the background is a shell script to open the .m3u file of a web radio station. Therefore I want to know inside the script, what's the user's program to open such files. At the moment, he has to set the environment variable $PLAYER, but obviously that is not a good way to go.

Alternative: Is there a command that takes a filename and searches itself for an appropriate program to handle that file? Like file, e.g.,

open-file my_playlist.m3u

The script should be portable, it will run at least on Ubuntu, Debian and Windows/Cygwin machines.

Cheers,

+4  A: 

This will have to be done differently on each platform. On Mac OS X the "open" command will do what you want.

In Linux it gets murky, since the desktop environment (GNOME or KDE) keeps its own list of applications to run for each file type.

There are two files you can look for in Ubuntu / GNOME that hold this info: ~/.local/share/applications/defaults.list and ~/.local/share/applications/mimeinfo.cache

Someone else hopefully knows how to do this in Windows and can chime in.

Edit: Stealing from the other answers:

Linux:

xdg-open [filename]

Cygwin:

cygstart [filename]

And for completeness, here's a link to a previous question about how to detect which operating system you are running on: Detect OS from bash Script

alxp
Thanks for the fast answer, but the Gnome files seem not to contain what I searched. On Ubuntu, Totem opens M3Us as default, but `grep audio mimeinfo.cache` brings me `kde-kaboodle.desktop`, which isn't even yet installed (kaboodle, I mean).I quickly scanned ~/.gconf, but this doesn't seem to contain the searched infos either.
Boldewyn
yup, that did it.
Boldewyn
If your distribution is so old that the xdg utils don't yet exist, try gnome-open (Gnome), kfmclient exec (KDE), exo-open (Xfce), open (GNUstep), etc...
ephemient
A: 

I'd like if there were a different answer to this but I think you'll have to check the file association configs for every desktop environment and file manager out there (so, nautilus, konqueror, thunar, mc... all in different places and in different formats AFAIK), as well as ascertaining which one of these the user is actually using...

If someone has a different idea I'm keen to hear it.

John Barrett
I see xdg-open mentioned but this does not exist on any of the machines I just checked - Debian, Slack and Zenwalk. cygstart is all good :)
John Barrett
As I just commented on the other answer: If your distribution is so old that the xdg utils don't yet exist, try gnome-open (Gnome), kfmclient exec (KDE), exo-open (Xfce), open (GNUstep), etc... Traditionally this file association information is stored in mailcap, but modern desktops don't seem to understand that anymore :(
ephemient
They're not particularly old versions... You can't bank on everyone running the latest version of their distro - I never install .0 versions :)Still, useful info, thanks
John Barrett