OS X Applescript 检查驱动器是否已安装,如果未安装则安装

Fbx*_*eve 5 macos applescript osx-yosemite

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

tell application "Finder"
mount volume "afp://AirPort%20Time%20Capsule._afpovertcp._tcp.local"
Run Code Online (Sandbox Code Playgroud)

结束告诉

小智 7

那是工作吗?

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
Run Code Online (Sandbox Code Playgroud)


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

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
Run Code Online (Sandbox Code Playgroud)

...或者,如果您想在对话框中显示磁盘名称:

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
Run Code Online (Sandbox Code Playgroud)