How do you add an item to dictionary with a variable for a key name?
I am trying to write a script that will add a new k开发者_如何学Cey to a dictionary plist. The script works fine when using a string for the key of the dictionary but I can't seem to figure out how to use a variable for the keyname. The script is below and is almost working. THe only issue is being able to use keyName as a variable instead of a string literal (which it is doing now).
on pListAddValueForKey(plistFile, keyName, keyValue)
tell application "System Events"
tell property list file plistFile
tell contents
set previousValue to value
set value to (previousValue & {keyName:keyValue}) -- this is the line in need of assistance
end tell
end tell
end tell
end pListAddValueForKey
Try this...
on pListAddValueForKey(plistFile, keyName, keyValue)
if class of plistFile is not text then set plistFile to plistFile as text
tell application "System Events"
tell property list file plistFile
tell contents
make new property list item at end of property list items with properties {name:keyName, value:keyValue}
end tell
end tell
end tell
end pListAddValueForKey
精彩评论