Starting Magnet Links Remotely

Updated 2013-03-01: I realized that if Transmission wasn’t already open, the GUI scripting would get passed over, so I added a bit to see if Transmission is the frontmost app.


I’ve been using Keyboard Maestro for months now to take a text file with a “magnet:” link inside, read the text out of it, and then simulate the keystrokes necessary to open the link in Transmission. I saw something similar done with just AppleScript and System Events’ GUI scripting capabilities so I got motivated to do my own system purely in AppleScript.

Here’s the AppleScript:

open for access theFile with write permission
set fileContents to read theFile using delimiter {linefeed}
set theString to fileContents
set the clipboard to theString as text
close access theFile

tell application "Transmission"
    repeat until frontmost is true
        activate
    end repeat
end tell

tell application "System Events"
    tell process "Transmission"
        keystroke "u" using {command down}
        keystroke "v" using {command down}
        delay 1
        keystroke return
    end tell

end tell

tell application "Finder"
    delete theFile
end tell

I have Hazel watch my notes directory in Dropbox for a file named “torrent.md” and when it sees that file, it reads the contents, sets the clipboard to those contents and then opens an “Open link…” menu item, pastes the text and hits “enter”. This fires the torrent up in Transmission and then deletes the original file. I recommend deleting the text file each time instead of setting the eof to 0 because I feel like Hazel more reliably catches the totally new file better than just a changed file.

I have another Keyboard Maestro macro on my remote Macs that create the text file from the copied link and also on my iPhone, I have Drafts Dropbox actions tied to Launch Center Pro actions that can create the file from the clipboard on the iPhone as well. The trouble I go to just to be able to get the latest Linux distros!