Wednesday, May 8, 2024
 Popular · Latest · Hot · Upcoming
1
rated 0 times [  1] [ 0]  / answers: 1 / hits: 760  / 1 Year ago, thu, november 24, 2022, 1:21:30

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?


More From » bash

 Answers
6

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.


[#37103] Friday, November 25, 2022, 1 Year  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
hirtieve

Total Points: 207
Total Questions: 104
Total Answers: 114

Location: Cook Islands
Member since Thu, May 21, 2020
4 Years ago
hirtieve questions
;