Reading/Writing Transmission.plist

I would like to take the transmission plist file, put it into a list, change some values then save it back. This is what a plist looks like.

I can get a list from the plist using this code.

set thePListFile to "path/to/Transmission/Transfers.plist"
tell application "System Events"
	set plistFile to property list file thePListFile
	set plistItems to value of property list items of plistFile
end tell

The list looks like the following. I can’t seem to figure out what the list values are in and how to do anything with them.

Hi Greg,

You have a list of two records. Here’s an example to change Active of the first record to false:

set the_result to {{Active:true, TorrentHash:"blablabla", InternalTorrentPath:"path/to/file.torrent", WaitToStart:false, GroupValue:-1, RemoveWhenFinishSeeding:true}, {Active:true, TorrentHash:"blablabla", InternalTorrentPath:"path/to/otherfile.torrent", WaitToStart:false, GroupValue:-1, RemoveWhenFinishSeeding:true}}

set Active of item 1 of the_result to false
return the_result

gl,
kel

Thank you kel1.

I couldn’t figure out how to work with those items in the list, how to address them (I’m used to lists of text items). This makes much more sense.

Hello.

I surmise you want to access the items by InternalTorrentPath, what I wonder are, what your goals with the other items in the dictionary for that particular torrent are; is it to set flip active state, of one or more torrents, or is it something else?

I want to make certain torrents active or inactive based on their name which I can get via InternalTorrentPath. Not sure what I want to do with the other stuff, maybe put stuff in groups.

Since transmission ins’t scriptable directly, quitting transmission, editing the plist and restarting transmission is my workaround. Not sure if it’s going to work.

Hello.

Don’t hold your breath for it, but by tomorrow your time, I’ll be back with a solution I have to create first:
It will be an AppleScript list dialog box, that has check marks for the torrents that are active. You’ll then be able to select multiple torrents in one go to flip their status. When you press ok, then a notification turns up telling you how many torrents were flipped, at the same time, a text edit document are generated, telling you exactly which torren’ts statuses were flipped.

Should the script just restart transmission, or would you like to be in control over that yourself?

Hello.

This works for me, that is, I can see that I change the values of the different torrents, but I haven’t actually tried it. -Ill leave that to you, as I have no idea of anything I want at the moment. :slight_smile:

property debug : true
try
	local transfPlist, trrStatus, trrName, trrntz
	set transfPlist to pathtotransferplist()
	-- time to read in the torrent data!
	set trrntz to {}
	tell application id "sevs" to tell property list file transfPlist
		set theDicts to every property list item of it whose kind is record
		repeat with aDict in theDicts
			tell contents of aDict
				set trrStatus to value of property list item "Active"
				set trrName to my fetch(my fetch(my fetch((value of property list item "InternalTorrentPath") as text, -1, "/"), 1, "."), 1, "~")
				if trrStatus then
					set end of trrntz to (trrName & " ✓")
				else
					set end of trrntz to trrName
				end if
			end tell
		end repeat
	end tell
	
	local jobList, theSel
	set theSel to choose from list trrntz default items (item 1 of trrntz) with prompt "Choose Torrents to activate/deactivate." with title "Configure Transmission..." with multiple selections allowed
	if theSel is not false then
		set jobList to {}
		_MAKEDOC("Configuring Transmission..." & linefeed, 400, 600)
		repeat with aChoice in theSel
			
			if (aChoice as text) ends with " ✓" then
				set contents of aChoice to text 1 thru -3 of contents of aChoice
				set toDeactivate to true
			else
				set toDeactivate to false
			end if
			set end of jobList to {contents of aChoice as text, toDeactivate}
		end repeat
	else
		error number -128 -- User defaulted
	end if
	tell application id "sevs" to tell property list file transfPlist
		theDicts to every property list item of it whose kind is record
		repeat with aDict in theDicts
			tell aDict
				repeat with aJob in jobList
					if item 1 of aJob is in ((value of property list item "InternalTorrentPath") as text) then
						if (item 2 of aJob) then
							set value of its property list item "Active" to false
							my _PRINT(item 1 of aJob & " Deactivated")
						else
							set value of its property list item "Active" to true
							my _PRINT(item 1 of aJob & " Activated")
						end if
						exit repeat
					end if
				end repeat
			end tell
		end repeat
	end tell
on error e number n
	local s
	if n = 4000 then
		set s to "Transmission hasn't created a directory in the Library/Applications Support Folder"
	else if n = 4001 then
		set s to "Can't find Transfer.plist in the Transmission folder of Library/Application Support"
	else
		set s to e
	end if
	display alert s
	
end try

on _PRINT(txt)
	# Nigel Garvey made the original version.
	local a
	tell application "TextEdit"
		if not my debug then
			activate
		else
			do shell script "open -b \"com.apple.textedit\""
		end if
		if ((count its documents) is 0) then make new document
		set a to (get path of its front document)
		try
			if a is "" then set a to missing value
		on error
			set a to missing value
		end try
		if a is not missing value then make new document
		make new paragraph at end of text of front document with data (txt & linefeed) with properties {font:"Andale Mono", size:12}
	end tell
	return true
end _PRINT

on _MAKEDOC(heading, _width, _height)
	tell application "TextEdit"
		tell (make new document at front)
			set text of it to heading
			set font of every character to "Andale Mono"
			set size of every character to 13
		end tell
		tell its front window
			#	set {item 3 of its bounds, item 4 of its bounds} to {800, 300}
			set _bounds to bounds
			set item 3 of _bounds to (item 1 of _bounds) + _width
			set item 4 of _bounds to (item 2 of _bounds) + _height
			set bounds to _bounds
			
		end tell
	end tell
end _MAKEDOC


on fetch(theText, theItem, theDelim)
	local l, tids
	-- to be expanded with ability for range
	tell (a reference to AppleScript's text item delimiters)
		set {tids, contents of it} to {contents of it, theDelim}
		set l to text items of theText
		set contents of it to tids
	end tell
	return item theItem of l
end fetch

on pathtotransferplist()
	local pth
	set pth to ((path to application support folder from user domain as text) & "Transmission:")
	local probe
	try
		set probe to pth as alias
	on error
		-- we fall back to local domain
		set pth to ((path to application support folder from local domain as text) & "Transmission:")
		try
			set probe to pth as alias
		on error
			error number 4000
		end try
	end try
	local tranfPlist
	set tranfPlist to (pth & "Transfers.plist")
	try
		set probe to tranfPlist as alias
	on error
		error number 4001
	end try
	return tranfPlist
end pathtotransferplist