在applescript中写入/读取单个变量

发布于 2024-12-04 02:58:20 字数 3132 浏览 0 评论 0 原文

我刚刚开始使用 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_

结束脚本

I'm just starting with applescript in xcode and currently have an app that asks for a folder location, then creates a folder structure.

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_

Now I'm trying to have the app take "theLocation" folder alias and save it, so the next time the app launches it automatically chooses that folder as the save location without having to add it. I understand the logic that will go into it, but I can't figure out how to store/read information. I've tried tutorials on writing to the info.plist, but none of them have worked. Am I missing a basic piece of info on how applescript works?

Thanks

** Edit

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_

end script

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

为你拒绝所有暧昧 2024-12-11 02:58:20

属性在 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_

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:

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_
番薯 2024-12-11 02:58:20

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

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

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

You can define a property at the beginning of your script, which allows you to store information even after the script quits...

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

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文