gpt4 book ai didi

variables - 在 AppleScript 中退出并重新打开后如何保存变量?

转载 作者:行者123 更新时间:2023-12-02 01:06:02 25 4
gpt4 key购买 nike

我正在编写一个非常复杂的 AppleScript 应用程序,它需要在退出后保存变量。因此,如果我在运行时设置变量,关闭它,重新打开它,变量仍然是相同的。

此脚本旨在在首次运行时调出设置菜单。然后保存应用程序关闭后的首选项。更多技术说明:

当它启动时(运行时),它会检查 isSetup 是否为 false,如果是,它将转到函数 setup()。 setup() 函数设置首选项并将 isSetup 设置为 true。当我退出并重新打开应用程序时,将再次运行 setup() 函数。

我知道我不应该复制和粘贴完整的脚本,但如果没有它,我就无法找到复制错误的地方。这里是:

--AppleScript: menu bar script -- Created 2017-03-03 by Takaaki     Naganoya adapted by ----
--2017 Piyomaru Software
use AppleScript version "2.4"
use scripting additions
use framework "Foundation"
use framework "AppKit"
--http://piyocast.com/as/archives/4502

property aStatusItem : missing value
global theToggle
global theMenuTitle
global aTag
global aTitle
global isSetup
global usrName
global usrPass
global usrProtocol
property usrName : missing value
property usrPass : missing value
property isSetup : false
property usrProtocol : missing value
on run
if isSetup is false then
setup()
else
set theToggle to "Connect"
set theMenuTitle to "Server Helper"
init()

end if
end run

on init()
set aList to {theToggle, "Preferences", "Change Password", "", "Quit"}
set aStatusItem to current application's NSStatusBar's systemStatusBar()'s statusItemWithLength:(current application's NSVariableStatusItemLength)

aStatusItem's setTitle:theMenuTitle
aStatusItem's setHighlightMode:true
aStatusItem's setMenu:(createMenu(aList) of me)
end init

on createMenu(aList)
set aMenu to current application's NSMenu's alloc()'s init()
set aCount to 1
repeat with i in aList
set j to contents of i
if j is not equal to "" then
set aMenuItem to (current application's NSMenuItem's alloc()'s initWithTitle:j action:"actionHandler:" keyEquivalent:"")
else
set aMenuItem to (current application's NSMenuItem's separatorItem())
end if
(aMenuItem's setTarget:me)
(aMenuItem's setTag:aCount)
(aMenu's addItem:aMenuItem)
if j is not equal to "" then
set aCount to aCount + 1
end if
end repeat

return aMenu
end createMenu

on setup()
display dialog " Welcome to the Server setup Utility.
To Begin click " & quote & "Continue" & quote & " below." buttons {"Cancel", "Continue"} default button 2
set theButton to the button returned of the result

if theButton is "Continue" then
display dialog "Please enter your " & quote & "Username" & quote & " for the Brown Server." default answer "Username" buttons {"Continue"} default button 1
set usrName to the text returned of the result
display dialog "Please enter your " & quote & "Password" & quote & " for the Brown Server." default answer "" buttons {"Continue"} default button 1 with hidden answer
set usrPass to the text returned of the result
set listDeProtocols to {"AFP", "SMB", "WebDav", "FTP"}
set usrProtocol to (choose from list listDeProtocols with prompt "Choose Your Prefered Protocol. AFP is recomended. If AFP does not work try SMB. All others are not supported at this time")
set isSetup to true
postSet()
end if
end setup
on postSet()
if isSetup is false then
setup()
else
set theToggle to "Connect"
set theMenuTitle to "Server Helper"
init()
end if
end postSet

on changePref()

end changePref
on pref()
set length1 to the length of usrPass
set p1 to ""
set p2 to ""
repeat length1 times
set p1 to "•"
set p2 to p1 & p2
end repeat
display dialog "These are your following preferences. Click the " & quote & "Change" & quote & " to change.

Username: " & usrName & "
Password: " & p2 & "

Prefered Protocol: " & usrProtocol buttons {"Back", "Change"}
set theButton to the button returned of the result

if theButton is "Change" then
changePref()
end if
end pref

on actionHandler:sender
set aTag to tag of sender as integer
set aTitle to title of sender as string

if aTitle is not equal to "Quit" then
current application's NSStatusBar's systemStatusBar()'s removeStatusItem:aStatusItem
if aTitle is "Connect" then
set theToggle to "Disconnect"
init()
end if
if aTitle is "Disconnect" then
current application's NSStatusBar's systemStatusBar()'s removeStatusItem:aStatusItem
set theToggle to "Connect"
init()
end if

if aTitle is "Preferences" then
pref()
end if

if aTitle is "Change Password" then
changePass()
end if
else
current application's NSStatusBar's systemStatusBar()'s removeStatusItem:aStatusItem
end if
end actionHandler:

最佳答案

更新:不起作用

是的!!!!我终于找到了答案。您必须删除 property aStatusItem : missing value从脚本的停止。这将防止在函数之间使用 aStatusItem。因此,当您按退出时,菜单栏不会被删除。要在最后解决该问题,请更改 current application's NSStatusBar's systemStatusBar()'s removeStatusItem:aStatusItemtell me to quit .这将退出应用程序,导致菜单栏项被删除。

关于variables - 在 AppleScript 中退出并重新打开后如何保存变量?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47492958/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com