2017-11-26 18 views
0

非常に複雑なAppleScriptアプリケーションを作成していますが、終了後に変数を保存する必要があります。ですから、実行中に変数を設定した場合は、変数を閉じてから、変数を再オープンしてください。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: 
+0

私のために動作します。使用しているOS X/macOSのバージョンは何ですか? – user3439894

+0

ハイシエラ。アプリケーションとしてエクスポートし、動作するかどうかを確認してください。 – Josh

+1

AppleScriptスクリプト/アプリケーションを実行中に別の値に設定した後にコンパイルしたり保存したりすると、元の値にリセットされます。この例では '欠損値'でしょうか?それはmacOS High Sierraでもそうであるように、私のために働く。 – user3439894

答えて

1

YES !!!!私はついにその答えを見つけました。スクリプトの停止位置からproperty aStatusItem : missing valueを削除する必要があります。これにより、関数間でaStatusItemが使用されなくなります。このため、quitを押すとメニューバーは削除されません。最後にその問題を解決するにはcurrent application's NSStatusBar's systemStatusBar()'s removeStatusItem:aStatusItemからtell me to quitに変更してください。これにより、アプリケーションが終了し、メニューバー項目が削除されます。

+0

これで、すべてのボタンが機能しないことがわかりました。 – Josh

+0

あなたは 'defaults read'と' defaults write'を使って見たいかもしれません。これにより、情報が.plistファイルに保存されます。私は他のユーザーと同意しますが、それはプロパティでも動作するはずです。 – ThrowBackDewd

+0

@ThrowBackDewd、 'defaults'は' do shell script' _command_で使用することができますが** AppleScript **の** System Events **は** Property List Suite **を持っており、おそらくデフォルトの方法と考えられますこのようなユースケースでplistファイルを扱うためのAppleScript。 'property' _variable_の使用に関しては、これが単なるAppleScriptの場合、問題はありませんが、スクリプトのコーディングが問題になっているか、このスクリプトがスクリプトの追加、FoundationとAppKitのフレームワークを使用しているためですそれ以上のものも問題になる可能性があります。 – user3439894

関連する問題