ドライブをマウントおよびアンマウントするAppleScript

ドライブをマウントおよびアンマウントするAppleScript

これはこのサイトへの私の最初の投稿です。重複投稿になってしまったら申し訳ありませんが、似たようなものは見つかりませんでした。

私は2011年初頭のMacbook Pro 13を使用しています。SSDをインストールし、HDDをOptiBayに移動し、Yosemiteをクリーンインストールしました。2台目のHDDは1TBで、家に帰ってからバックアップを取る前に、仕事やデータをそこに保存しています。

HDD を常にマウントしておく必要はなく、電力を節約して隠しておく必要があるため、スポットライト検索から削除し、「sudo pmset -a disksleep 1」を実行して 2 つの AppleScript を作成しました。1 つはログイン時に起動して HDD を直接アンマウントするもので、もう 1 つは karabiner (以前は KeyRemap4MacBook と呼ばれていました) で変更したイジェクト キーを介して起動するものです。

2 番目のスクリプトは、パスワードを要求するダイアログを起動し、HDD にアクセスするかどうかを尋ねます。アクセスする場合は、HDD がマウントされ、アクセスしない場合は、アンマウントされます。

問題は、HDD がマウントされている状態で MacBook をシャットダウンすると、HDD のディスク識別子が disk2 から disk1 に変更され、両方のスクリプトが SSD をアンマウントしようとするため、すべてが正常に戻るように HDD を手動で取り出して再起動する必要があることです。

私がやりたいのは、イジェクト キーを介して起動するスクリプトを変更して、シャットダウン ダイアログとまったく同じように最初のダイアログを起動し、キャンセル ボタンを削除して、「拡張」(HDD の名前) というボタンを追加することです。

私はAppleScriptの初心者です。私がやりたいことは次のとおりです:

再起動が押された場合は、HDDをアンマウントして再起動します。
スリープが押された場合は、HDDをアンマウントしてスリープします
。シャットダウンが押された場合は、HDDをアンマウントしてシャットダウンします。
拡張が押された場合は、古いスクリプトを起動します。

これは私の古いスクリプトです。新しいスクリプトはその直前に来るはずです

     set my_password to display dialog ¬
    "Allow access to Expansion" with title ¬
    "Expansion" with icon caution ¬
    default answer ¬
    "" buttons {"Cancel", "OK"} default button 2 ¬
    giving up after 295 ¬
    with hidden answer
if text returned of my_password is "password here" then

    set answer to the button returned of (display dialog "Allow access to Expansion?" with icon caution buttons {"Yes", "No"})

    if answer = "Yes" then
        do shell script "diskutil mountDisk disk2"
        tell application "Notifications Scripting"


            display notification "Expansion" subtitle "is now mounted" sound name "Blow"

        end tell
    else if answer = "No" then
        try

            do shell script "hdiutil eject disk2"

        on error

            tell application "System Events"
                set termOpen to count (processes whose name is "Terminal")
                set amOpen to count (processes whose name is "Activity Monitor")
            end tell


            tell application "Terminal"
                activate
                set newTab to do script "lsof /Volumes/'HFS HD'"
            end tell

            tell application "Activity Monitor"
                activate
            end tell

            delay 3

            set question to display dialog "Kill running?" buttons {"Yes", "No"} default button 2
            set answer to button returned of question

            if answer is equal to "Yes" then
                do shell script "lsof -P | grep '/Volumes/HFS HD'  | awk '{print $2}' | xargs kill -9"
                do shell script "hdiutil eject disk2"
            end if


            tell application "Activity Monitor"
                if amOpen is 0 then
                    quit
                end if
            end tell

            tell application "Terminal"
                if termOpen is 0 then
                    quit
                else
                    close (first window whose selected tab is newTab) saving no
                end if
            end tell



        end try
        tell application "Notifications Scripting"


            display notification "Expansion" subtitle "is now unmounted" sound name "Blow"

        end tell
    end if
else
    tell application "Notifications Scripting"


        display notification "A Goomba killed Mario!" subtitle "Next time, try jumping on it" sound name "Sosumi"

    end tell
    quit

end if

ご協力ありがとうございます。長文になってしまい申し訳ありません。

答え1

そこで、3時間グーグルで検索して試行錯誤した結果、解決策を見つけました。

「diskutil mountDisk disk2」と「hdiutil eject disk2」を使用して HDD をマウントおよびアンマウントするのは悪い考えでした。前回のシャットダウン時に HDD がアンマウントされたかどうかに関係なく、ディスク識別子がランダムに変化し続けることがわかったため、追加しようとしていたスクリプトは役に立たなかったからです。

私が見つけた解決策は明らかに UUID でした。最初は正しく動作しませんでしたが、何度か試した後、すべて正常に動作するようになりました。

方法: ディスクユーティリティを起動すると、左側に各ディスクとそのパーティションが表示されます。スクリプトを作成するパーティションを選択し、左上の情報をクリックすると、情報ウィンドウがポップアップ表示されるので、それが正しいパーティションであることを確認します。情報リストに (Universal Unique Identifier: 一連の文字と数字) が表示されます。これが UUID です。

私の最終的なスクリプト:

set answer to the button returned of (display dialog “Mount the second HDD?” with icon caution buttons {"Yes", "No"})

if answer = "Yes" then
    do shell script "diskutil mount *YOUR UUID WITHOUT THE ASTERISK* ”

else if answer = "No" then
    try

        do shell script "diskutil unmount *YOUR UUID WITHOUT THE ASTERISK*"

    end try
end if

Try コマンドは、ディスクがすでにアンマウントされている場合にメッセージを表示しないようにするためのものです。

以上です。シンプルで正確です。今後役立つことを願っています

関連情報