
這是我在這個網站上的第一篇文章,如果這是重複的文章,請原諒我,但我找不到類似的內容。
我在 2011 年初使用的是 Macbook Pro 13,我安裝了 SSD 並將 HDD 移至 optibay,全新安裝了 Yosemite,我的第二個 HDD 是 1TB,用於存儲我的工作和數據,然後進行備份。到家了。
我不需要一直安裝硬碟,我需要節省能源並將其隱藏起來,所以我將其從聚光燈搜尋中刪除,執行“sudo pmset -a disksleep 1”並創建了兩個applescript,一個要啟動登入時直接卸載硬碟,第二個是透過我用karabiner 修改的彈出鍵啟動的(以前稱為KeyRemap4MacBook)
第二個腳本啟動一個對話框,要求輸入密碼,然後詢問我是否要訪問硬碟,如果是,則將安裝硬碟,如果否,將卸載硬碟
問題是,我注意到,如果我在掛載 HDD 時關閉 MacBook,則 HDD 的磁碟識別碼會從 disk2 更改為 Disk1,並且兩個腳本都會嘗試卸載 SSD,因此我需要手動彈出hdd 並重新啟動,一切恢復正常。
我想要做的是修改透過彈出鍵啟動的腳本,使其啟動第一個對話框,就像關閉對話框一樣,刪除取消按鈕並添加一個名為“擴展”的按鈕(這是硬碟)。
我是 applescript 的新手,所以這就是我想做的: 例子
如果按下重新啟動,則卸載硬碟並重新啟動
如果按下睡眠,則卸載硬碟並睡眠
如果按下關機,則卸載硬碟並關機
如果按下擴展,則啟動舊腳本
這是我的舊腳本,新腳本應該出現在它之前
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”安裝和卸載硬碟是一個壞主意,因為我發現無論上次關機時是否卸載硬碟,磁碟標識符都會隨機變化,因此腳本我試圖添加是無用的。
我找到的解決方案顯然是UUID,一開始我無法讓它正常工作,但經過多次嘗試後,一切正常。
如何:啟動磁碟工具,在左側您將看到每個磁碟及其分區選擇要為其創建腳本的分區,然後單擊左上角的“資訊”,將彈出一個資訊窗口,請確保這是正確的分區,在資訊清單中,您會發現(通用唯一識別碼:此處是字母和數字束)這就是 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 指令是為了避免在磁碟已卸載時顯示訊息。
就是這樣,簡單又準確,希望這對未來有幫助