2

アプリケーションを起動する前にドライブをマウントするスクリプトを作成しました。ただし、アプリケーションは、ドライブが正常にマウントされた場合にのみ起動する必要があります。

ドライブをマウントしてアプリケーションを起動するための正しいコードがありますが、操作が終了した後でのみマウントが成功したかどうかを確認する必要があります。現時点では、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
4

1 に答える 1

1

You could check if the mount was successful with a cycle like this:

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

You could also add a condition to exit the loop after a certain number of iterations:

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

EDIT:

The script must be modified to be compatible with latest macOS versions:

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

Thanks to a walker for the suggestion.

于 2013-01-10T21:03:34.683 に答える