首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >AppleScript应用程序的全局首选项

AppleScript应用程序的全局首选项
EN

Stack Overflow用户
提问于 2011-05-06 02:03:53
回答 3查看 1.3K关注 0票数 2

是否可以保存在AppleScript中创建的应用程序的某种设置?

这些设置应在脚本开始时加载,并在脚本结束时保存。

示例:

代码语言:javascript
运行
复制
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就是我需要的函数。

EN

回答 3

Stack Overflow用户

回答已采纳

发布于 2011-05-06 02:25:23

脚本properties是永久性的,但是只要重新保存脚本,保存的值就会被脚本中指定的值覆盖。运行:

代码语言:javascript
运行
复制
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

票数 4
EN

Stack Overflow用户

发布于 2012-10-19 20:03:24

这也工作得很好(请查看提示的第一条注释):

http://hints.macworld.com/article.php?story=20050402194557539

它使用"defaults“系统,您可以在~/Library/Preferences中获取您的首选项

票数 3
EN

Stack Overflow用户

发布于 2014-01-11 09:49:53

Applescript支持通过系统事件本地读写plist:

代码语言:javascript
运行
复制
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的存在不确定,你需要把它包装在上,试试

代码语言:javascript
运行
复制
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
票数 3
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/5902202

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档