Opening Apple webloc files



As you can imagine, transitioning 23 years of user data from Mac to Linux, has a few issues to overcome.

One minor frustration was not being able to open .webloc files in Ubuntu. Webloc is very small XML file that is basically a URL. On Mac OS it's quite convenient to just drag your browser URL to the desktop or a folder to save the location. My old data is littered with these, so just being able to click them & open them as intended is helpful.

To open these files in Linux I created a quick-fix shell script to extract the URL & pass it to google-chrome. So you can double-click .webloc files & they just open in Chrome - nice!

This is my first attempt at doing anything like this, so it was a nice easy place to start to understand creating a script, running it, passing in a file, etc

1) Create a shell script to process the xml. I chose to do this with text parsing tools included by default & not install something like xmllint because the scope of this exercise is so minor.

I am calling my script 'webloc' & keeping it in /usr/local/bin, which is recommended for user created apps.

cd /usr/local/bin
sudo nano webloc
The script

#! /bin/sh
url=$(sed -n 's:.*<string>\(.*\)</string>.*:\1:p' "$1")
google-chrome $url

The text manipulation tool sed accepts the file input "$1", pulls out the URL from between the XML <string> tags & stores it as variable $url. Then chrome opens the url. Super simple other than the regex, which, of course, I stole from a discussion on a similar topic.

Save your file & update execute permissions. I went for rwx on everything because I wasnt sure what exactly would be needed.

sudo chmod 777 webloc

2) Next you need a Desktop Entry file to register your webloc 'app'. Here is the source article I used & also this one explains it well too.

cd /usr/share/applications
sudo nano webloc.desktop

The contents of the .desktop file:

[Desktop Entry]
Version=1.0
Type=Application
Name=Webloc
Comment=Open Apple webloc file in chrome
Exec=webloc %u
Icon=edit-copy
Path=
Terminal=false
StartupNotify=false
Actions=
Categories=Utility

3) Now you will be able to "Open with" on an Apple Webloc file & select your app 'webloc' as the program to open it. webloc will open Chrome seamlessly.







Notes: Apple Webloc files (some_website.webloc) on Linux have the mime-type "Apple System Profiler (x-apple-systemprofiler+xml)"
I am not sure if any other files from Mac OS will have the same mime-type & thus not work as expected.
I've tested this with .webloc files copied onto Linux filesystem & also .webloc files on Mac OS file system over Samba file sharing & both work.
However .webloc files hosted on Google Drive have a different mime-type (xml/txt) & wont work. Be aware in this case you should not have Linux open all (xml/txt) with your Webloc app as it wont be appropriate in most cases for this mime-type.




Comments