使用 AppleScript 安装和卸载驱动器

使用 AppleScript 安装和卸载驱动器

这是我在此网站上的第一篇帖子,所以如果这是重复的帖子,请原谅,但我找不到任何类似的内容。

我在 2011 年初使用 Macbook Pro 13,我安装了 ssd 并将 hdd 移到 optibay,全新安装了 Yosemite,我的第二个 hdd 是 1TB,我将我的工作和数据存储在其中,然后在我回家后进行备份。

我不需要一直挂载硬盘,而且我需要节省能源并保持它隐藏,所以我把它从聚光灯搜索中删除,执行“sudo pmset -a disksleep 1”并创建了两个 applescript,一个在登录时启动以直接卸载硬盘,第二个通过我用 karabiner 修改的弹出键启动(以前称为 KeyRemap4MacBook)

第二个脚本启动一个对话框,要求输入密码,然后询问我是否要访问硬盘,如果是,则将安装硬盘,如果不是,则将卸载硬盘

问题是,我注意到,如果我在安装了硬盘驱动器的情况下关闭我的 MacBook,硬盘驱动器的磁盘标识符会从磁盘 2 更改为磁盘 1,并且两个脚本都会尝试卸载固态硬盘驱动器,因此我需要手动弹出硬盘驱动器并重新启动,这样一切就会恢复正常。

我想要做的是修改通过弹出键启动的脚本,使其启动第一个对话框,与关机对话框完全一样,删除取消按钮并添加一个名为“扩展”的按钮(这是硬盘的名称)。

我是 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

谢谢你的帮助,如果太长不看,请见谅 :p

答案1

因此,经过 3 个小时的谷歌搜索和尝试,我找到了解决方案。

使用“diskutil mountDisk disk2”和“hdiutil jet 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 是为了避免在磁盘已卸载时显示消息。

就是这样,简单而准确,希望这对未来有所帮助

相关内容