4

I need to create a script that returns the default app to be used for a given file extension (including the path). I had a look at the file command which can return the mime type and xdg-open which would open a file. But what I'd like to get is

myscript doc

/usr/bin/libreoffice

.or.

myscript fun

/opt/acme/roadrunner/meepmeep

How do I do that?

stwissel
  • 6,211
  • 5
  • 23
  • 24

2 Answers2

1

I grabbed a Linux expert in the office and we found a reasonable solution:

#!/bin/bash
EXTENSION=$1
SAMPLENAME=$HOME/~webdavhelpersample.$EXTENSION
touch $SAMPLENAME
CURMINE=$(xdg-mime query filetype $SAMPLENAME)
rm $SAMPLENAME
CURDSK=$(xdg-mime query default $CURMINE)

if [ -f /.local/share/applications/$CURDSK ]; then
    TRUEDSK=/.local/share/applications/$CURDSK
elif [ -f /usr/local/share/applications/$CURDSK ]; then
    TRUEDSK=/usr/local/share/applications/$CURDSK
elif [ -f  /usr/share/applications/$CURDSK ]; then
    TRUEDSK=/usr/share/applications/$CURDSK
else 
    echo "Sorry no executable found for $1"
    exit 1
fi

WHATTODO=$(grep "^Exec" $TRUEDSK | head -1)
echo $WHATTODO

Once we figured that there are only 3 locations for the desktop files it was not hard anymore.

stwissel
  • 6,211
  • 5
  • 23
  • 24
0

The command can be something like:

xdg-mime query default `xdg-mime query filetype example.odp` 

Result:

libreoffice-impress.desktop
Samik
  • 2,660
  • Hi Samik, thx for the reply. I tried the command on Ubuntu 12.04 and ran into 2 problems: a) if example.odp doesn't exist it just throws an error and secondly (the bigger issue) I just got application/vnd.oasis.opendocument.presentation in return. Also the desktop file doesn't execute, I need to have something I can call with a set of parameters thereafter – stwissel Jul 05 '12 at 00:44
  • Quick update: I can get to the desktop file using xdg-mime query filetype example.odp || xdg-mime query default but then still the challenge is how to get to the executable including the path – stwissel Jul 05 '12 at 00:49