1

我刚从 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_

结束脚本

4

2 回答 2

2

属性在 Xcode 脚本中不是持久的,但您可以使用用户默认系统。要使用默认值,您在应用程序启动时注册一些初始值,然后从默认值中读取(如果之前保存过注册值,这将覆盖注册值),当您的应用程序退出时保存新值 - 例如:

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_
于 2011-09-12T19:42:52.413 回答
0

您可以在脚本的开头定义一个属性,即使在脚本退出后也可以存储信息...

property theLocation : null
if theLocation is null then set theLocation to (choose folder with prompt "Where to save your project?"
...

该脚本会将别名存储在“theLocation”中以供将来使用。但是,保存或重新编译脚本会将属性重置为其原始值。

于 2011-09-12T18:47:47.957 回答