OS X Applescript 检查驱动器是否已安装,如果未安装则安装 [英] OS X Applescript to Check if Drive Mounted and Mount It If Not

查看:39
本文介绍了OS X Applescript 检查驱动器是否已安装,如果未安装则安装的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I am trying to write an AppleScript that will check if a network drive (in this case, my Time Capsule) is mounted and, if not, mount it. I've figured out how to mount the Time Capsule, but I am at a loss over how to have the script check whether it is mounted first and just exit if it is, or mount it if not.

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


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


Update I thought you want to unmount the disk when mounted but that was wrong :) here a shorter version:

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

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

…or, if you want the disk name in the dialog:

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

这篇关于OS X Applescript 检查驱动器是否已安装,如果未安装则安装的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆