AppleScript安装驱动器,一旦完成,报告是否成功

时间:2013-01-10 20:39:50

标签: applescript

我已经编写了一个脚本,可以在启动应用程序之前安装驱动器。但是,只有在驱动器安装成功后才能启动应用程序。

我有正确的代码来安装驱动器并启动应用程序,但是我需要在操作完成后检查安装是否成功。目前看来if语句在mount之后立即运行。

# Get wireless network SSID
set SSID to do shell script "/System/Library/PrivateFrameworks/Apple80211.framework/Resources/airport -I | awk '/ SSID: / {print $2}'"

# Test if we are connected to the right network(s)
# We can test for multiple networks if you’re always on the move, like me!
# if SSID is "YOUR_SSID_ONE" or SSID is "YOUR_SSID_TWO" then
if SSID is "virginmedia7912273" or SSID is "virginmedia5097309" then
    tell application "Finder"
        # Mount your disk
        mount volume "afp://nas/iTunes/"
        # Check that the disk successfully mounted
        if exists disk "iTunes" then
            # If the disk successfully mounted, launch iTunes
            tell application "iTunes"
                # Launch launches, activate brings window to focus (?)
                launch
                activate
            end tell
        else
            # If the disk didn’t mount, throw an error.
            display dialog "Unable to connect to iTunes storage device."
        end if
    end tell
else
    # If we are not connected to the right network(s), throw an error.
    display dialog "You are not connected to your home network."
end if

1 个答案:

答案 0 :(得分:1)

您可以通过以下循环检查装载是否成功:

repeat until name of every disk contains "iTunes"
   delay 1
end repeat

您还可以添加条件以在一定次数的迭代后退出循环:

set i to 0
repeat until (name of every disk contains "iTunes" or i is greater than max_number_of_iteration)
   delay 1
   set i to i + 1
end repeat

修改

必须修改脚本以与最新的macOS版本兼容:

tell application "System Events" to set diskNames to name of every disk 
set i to 0
set max_number_of_iteration to 5
repeat until (diskNames contains "iTunes" or i is greater than max_number_of_iteration)
    tell application "System Events" to set diskNames to name of every disk 
    delay 1
    set i to i + 1
end repeat

感谢a walker提出建议。