Add to Plist Array

Hello all,
I’m currently trying to append an element to a plist array, but I’m getting the error “error “System Events got an error: Handler only handles single objects.” number -10014”. Here is the snippet of code that is breaking:


tell application "System Events"
		tell property list items of property list file dataFile
			tell property list items of property list item "bookmarks" to make new property list item at end with properties {kind:string, name:bookmarkName, value:bookmarkName}
		end tell
	end tell

Any help would be greatly appreciated.

Thanks,
CamSox

Hi, CamSox.

Here is the working script.
To add top-level property list item is enough this:


set bookmarkName to "myOneBookMark"
set dataFile to (choose file of type {"plist"}) as string

tell application "System Events" to tell property list file dataFile to tell contents
	make new property list item at end of every property list item with properties {kind:string, name:bookmarkName, value:bookmarkName}
end tell

To add key 1 level deeper, than the top level, you can do something like this:


set bookmarkName to "myOneBookMark"
set dataFile to (choose file of type {"plist"}) as string

tell application "System Events" to tell property list file dataFile
		get property list items
		-- RESULT: {property list item "FinderOrdering" of contents of property list file "HARD_DISK:Users:123:Desktop:pbs.plist" of application "System Events", property list item "NSServicesStatus" of contents of property list file "HARD_DISK:Users:123:Desktop:pbs.plist" of application "System Events", property list item "FinderActive" of contents of property list file "HARD_DISK:Users:123:Desktop:pbs.plist" of application "System Events"}
		
		tell property list item "NSServicesStatus" of contents
			make new property list item at end of every property list item with properties {kind:string, name:bookmarkName, value:bookmarkName}
		end tell

end tell

NOTE: You should first check the hierarchy of your plist file, as I do in the my script. Then you should tell to one property list item, at end of which you want add the new key.

To add key 2 levels deeper, you can do something like this:


set bookmarkName to "myOneBookMark"
set dataFile to (choose file of type {"plist"}) as string

tell application "System Events" to tell property list file dataFile to tell contents
	get property list items
	-- RESULT: {property list item "FinderOrdering" of contents of property list file "HARD_DISK:Users:123:Desktop:pbs.plist" of application "System Events", property list item "NSServicesStatus" of contents of property list file "HARD_DISK:Users:123:Desktop:pbs.plist" of application "System Events", property list item "FinderActive" of contents of property list file "HARD_DISK:Users:123:Desktop:pbs.plist" of application "System Events"}
	
	tell property list item "NSServicesStatus"
		get property list items
		-- RESULT: {property list item "com.apple.dt.instruments.backgroundinstruments - Time Profile Active Application - timeProfileFrontMost" of property list item "NSServicesStatus" of contents of property list file "HARD_DISK:Users:123:Desktop:pbs.plist" of application "System Events", property list item "myOneBookMark" of property list item "NSServicesStatus" of contents of property list file "HARD_DISK:Users:123:Desktop:pbs.plist" of application "System Events", property list item "com.apple.dt.instruments.backgroundinstruments - Toggle Instruments Recording - toggleRecordingState" of property list item "NSServicesStatus" of contents of property list file "HARD_DISK:Users:123:Desktop:pbs.plist" of application "System Events", property list item "com.apple.dt.instruments.backgroundinstruments - Time Profile App Under Mouse - timeProfileUnderMouse" of property list item "NSServicesStatus" of contents of property list file "HARD_DISK:Users:123:Desktop:pbs.plist" of application "System Events", property list item "com.apple.dt.instruments.backgroundinstruments - Time Profile Entire System - timeProfileSystem" of property list item "NSServicesStatus" of contents of property list file "HARD_DISK:Users:123:Desktop:pbs.plist" of application "System Events"}
		tell property list item "com.apple.dt.instruments.backgroundinstruments - Time Profile Active Application - timeProfileFrontMost"
			make new property list item at end of every property list item with properties {kind:string, name:bookmarkName, value:bookmarkName}
		end tell
	end tell
	
end tell

You can go as deeper, as need.