如何使用键名称的变量将项目添加到字典中?

时间:2010-09-19 01:09:21

标签: macos applescript

我正在尝试编写一个脚本,将新的键添加到字典plist中。当字符串用于字典的键时,脚本工作正常,但我似乎无法弄清楚如何使用变量作为键名。脚本在下面,几乎正常工作。唯一的问题是能够使用keyName作为变量而不是字符串文字(它现在正在做)。

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

结束pListAddValueForKey

1 个答案:

答案 0 :(得分:1)

试试这个......

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