Page 1 of 1

AppleScript Remote Torrent Launcher

Posted: Mon Jun 01, 2009 7:34 pm
by aaron_stasis
I've spent the last few hours making my first applescript torrent launcher. I use it with Firefox so that when i select a torrent, it uploads it to my transmission-daemon, and starts the torrent. Note, it actually uploads the torrent-file intact, it doesn't just point transmission at a url, so that should get around problems involving authentication, etc.

I use the applescript editor and save it as the program "OpenTorrent". Note, this may not be the best code in existance - it's my first attempt at applescript, but it could serve as a good starting point for someone else, and may be useful to enough people i thought i'd post it.

This version works with Transmission 1.53+:

Code: Select all

on open some_items
	set transmissionurl to "http://admin:password@your.host.name:9091"
	set rpcurl to transmissionurl & "/transmission/rpc"
	repeat with this_item in some_items
		set filename to POSIX path of this_item
		set b64 to do shell script "openssl base64 -e -in " & quoted form of (filename) & " | tr -d \"\\n\" | tr -d \"\\r\" "
		set session to do shell script "curl -D - " & rpcurl & " 2>/dev/null | grep '^X-Transmission-Session-Id'"
		set json to "{ \"method\": \"torrent-add\", \"arguments\": { \"metainfo\": \"" & b64 & "\" } }"
		set result to do shell script "curl -H " & quoted form of (session) & " -d " & quoted form of (json) & " " & rpcurl
	end repeat
	tell application "Firefox"
		OpenURL transmissionurl
	end tell
end open
and this version works with versions with clutch/versions with rpc < 1.53

Code: Select all

on open some_items
        set transmissionurl to "http://admin:password@your.host.name:9091"
        set rpcurl to transmissionurl & "/transmission/rpc"
        repeat with this_item in some_items
                set filename to POSIX path of this_item
                set b64 to do shell script "openssl base64 -e -in " & quoted form of (filename) & " | tr -d \"\\n\" | tr -d \"\\r\" "
                set json to "{ \"method\": \"torrent-add\", \"arguments\": { \"metainfo\": \"" & b64 & "\" } }"
                set result to do shell script "curl -d " & quoted form of (json) & " " & rpcurl
        end repeat
        tell application "Firefox"
                OpenURL transmissionurl
        end tell
end open
note, you have to change: set transmissionurl to "http://admin:password@your.host.name:9091" to point to the appropriate url

Re: AppleScript Remote Torrent Launcher

Posted: Mon Jun 01, 2009 7:48 pm
by aaron_stasis
whoops. i see i'm a little late to the party.

http://forum.transmissionbt.com/viewtop ... 33&start=0

does the same thing with bash scripting instead. looks like we're both using similar methods. i was going to tackle the bash script next for my linux machines, so thanks for saving me the trouble.

Re: AppleScript Remote Torrent Launcher

Posted: Mon Jun 01, 2009 10:55 pm
by aaron_stasis
here's a modification that prompts you for your url the first time you run it (you can reset the url by running it without any input torrents). it also shows you any errors, and allows you to choose whether or not you view the results. This version also should work with any version of transmission/clutch with rpc that supports posting torrents.

Paste code into "Script Editor"
Save as application "OpenTorrent"
use "OpenTorrent" to open your torrent files from safari, firefox, or Finder.

Code: Select all

property transmissionurl : ""

on open some_items
	if transmissionurl is equal to "" then
		set newurl to display dialog "Transmission Url" default answer "http://username:password@host.name:9091" with title "Preferences"
		set transmissionurl to text returned of newurl
	end if
	
	set rpcurl to transmissionurl & "/transmission/rpc"
	
	set successes to ""
	set failures to ""
	
	repeat with this_item in some_items
		set filename to POSIX path of this_item
		set b64 to do shell script "openssl base64 -e -in " & quoted form of (filename) & " | tr -d \"\\n\" | tr -d \"\\r\" "
		try
			set session to do shell script "curl -D - " & rpcurl & " 2>/dev/null | grep '^X-Transmission-Session-Id'"
		on error
			set session to ""
		end try
		set json to "{ \"method\": \"torrent-add\", \"arguments\": { \"metainfo\": \"" & b64 & "\" } }"
		if session is equal to "" then
			set result to do shell script "curl -d " & quoted form of (json) & " " & rpcurl
		else
			set result to do shell script "curl -H " & quoted form of (session) & " -d " & quoted form of (json) & " " & rpcurl
		end if
		set resultstr to do shell script "echo " & quoted form of (result) & " | sed -e 's/.*\"result\": \"\\([^\"]*\\)\".*/\\1/'"
		if "success" is in resultstr then
			set successes to successes & "* " & this_item & "
"
		else
			set failures to failures & " * " & resultstr & " -  " & this_item & " 
"
		end if
		
		if failures is not equal to "" then
			display alert "Failed!" message "FAILED Torrents: 
" & failures as warning
		end if
		
		if successes is not equal to "" then
			set question to display dialog "Successfully Started Torrents: 
" & successes buttons {"OK", "View"} default button 2
			set answer to button returned of question
			
			if answer is equal to "View" then
				open location transmissionurl
			end if
		end if
	end repeat
end open

set newurl to display dialog "Transmission Url" default answer transmissionurl with title "Preferences"
set transmissionurl to text returned of newurl
or just download as an app here: http://aaron.stasis.org/OpenTorrent.dmg

Re: AppleScript Remote Torrent Launcher

Posted: Wed Jun 03, 2009 9:01 am
by BELzEBUB
I realy like your AppleScript, what you did is exaclty what i tried to do but I didn't know how. So I made a bash-script to upload all downloaded torrent-files. :D

Re: AppleScript Remote Torrent Launcher

Posted: Wed Jun 10, 2009 12:08 pm
by godsyn
BELzEBUB wrote:I realy like your AppleScript, what you did is exaclty what i tried to do but I didn't know how. So I made a bash-script to upload all downloaded torrent-files. :D
Would you please share?

Re: AppleScript Remote Torrent Launcher

Posted: Thu Jun 11, 2009 6:32 pm
by BELzEBUB
godsyn wrote:
BELzEBUB wrote:I realy like your AppleScript, what you did is exaclty what i tried to do but I didn't know how. So I made a bash-script to upload all downloaded torrent-files. :D
Would you please share?
The Script can be found in this topic http://forum.transmissionbt.com/viewtop ... f=8&t=7633.

Re: AppleScript Remote Torrent Launcher

Posted: Fri Dec 09, 2016 4:48 pm
by Nicode70
thank you very much. i've tried the app, but get an error message: invalide or corrupt torrent file.
could you please help me?

Re: AppleScript Remote Torrent Launcher

Posted: Sat Dec 10, 2016 8:37 am
by Nicode70
yes i have tried with different .torrent from various sources. each one gives the same invalide or corrupt error. but i can open them in trans gui and transmission without a problem.