Take the 2-minute tour ×
Ask Ubuntu is a question and answer site for Ubuntu users and developers. It's 100% free, no registration required.

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?

share|improve this question

2 Answers 2

up vote 1 down vote accepted

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.

share|improve this answer

The command can be something like:

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

Result:

libreoffice-impress.desktop
share|improve this answer
    
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 5 '12 at 0: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 5 '12 at 0:49

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.