AppleScript应用程序的全局首选项

AppleScript应用程序的全局首选项,applescript,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", timesR

是否可以在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函数将是我需要的函数。

脚本是持久的,尽管保存的值会在每次保存脚本时被脚本中指定的值覆盖。运行:

property |count| : 0
display alert "Count is " & |count|
set |count| to |count| + 1
再保存几次,然后再运行几次

如果要使用用户默认值系统,可以使用
do shell script“defaults…”
命令或(如果使用Applescript Studio)
用户默认值的默认条目“propertyName”
。在Applescript Studio中,您。

这也可以正常工作(检查提示的第一条注释):


它使用“默认”系统,您可以在~/Library/preferences

Applescript中获得您的首选项,它支持通过系统事件本机读取和写入plists:

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
唯一的问题是它无法创建plist,因此如果不确定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