2

this may not be strictly about programming, but if I find no ready-made solution it may become a programming task: On UNIX, what is a command-line method for determining the user-preferred application for a given filetype?

My ideal solution here would be a command that stopped me having to do the following:

okular foo.pdf

And allowed me to do something like this, working with my set preferred applications:

launch foo.pdf

I found no answer by searching, and a DIY approach wouldn't work as, while I've been using Linux for a while, I have no clue of the internals that manage my preferred applications.

dmckee --- ex-moderator kitten
  • 98,632
  • 24
  • 142
  • 234
jameshfisher
  • 34,029
  • 31
  • 121
  • 167

3 Answers3

5

On unix per se that would be the one the user used to open it, because there is no OS level notion of a preferred application.

However the major X desktop environment all define such a notion, and then you have to use their facilities:

  • gnome-open in GNOME (duh)
  • exo-open in XFCE [see the comments in the gnome link]
  • xdg-open may work in many environments (reputedly works in KDE) [see the comments in the gnome link]
  • just plain kfmclient exec (or kfmclient4 exec) in KDE (I haven't been able to find a reference to kde-open as Rob H suggests, and don't have a KDE system at hand to try it)

Now Mac OS X provides the open command which works like clicking the file in the finder (which is to say, it asks the OS...)


Several corrections thanks to ephemient in the comments. I won't discuss mailcap, because I never understood it and had forgotten it existed...

Community
  • 1
  • 1
dmckee --- ex-moderator kitten
  • 98,632
  • 24
  • 142
  • 234
  • `xdg-open` isn't from xdm, it's from http://portland.freedesktop.org/ and designed to be desktop-agonistic; the KDE example is inaccurate, it's `kfmclient exec` (or `kfmclient4 exec` if you distribution renames binaries); you left out mention of the traditional `mailcap` mechanism. – ephemient Dec 23 '09 at 01:56
  • xdg-open seems to be exactly what I'm looking for here. Lovely. – jameshfisher Dec 23 '09 at 09:51
1

The answer differs depending on the desktop environment your using. Since you mentioned Okular, I'm going to assume you're using KDE. So try:

kde-open <file>

For GNOME, there is the equivalent:

gnome-open <file>
Rob H
  • 14,502
  • 8
  • 42
  • 45
0

To answer this myself, I've defined a simple (bash) function that works in the way I expect:

function show { 
    xdg-open $1 &> NUL
    }

xdg-open was almost exactly what I wanted, but it lets ugly program warnings slip through into the shell, which the above seems to fix.

Thanks all.

jameshfisher
  • 34,029
  • 31
  • 121
  • 167