AppleScript Library

Property List Library

   Create, read from, write to and add keys to property list files.

All handlers displayed on this page were freely available on the internet or written by myself. Please refer to the copyright section at the end of this page. Feedback appreciated.


AppleScript Editor open download

Properties
myName
Read Keys
readKey
readSubKey
Write Keys
setKey
setSubKey
Add Keys
addKey
addKey2
New Plist
newPlist

Properties

property myName

Name that should be used when loading this library.

show source AppleScript-Editor open insert append toc
property myName : "_plist"

Read Keys

readKey(posixPath, keyname)

Read value of existing key in existing plist.

posixPathstringposix path to plist
keynamestringname of property (case-sensitive)
anythingvalue of key

Written by:ljr

Example:

readKey(thePListPath, "User Email") 
--> value of key in plist
show source AppleScript Editor open insert append toc
on readKey(posixPath, keyname)
	local posixPath, keyname, val
	try
		tell application "System Events"
			tell property list file posixPath
				tell contents
					set val to value of property list item keyname
				end tell
			end tell
		end tell
		return val
	on error eMsg number eNum
		error "Can't readKey: " & eMsg number eNum
	end try
end readKey

readSubKey(posixPath, keyname, subKeyName)

Read sub-value of existing value of existing key in existing plist.

posixPathstringposix path to plist
keynamestringname of property (case-sensitive)
subKeyNamestring(case-sensitive)
anythingvalue of sub-key

Written by:ljr

Example:

readSubKey(posixPath, "Rec", "Icecream") 
--> sub-key value of key in plist
show source AppleScript Editor open insert append toc
on readSubKey(posixPath, keyname, subKeyName)
	local posixPath, keyname, subKeyName, val
	try
		tell application "System Events"
			tell property list file posixPath
				tell contents
					set val to value of property list item subKeyName ¬
						of property list item keyname
				end tell
			end tell
		end tell
		return val
	on error eMsg number eNum
		error "Can't readSubKey: " & eMsg & eNum number eNum
	end try
end readSubKey

Write Keys

setKey(posixPath, keyname, keyValue)

Set value of existing key in existing plist.

posixPathstringposix path to plist
keynamestringname of property (case-sensitive)
keyValueanything
nothing

Written by:ljr

Example:

setKey(thePListPath, "Rec", {|IceCream|:true}) 
--> key value in plist changed
show source AppleScript Editor open insert append toc
on setKey(posixPath, keyname, keyValue)
	local posixPath, keyname, keyValue
	try
		tell application "System Events"
			tell property list file posixPath
				tell contents
					set value of property list item keyname to keyValue
				end tell
			end tell
		end tell
	on error eMsg number eNum
		error "Can't setKey: " & eMsg & eNum number eNum
	end try
end setKey

setSubKey(posixPath, keyname, subKeyName, subKeyValue)

Set sub-value of existing value of existing key in existing plist.

posixPathstringposix path to plist
keynamestringname of property (case-sensitive)
subKeyNamestring(case-sensitive)
subKeyValueanything
nothing

Written by:ljr

Example:

setSubKey(posixPath, "Rec", "IceCream", false) 
--> sub-key value in key in plist changed
show source AppleScript Editor open insert append toc
on setSubKey(posixPath, keyname, subKeyName, subKeyValue)
	local posixPath, keyname, subKeyValue, subKeyName
	try
		tell application "System Events"
			tell property list file posixPath
				tell contents
					set value of property list item subKeyName ¬
						of property list item keyname to subKeyValue
				end tell
			end tell
		end tell
	on error eMsg number eNum
		error "Can't setSubKey: " & eMsg number eNum
	end try
end setSubKey

Add Keys

addKey(posixPath, keyname, keyValue)

Add property item to existing plist. Case of key name is preserved. Case is not preserved for subkeys in records unless properly piped. (Consider using addKey2 instead!)

posixPathstringposix path to plist
keynamestringname of new property
keyValueanything
nothing

Written by:ljr

Example:

addKey(POSIX path of (path to desktop as string) & "newPlist.plist", "NewKey", {"ABC", 33}) 
--> new key in plist
show source AppleScript Editor open insert append toc
on addKey(posixPath, keyname, keyValue)
	local posixPath, keyname, keyValue
	local newStr, chars, lst, scpt, oldStr, ASTID
	set ASTID to AppleScript's text item delimiters
	try
		--escape backslashes and pipes in the identifier-to-be
		set keyname to keyname as string
		repeat with chars in {{"\\", "\\\\"}, {"|", "\\|"}}
			set {oldStr, newStr} to chars
			if oldStr is in keyname then
				set AppleScript's text item delimiters to oldStr
				set lst to keyname's text items
				set AppleScript's text item delimiters to newStr
				set keyname to lst as string
			end if
		end repeat
		set keyname to "|" & keyname & "|"
		set AppleScript's text item delimiters to ASTID
		set scpt to "script\r\ton fn(posixPath,keyValue)\r\t\ttell application " & ¬
			"\"System Events\"\r" & "\t\t\ttell property list file posixPath\r" & ¬
			"\t\t\t\ttell contents\r\t\t\t\t\tset previousValue to value\r\t\t" & ¬
			"\t\t\tset value to (previousValue & {" & keyname & ¬
			":keyValue})\r\t\t\t\tend tell\r\t\t\tend tell\r\t\tend tell\r\tend" & ¬
			" fn\t\rend script"
		fn(posixPath, keyValue) of (run script scpt)
	on error eMsg number eNum
		set AppleScript's text item delimiters to ASTID
		error "Can't addKey: " & eMsg number eNum
	end try
end addKey

addKey2(posixPath, keyname, keyValue, keyType)

Add property item to existing plist. Case of key name is preserved. Case of subkey names in records is also preserved.

posixPathstringposix path to plist
keynamestringname of new property
keyValueanything
keyTypeAppleScript class supported by plist
nothing

Written by:ljr

Example:

addKey2(plistPath, "NewKey", {"ABC", 33}, list) 
--> new key in plist
show source AppleScript Editor open insert append toc
on addKey2(posixPath, keyname, keyValue, keyType)
	local posixPath, keyname, keyValue, plistFile
	local keyType
	try
		tell application "System Events"
			set plistFile to property list file posixPath
			make new property list item at end of property list items ¬
				of contents of plistFile with properties ¬
				{kind:keyType, name:keyname, value:keyValue}
		end tell
	on error eMsg number eNum
		error "Can't addKey2: " & eMsg number eNum
	end try
end addKey2

New Plist

newPlist(posixPath)

Create a new plist. Overwrites existing files.

posixPathstringposix path to plist
nothing

Written by:ljr (based on Mac OS X Automation)

Example:

newPlist(POSIX path of (path to desktop as string) & "newPlist.plist") 
--> new plist
show source AppleScript Editor open insert append toc
on newPlist(posixPath)
	local posixPath, parentDictionary, thisPlistFile
	try
		--http://www.macosxautomation.com/applescript/features/propertylists.html
		tell application "System Events"
			-- create an empty property list dictionary item
			set parentDictionary to ¬
				make new property list item with properties {kind:record}
			-- create new property list file using the empty dictionary 
			-- list item as contents
			set thisPlistFile to ¬
				make new property list file with properties ¬
					{contents:parentDictionary, name:posixPath}
			return thisPlistFile
		end tell
	on error eMsg number eNum
		error "Can't newPlist: " & eMsg number eNum
	end try
end newPlist

Copyright

© 2009 ljr (http://applescript.bratis-lover.net)
all handlers

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON INFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.