AppleScript Remote Torrent Launcher

Discussion of the Web Interface for Transmission, formerly known as Clutch. This applies to all version of Transmission
aaron_stasis
Posts: 3
Joined: Mon Jun 01, 2009 7:16 pm

AppleScript Remote Torrent Launcher

Postby aaron_stasis » Mon Jun 01, 2009 7:34 pm

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

aaron_stasis
Posts: 3
Joined: Mon Jun 01, 2009 7:16 pm

Re: AppleScript Remote Torrent Launcher

Postby aaron_stasis » Mon Jun 01, 2009 7:48 pm

whoops. i see i'm a little late to the party.

viewtopic.php?f=8&t=7633&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.

aaron_stasis
Posts: 3
Joined: Mon Jun 01, 2009 7:16 pm

Re: AppleScript Remote Torrent Launcher

Postby aaron_stasis » Mon Jun 01, 2009 10:55 pm

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

BELzEBUB
Posts: 18
Joined: Sun Apr 19, 2009 8:06 am

Re: AppleScript Remote Torrent Launcher

Postby BELzEBUB » Wed Jun 03, 2009 9:01 am

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

godsyn
Posts: 36
Joined: Mon Mar 31, 2008 10:10 pm

Re: AppleScript Remote Torrent Launcher

Postby godsyn » Wed Jun 10, 2009 12:08 pm

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?

BELzEBUB
Posts: 18
Joined: Sun Apr 19, 2009 8:06 am

Re: AppleScript Remote Torrent Launcher

Postby BELzEBUB » Thu Jun 11, 2009 6:32 pm

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 viewtopic.php?f=8&t=7633.

Nicode70
Posts: 5
Joined: Fri Dec 09, 2016 3:01 pm

Re: AppleScript Remote Torrent Launcher

Postby Nicode70 » Fri Dec 09, 2016 4:48 pm

thank you very much. i've tried the app, but get an error message: invalide or corrupt torrent file.
could you please help me?

x190
Posts: 5094
Joined: Sun Nov 30, 2008 4:59 am

Re: AppleScript Remote Torrent Launcher

Postby x190 » Sat Dec 10, 2016 2:50 am

Nicode70 wrote:thank you very much. i've tried the app, but get an error message: invalide or corrupt torrent file.
could you please help me?

Have you tried a known valid torrent? That one may, in fact, be invalid or corrupt.
The stone age didn’t end because we ran out of stones.
---The Great Disruption - by Paul Gilding
https://paulgilding.com/
-------------------------------------------------

Nicode70
Posts: 5
Joined: Fri Dec 09, 2016 3:01 pm

Re: AppleScript Remote Torrent Launcher

Postby Nicode70 » Sat Dec 10, 2016 8:37 am

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.

x190
Posts: 5094
Joined: Sun Nov 30, 2008 4:59 am

Re: AppleScript Remote Torrent Launcher

Postby x190 » Sat Dec 10, 2016 6:51 pm

Then the script must be grabbing the wrong url, I guess.
The stone age didn’t end because we ran out of stones.
---The Great Disruption - by Paul Gilding
https://paulgilding.com/
-------------------------------------------------


Return to “Web Interface”