My primary OS is OS X, however I do have a Linux Mint system I use and run into similar situations in which I want to do the same thing on both systems, and when it's not directly supported I have to find another way. In a case such as this, if there wasn't a Browser Extension for .desktop
files, like in your other question about .url
files, here is what I'd do.
I'd convert the .desktop
files to .webloc
files and or .url
files and use the Default Apps Preference Pane, RCDefaultApp by Rubicode, to change which application opens any given file extension.
Here's a bash script, that I modified from a similar script I wrote years ago, which will create .webloc
files and or .url
files from .desktop
files that have URLs in them.
- In a Terminal
cd
to the directory containing the .desktop
files.
Issues the following commands:
touch d2wu
open d2wu
Copy and paste the code below into the opened d2wu
file.
#!/bin/bash
clear
echo
echo " Create .webloc/.url files from .desktop files."
echo " ----------------------------------------------"
echo " Choose File Type(s):"
echo
echo " [1] Create .webloc & .url files."
echo " [2] Create only .webloc files."
echo " [3] Create only .url files."
echo " [Q] Quit."
echo
echo " Press 1 to 3 or Q then press Enter..."
Create_WEBLOC () {
for f in *.desktop; do
if [[ ! -f ${f%.*}.webloc ]]; then
url="$(awk -F '=' '/^URL=/{print $2}' "$f")"
if [[ ! -z $url ]]; then
cat <<EOF >"${f%.*}.webloc"
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
<key>URL</key>
<string>$url</string>
</dict>
</plist>
EOF
else
echo " No URL found in $f file."
fi
fi
done
}
Create_URL () {
for f in *.desktop; do
if [[ ! -f ${f%.*}.url ]]; then
url="$(grep -e '^URL=' "$f")"
if [[ ! -z $url ]]; then
printf "[InternetShortcut]\n${url}\n" > "${f%.*}.url"
else
echo " No URL found in $f file."
fi
fi
done
}
read keypress
echo
case "$keypress" in
"1")
echo " Creating .webloc & .url files."
Create_WEBLOC
Create_URL
;;
"2")
echo " Creating .webloc files."
Create_WEBLOC
;;
"3")
echo " Creating .url files."
Create_URL
;;
"q" | "Q")
echo " No files were created."
;;
*)
echo " Pressing \"$keypress\" is not a valid choice... Try again!"
echo
read -p " Press Enter to continue..."
"$0" # Run the Script again.
;;
esac
echo
Save and close the d2wu
file.
Make d2wu
executable:
chmod u+x d2wu
Execute d2wu
:
./d2wu
Example script output:
Create .webloc/.url files from .desktop files.
----------------------------------------------
Choose File Type(s):
[1] Create .webloc & .url files.
[2] Create only .webloc files.
[3] Create only .url files.
[Q] Quit.
Press 1 to 3 or Q then press Enter...
[]
Note: You can use the Default Apps Preference Pane, RCDefaultApp by Rubicode, to change which application opens a given file extension.
Also, if you need modify other default methods of file types, Apps, URLs, UTIs, etc. the the Default Apps Preference Pane is a great utility for it.
Note: As coded it will not overwrite existing .webloc
files and or .url
files even thought when selecting 1, 2 or 3, it reports "Creating ... files.". This was done to both not step on existing files and minimize output to the screen while only outputting which .desktop
files do not contain a URL=...
line, as can be the case depending on what type of .desktop
file it is.