OS X Applescript 检查是否安装了驱动器,如果没有则安装它

OS X Applescript to Check if Drive Mounted and Mount It If Not

我正在尝试编写一个 AppleScript 来检查是否安装了网络驱动器(在本例中为我的 Time Capsule),如果没有,则安装它。我已经弄清楚如何安装 Time Capsule,但我不知道如何让脚本检查它是否首先安装,如果是则退出,如果不是则安装。

tell application "Finder"
mount volume "afp://AirPort%20Time%20Capsule._afpovertcp._tcp.local"

结束讲述

这是工作吗?

set mountedDiskName to "AirPort Time Capsule"
set diskIsMounted to false

tell application "System Events" to set diskNames to name of every disk
if mountedDiskName is in diskNames then
    set diskIsMounted to true
end if

if diskIsMounted then

    log "Disk Found, unmounting now..."
    do shell script "diskutil unmountDisk" & space & quoted form of mountedDiskName

else

    log "Disk Not Found, mounting now…"
    mount volume "afp://AirPort%20Time%20Capsule._afpovertcp._tcp.local"

end if


更新 我以为你想在挂载时卸载磁盘,但那是错误的 :) 这里有一个较短的版本:

tell application "System Events" to set diskNames to name of every disk
if "AirPort Time Capsule" is in diskNames then
    display dialog "Disk already mounted" buttons {"OK"} default button 1 with icon 1
else
    mount volume "afp://AirPort%20Time%20Capsule._afpovertcp._tcp.local"
end if

…或者,如果您想要对话框中的磁盘名称:

set diskName to "AirPort Time Capsule"
tell application "System Events" to set diskNames to name of every disk
if diskName is in diskNames then
    display dialog quoted form of diskName & " is already mounted." & return buttons {"OK"} default button 1
else
    mount volume "afp://AirPort%20Time%20Capsule._afpovertcp._tcp.local"
end if

您不必检查所有磁盘的列表。你可以直接询问你要的磁盘是否存在

tell application "Finder"
    if not (disk "Airport Time Capsule" exists) then
        mount volume "afp://AirPort%20Time%20Capsule._afpovertcp._tcp.local"
    end if
end tell

在 Finder 上使用 apple script 的 mount 命令时,似乎共享是否已经挂载并不重要。无论如何都安装它,Finder 会正确处理这个问题(它不会第二次安装它,也不会大声抱怨)。