Multiple torrents/one file script

Discussion of Transmission that doesn't fit in the other categories
Post Reply
abigjerk
Posts: 1
Joined: Thu Aug 04, 2011 6:20 pm

Multiple torrents/one file script

Post by abigjerk »

I realize that Transmission doesn't have a dictionary for AppleScript, but I used a downloaded repository (mktorrent) to make a torrent-creating script that may be useful for some of you. It can make a torrent file with a different hash size so that you can make one file point to two different trackers and have them exist as separate entries in Transmission.

Please note that it checks to see if you have Xcode / Mac Ports installed. I know that Xcode is big, so if you don't feel comfortable downloading or installing it, just skip this script. Otherwise, it is pretty handy, and I could use some help improving it.

To use the script, open up AppleScript editor and paste the code in. Save-as an application and run it.

Enjoy.

Code: Select all

tell application "Finder"
	delay 1
	
	--First, the app checks to see if the installs are there.  Here's the default value:
	set installs to true
	
	--Xcode check
	set XCodeString to ""
	set Xcode to true
	set Xcodepath to ((startup disk as string) & "Developer:Applications:Xcode.app")
	if not (exists Xcodepath as alias) then
		set Xcode to false
		set installs to false
		set XCodeString to "You need to install XCode from the Mac App Store (free, ~3GB).  "
	end if
	
	--MacPorts check
	set MacPorts to true
	set MacPortsString to ""
	set MacPortspath to (((startup disk) as string) & "opt/local/var/MacPorts")
	if not (exists folder MacPortspath) then
		set MacPortsString to "You need to install Mac Ports from macports.org.  "
		set MacPorts to false
		set installs to false
	end if
	
	--MacPorts browser open for download
	if MacPorts is false then
		tell application "Safari"
			open location "http://www.macports.org/install.php"
		end tell
	end if
	
	--Xcode browser open for download
	if Xcode is false then
		tell application "Safari"
			open location "http://itunes.apple.com/us/app/xcode/id448457090?mt=12"
		end tell
	end if
	
	--check to see if mktorrents is installed
	set mktorrent to true
	if not (exists "/opt/local/bin/mktorrent" as POSIX file) then
		set mktorrent to false
		set installs to false
	end if
	
	--Install mktorrent the right way.
	if MacPorts is true and Xcode is true and mktorrent is false then
		display dialog "Please type in your password to install mktorrent."
		do shell script "/opt/local/bin/port install mktorrent" with administrator privileges
	end if
	if installs = false then
		display dialog (XCodeString & MacPortsString & "The components are required to run the script.  Restart the script when you're ready.") buttons {"OK, I get it."} default button 1
	end if
	
	--if the installs are there then start
	if installs is true then
		
		--Currently, Folder and File are separate because Apple wants it that way, and it is a much harder method to combine this
		display dialog "Are you choosing a file or a folder to torrent?" buttons {"Folder", "File"} default button 2
		if result = {button returned:"Folder"} then
			set TorrentPath to ((choose folder with prompt "Select a folder to create a torrent file") as string)
			set TorrentPathPosix to POSIX path of TorrentPath
			set fileName to (displayed name of alias TorrentPath)
			set fileSize to (physical size of alias TorrentPath)
			set pureName to fileName
		else
			set TorrentPath to ((choose file with prompt "Select a single file to create a torrent file") as string)
			set TorrentPathPosix to POSIX path of TorrentPath
			set fileSize to (physical size of item TorrentPath)
			set pureName to (displayed name of alias TorrentPath)
		end if
		
		-- find the appropriate piece size for the new torrent
		set fileSize to (physical size of item TorrentPath)
		if fileSize ≥ 2.147483648E+9 then
			set PieceLength to 21
			set defaultpiece to 20
		else if fileSize ≥ 1.073741824E+9 then
			set PieceLength to 20
			set defaultpiece to 19
		else if fileSize ≥ 5.36870912E+8 then
			set PieceLength to 19
			set defaultpiece to 18
		else if fileSize ≥ 367001600 then
			set PieceLength to 18
			set defaultpiece to 17
		else if fileSize ≥ 157286400 then
			set PieceLength to 17
			set defaultpiece to 16
		else if fileSize ≥ 52428800 then
			set PieceLength to 16
			set defaultpiece to 15
		else
			set PieceLength to 15
			set defaultpiece to 16
		end if
		
		set dtpath to (path to desktop folder)
		
		--Reveal the torrent file if it exists already and then quit
		if exists item (pureName & ".torrent") then
			display dialog "This torrent already exists!" buttons {"OK"} default button 1
			open dtpath
			reveal item (pureName & ".torrent")
			return 0
		end if
		
		--Choose a Piece Length
		display dialog "Recommended piece size is " & PieceLength & ".  Piece size must be between 15 and 28. What do you want to replace it with?" default answer defaultpiece buttons {"Run Away", "Pikachu, I choose you!"} default button 2
		
		if result = {button returned:"Pikachu, I choose you!"} then
			set PieceLength to (text returned of result) as number
		else if result = {button returned:"Run Away"} then
			display dialog "Got away safely!" buttons {"OK"} default button 1
			return 0
		end if
		
		--Make the Torrent and drop it on the desktop
		do shell script "cd ~/Desktop && /opt/local/bin/mktorrent -a google.com -l " & PieceLength & " " & quoted form of TorrentPathPosix
		open dtpath
		reveal item (pureName & ".torrent")
	end if
end tell
Post Reply