在applescript中写入/读取单个变量
我刚刚开始使用 xcode 中的 applescript,目前有一个应用程序要求提供文件夹位置,然后创建文件夹结构。
on buttonClick_(sender)
set theLocation to choose folder with prompt "Where to save your project?"
tell application "Finder"
set newFolder to make new folder at theLocation with properties {name:(theTextField's stringValue() as string)}
set fontsFolder to make new folder at newFolder with properties {name:"fonts"}
set jpgFolder to make new folder at newFolder with properties {name:"jpg-pdf"}
set mainFolder to make new folder at newFolder with properties {name:"main"}
set printFolder to make new folder at mainFolder with properties {name:"• for printer"}
set refverFolder to make new folder at newFolder with properties {name:"ref_ver"}
set supportFolder to make new folder at newFolder with properties {name:"support"}
end tell
quit
end buttonClick_
现在,我尝试让应用程序采用“theLocation”文件夹别名并保存它,以便下次应用程序启动时,它会自动选择该文件夹作为保存位置,而无需添加它。我理解其中的逻辑,但我不知道如何存储/读取信息。我尝试过有关写入 info.plist 的教程,但没有一个起作用。我是否缺少有关 applescript 如何工作的基本信息?
谢谢
** 编辑
script New_ProjectAppDelegate
property parent : class "NSObject"
property theTextField : missing value
property theLocation : ""
on applicationWillFinishLaunching_(aNotification)
tell standardUserDefaults() of current application's NSUserDefaults
registerDefaults_({theLocation:theLocation}) -- register the starting user default key:value items
set theLocation to objectForKey_("theLocation") as text -- read any previously saved items (which will update the values)
end tell
end applicationWillFinishLaunching_
on buttonClick_(sender)
if theLocation is "" then
set theLocation to choose folder with prompt "Where to save your project?"
tell standardUserDefaults() of current application's NSUserDefaults
setObject_forKey_(theLocation, "theLocation") -- update the default items
end tell
else
set theLocation to theLocation as text
--display dialog theLocation as text
end if
tell application "Finder"
set newFolder to make new folder at theLocation with properties {name:(theTextField's stringValue() as string)}
set fontsFolder to make new folder at newFolder with properties {name:"fonts"}
set jpgFolder to make new folder at newFolder with properties {name:"jpg-pdf"}
set mainFolder to make new folder at newFolder with properties {name:"main"}
set printFolder to make new folder at mainFolder with properties {name:"• for printer"}
set refverFolder to make new folder at newFolder with properties {name:"ref_ver"}
set supportFolder to make new folder at newFolder with properties {name:"support"}
end tell
quit
end buttonClick_
on applicationShouldTerminate_(sender)
tell standardUserDefaults() of current application's NSUserDefaults
setObject_forKey_(theLocation, "theLocation") -- update the default items
end tell
return current application's NSTerminateNow
end applicationShouldTerminate_
结束脚本
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
属性在 Xcode 脚本中并不持久,但您可以利用 用户默认系统。要使用默认值,您可以在应用程序启动时注册一些初始值,然后从默认值中读取(如果之前已保存,这将覆盖注册的值),并在应用程序退出时保存新值 - 例如:
Properties are not persistent in Xcode scripts, but you can utilize the user defaults system. To use the defaults, you register some initial values when your application starts up, then read from the defaults (which will overwrite the registered values if they have been saved before), and when your application quits save the new values - for example:
您可以在脚本的开头定义一个属性,它允许您即使在脚本退出后也可以存储信息...
脚本会将别名存储在“theLocation”中以供将来使用。但是,保存或重新编译脚本会将属性重置回其原始值。
You can define a property at the beginning of your script, which allows you to store information even after the script quits...
The script will store the alias in "theLocation" for future use. However, saving or recompiling the script will reset the property back to its original value.