드라이브를 마운트 및 마운트 해제하는 AppleScript

드라이브를 마운트 및 마운트 해제하는 AppleScript

이것은 이 사이트의 첫 번째 게시물이므로 이중 게시물인 경우 죄송하지만 유사한 게시물을 찾을 수 없습니다.

저는 2011년 초 Macbook Pro 13을 사용하고 있으며 SSD를 설치하고 HDD를 optibay로 옮겼으며 Yosemite를 새로 설치했습니다. 두 번째 HDD는 1TB로 작업과 데이터를 저장한 후 백업을 할 때 사용합니다. 나 집에 있어.

HDD를 항상 마운트할 필요가 없고 에너지를 절약하고 숨겨야 하기 때문에 스포트라이트 검색에서 제거하고 "sudo pmset -a disksleep 1"을 수행하고 두 개의 applescript를 생성했습니다. 하나는 실행됩니다. 로그인 시 HDD를 직접 마운트 해제하고 두 번째 HDD는 Karabiner(이전에는 KeyRemap4MacBook으로 알려짐)로 수정한 꺼내기 키를 통해 실행됩니다.

두 번째 스크립트는 비밀번호를 묻는 대화 상자를 시작한 다음 HDD에 액세스할지 묻습니다. 그렇다면 HDD가 마운트되고, 아니요이면 마운트 해제됩니다.

문제는 HDD가 마운트된 동안 Macbook을 종료하면 HDD의 디스크 식별자가 disk2에서 disk1로 변경되고 두 스크립트 모두 SSD를 마운트 해제하려고 시도하므로 SSD를 수동으로 꺼내야 한다는 것입니다. hdd를 추가하고 다시 시작하면 모든 것이 정상으로 돌아갑니다.

내가 하고 싶은 것은 꺼내기 키를 통해 실행되는 스크립트를 수정하여 종료 대화 상자와 똑같이 첫 번째 대화 상자를 시작하도록 하고, 취소 버튼을 제거하고 "확장"이라는 버튼을 추가하는 것입니다. HDD).

저는 applescript를 사용하는 초보자입니다. 따라서 제가 하고 싶은 작업은 다음과 같습니다.

Restart를 누른 경우 HDD를 마운트 해제하고 다시 시작
Sleep을 누른 경우 HDD를 마운트 해제하고 Sleep
Shutdown을 누른 경우 HDD를 마운트 해제하고 종료
Expansion을 누른 경우 이전 스크립트를 시작합니다.

여기 내 이전 스크립트가 있습니다. 새 스크립트는 그 바로 앞에 와야 합니다.

     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

도움을 주셔서 감사합니다. TLDR이라면 죄송합니다 :p

답변1

그래서 3시간 동안의 구글링 끝에 해결책을 찾았습니다.

"diskutil mountDisk disk2" 및 "hdiutilject disk2"를 사용하여 hdd를 마운트 및 마운트 해제하는 것은 나쁜 아이디어였습니다. 마지막 종료에서 hdd가 마운트 해제되었는지 여부에 관계없이 디스크 식별자가 무작위로 계속 변경된다는 것을 알았기 때문에 스크립트는 추가하려고 했는데 소용없더군요.

내가 찾은 해결책은 분명히 UUID였습니다. 처음에는 올바르게 작동하도록 할 수 없었지만 여러 번 시도한 후에는 모든 것이 잘 작동합니다.

방법: 디스크 유틸리티를 실행하면 왼쪽에 각 디스크와 해당 파티션이 표시됩니다. 스크립트를 생성하려는 파티션을 선택하고 왼쪽 상단에서 정보를 클릭하면 정보 창이 팝업되어 올바른 파티션인지 확인합니다. , 정보 목록에서 ( Universal Unique Identifier : Bunch-of-letters-and-numbers-here )가 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 명령은 디스크가 이미 마운트 해제된 경우 메시지 표시를 방지하는 것입니다.

그게 다입니다. 간단하고 정확합니다. 앞으로 도움이 되길 바랍니다.

관련 정보