我刚开始使用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的教程,但没有一个有用。我错过了关于Applecript如何工作的基本信息吗?
由于
**编辑
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_
结束脚本
答案 0 :(得分:2)
属性在Xcode脚本中不是持久的,但您可以使用user defaults系统。要使用默认值,请在应用程序启动时注册一些初始值,然后从默认值中读取(如果之前已保存,将覆盖已注册的值),并在应用程序退出时保存新值 - 例如:< / p>
property theLocation : "" -- this will be the (text) folder path
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
-- other initialization stuff
end applicationWillFinishLaunching_
on buttonClick_(sender)
if theLocation is "" then
set theLocation to choose folder with prompt "Where to save your project?"
set theLocation to theLocation as text
end if
-- display dialog theLocation
tell application "Finder"
-- create folder structure
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_
答案 1 :(得分:0)
您可以在脚本的开头定义一个属性,这样即使在脚本退出后也可以存储信息...
property theLocation : null
if theLocation is null then set theLocation to (choose folder with prompt "Where to save your project?"
...
该脚本将别名存储在“theLocation”中以供将来使用。但是,保存或重新编译脚本会将属性重置为其原始值。