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

时间:2015-03-29 18:11:21

标签: macos applescript osx-yosemite

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

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

告诉

3 个答案:

答案 0 :(得分:4)

这是工作吗?

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

答案 1 :(得分:2)

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

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

答案 2 :(得分:1)

在使用Apple脚本在Finder上使用mount命令时,是否已挂载共享似乎无关紧要。无论如何,只需安装它,Finder正确处理它(它不会再次安装它,也不会大声抱怨)。