2011-05-05 55 views
2

是否可以保存AppleScript中创建的应用程序的某种设置?AppleScript应用程序的全局首选项

设置应该在脚本的开始加载并保存在脚本的末尾。

例子:

if loadSetting("timesRun") then 
    set timesRun to loadSetting("timesRun") 
else 
    set timesRun to 0 
end if 
set timesRun to timesRun + 1 
display dialog timesRun 
saveSetting("timesRun", timesRun) 

凡对话框将显示1第一次运行该脚本,2第二次...

而且功能loadSetting和saveSetting将是我所需要的功能。

回答

4

脚本properties是持久的,尽管每当您重新保存脚本时,保存的值将被脚本中指定的值覆盖。运行:

property |count| : 0 
display alert "Count is " & |count| 
set |count| to |count| + 1 

几次,重新保存它,然后运行它几个。

如果要使用用户默认系统,可以使用do shell script "defaults ..."命令或(如果使用Applescript Studio)default entry "propertyName" of user defaults。在Applescript Studio中,您可以使用bind values to user defaults

+0

非常感谢! – Tyilo 2011-05-05 19:16:46

3

AppleScript的支持本地阅读,并通过系统事件编写的Plist:

use application "System Events" # Avoids tell blocks, note: 10.9 only 

property _myPlist : "~/Library/Preferences/com.plistname.plist 

set plistItemValue to get value of property list item "plistItem" of contents of property list file _myPlist 
set plistItemValue to plistItemValue + 1 

set value of property list item "plistItem" of contents of property list file _myPlist to plistItemValue 

与此唯一的问题是THA它不能创建plists所以如果plist的存在不确定,你需要将它包装在尝试

try 
    set plistItemValue to get value of property list item "plistItem" of contents of property list file _myPlist 
on error -1728 # file not found error 
    do shell script "defaults write com.plistname.plist plistItem 0" 
    set plistItemValue to get value of property list item "plistItem" of contents of property list file _myPlist 
end try