Magnet handler script for Firefox on OSX

One thing I was missing when downloading torrents with ‘magnet:’ links was an easy way to transfer this link to my bittorent client (which is running on a different server). After copy-pasting many magnet links I finally decided to do something about this and write a small helper application that Firefox can call when it encounters a magnet link.

This example script will save the URL to a file in your home directory called torrents.txt but it should serve as an example to invoke other commands using the shell.

Here we go!

Step 1 - Script

Open the ‘Script Editor’ application and choose ‘Create new document’. Paste this script:

on open location this_URL
   #In case you want to display the URL that is being passed, uncomment the following line
   #display dialog "I'm doing something with this URL: " & return & this_URL

   tell application "Terminal"
      # Create a shell command to append the URL to ~/torrents.txt and exit
      set run_cmd to "echo \"" & this_URL & "\" >> ~/torrents.txt && exit"
      # Execute shell command
      do script run_cmd
   end tell

   # These three lines switch you back to Firefox, might want to change to your preferred browser
   tell application "Firefox"
   end tell
end open location

Now save the file, for example on your Desktop and with an example name of “My magnet handler”. Be sure to choose ‘File format: Application’ in the dropdown.

Step 2 - Hack the app file so it registers as a protocol handler

OSX doesn’t know yet that this new app can handler the ‘magnet:’ links so we have to hack the Info.plist that is inside the app.

  1. Go to your Desktop
  2. Right click on ‘My magnet’ and choose ‘Show Package Contents’
  3. Navigate to the ‘Contents’ folder
  4. Right click the ‘Info.plist’ file and open it with ‘Other’ –> ‘’
  5. At the bottom of the file (but before the final ‘</dict>’ and ‘</plist>’ tags) add another key/array pair by pasting this block:
      <string>My magnet handler</string>

This tells Finder that our app can handle URL’s starting with ‘magnet:’. Save the file and exit TextEdit.

Step 3 – Make finder aware of our app

This step is very counter-intuitive but locate your ‘My magnet’ file on your Desktop and move it to another folder, perhaps your home folder. Moving the file will let Finder re-read the Info.plist file and register it as a protocol handler.

Step 4 - Try it in your browser

Open your favorite torrent site and locate a magnet link. Click on it and if all went well you should be greeted with the ‘Launch Application’ dialog that already lists your application in the ‘Send to’ list. Select it, and press OK.

Your torrent URL should now be listed in a file called ‘torrents.txt’ located in your home directory!

Further expansion

Instead of echo’ing to a file, you can also run any other command you like. In my case, it’s logging (using SSH keys to prevent a password prompt) into my server and calling ‘deluge-console add’ to queue the torrent. In case you’re wondering, it looks a bit like this:

set run_ssh to "ssh \"deluge-console add " & this_URL & "\" && exit"
do script run_ssh

Happy downloading!