2010-09-19 41 views
1

我想写一个脚本,将一个新的关键字添加到字典plist。当使用字典的字符串作为字典的键时,脚本工作正常,但我似乎无法弄清楚如何使用keyname的变量。脚本在下面,几乎工作。唯一的问题是能够使用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

尝试......

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 
+0

如果我可以把2个检查这个答案我会的。 AppleScripting plists一直在杀死我。谢谢! – prime31 2010-09-20 15:15:34

+0

很高兴我可以帮助uprise! – regulus6633 2010-09-21 04:16:27