2017-11-26 10 views
0

종료 후 변수를 저장해야하는 매우 복잡한 AppleScript 응용 프로그램을 작성하고 있습니다. 따라서 변수가 실행 중일 때 변수를 설정하고 닫은 다음 다시 열면 변수가 동일하게 유지됩니다.AppleScript에서 종료하고 다시 연 후 변수를 저장하는 방법은 무엇입니까?

이 스크립트는 처음 실행시 설정 메뉴를 표시합니다. 그런 다음 응용 프로그램을 닫은 후에 환경 설정을 저장하십시오. 기타 기술적 설명 :

setup (실행)을 시작하면 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: 
+0

필자에게 적합합니다. 사용중인 OS X/macOS의 버전은 무엇입니까? – user3439894

+0

높은 시에라. 응용 프로그램으로 내보내고 작동하는지보십시오. – Josh

+1

실행 중에 다른 값으로 설정된 AppleScript 스크립트/응용 프로그램을 컴파일하거나 저장하면 원래 값으로 다시 설정된다는 점을 알고 있습니다. 이 예제에서'missing value', 맞습니까? 그것은 macOS High Sierra에서도 마찬가지입니다. – user3439894

답변

1

예 !!!! 드디어 답을 찾았습니다. 스크립트 중지 지점에서 property aStatusItem : missing value을 제거해야합니다. 이렇게하면 aStatusItem이 함수간에 사용되지 않습니다. 이 때문에 종료 할 때 메뉴 모음이 제거되지 않습니다. 끝에서 문제를 해결하려면 current application's NSStatusBar's systemStatusBar()'s removeStatusItem:aStatusItem에서 tell me to quit으로 변경하십시오. 그러면 메뉴 모음 항목이 제거되는 응용 프로그램이 종료됩니다.

+0

이 버튼을 누르면 모든 버튼이 작동하지 않습니다. – Josh

+0

'defaults read'와'defaultswrite'를 사용하면됩니다. 이렇게하면 .plist 파일에 정보가 저장됩니다. 비록, 나는 다른 사용자와 함께 그것이/속성과 함께 작동해야합니다 상태에 동의합니다. – ThrowBackDewd

+0

@ThrowBackDewd,'defaults'는'do shell script' _command_에서 사용할 수 있지만, ** AppleScript **의 ** System Events **는 ** Property List Suite **를 가지고 있으며 아마도 기본 메소드로 간주 될 것입니다 이 같은 유스 케이스의 plist 파일 작업을위한 AppleScript. 'property' _variable_의 사용에 관해서는, 이것이 보통의 AppleScript라면 문제가되지 않을 것입니다. 그러나 스크립트의 코딩이 문제가되거나이 스크립트가 스크립트 추가, Foundation 및 AppKit 프레임 워크를 사용하기 때문에 또는 그 이상이 문제가 될 수 있습니다. – user3439894