I was working on applescripts for our lab environment that necessitated creating and reading from a plist file in the user’s preferences folder. The script below will create a plist file with default values and if the plist exists verify that the domain key pair necessary for the applescript is present, if not it will create the domain key with a default value.
--variables
set prefFolderPath to path to preferences folder as text
set prefFileName to "edu.domain.application.plist"
set prefFilePath to prefFolderPath & prefFileName
--verify that edu.domain.application.plist exists
repeat
try
get prefFilePath as alias
set plistExist to "true"
on error
set plistExist to "false"
end try
--if plist does not exist create it
if plistExist is "false" then
--create domain key pair with default value
do shell script "defaults write edu.domain.application someKey someValue"
else
--if plist does exist, verify key to read exists
if plistExist is "true" then
try
do shell script "defaults read edu.domain.application someKey"
on error
--if domain key pair does not exist, create with default value
do shell script "defaults write edu.domain.application someKey somevalue"
end try
exit repeat
end if
end if
end repeat