I attempted this question earlier today but finally realized I had completely mangled what I was trying to ask and also posted the wrong script code. This is attempt #2.
I have two Macs that reboot automatically early each morning in order to keep all of their various services functioning optimally. Each Mac needs to mount a volume on the other after they boot up. This is easy enough to do, except for a case where one machine hangs during the restart for any reason.
If one of the machines is unavailable when the other boots, then obviously its attempt to mount the unavailable machine will fail. The script is designed to run every minute or so in a loop for as long as the remote volume has not been mounted, since eventually it will come up. But when the mount fails, it produces an error dialog on the machine trying to perform the mount, which then breaks the loop and the script does not repeat.
Is there a way in AppleScript to attempt to mount a server, but without an error dialog if that server is unavailable in the moment?
I thought this was a main purpose for try blocks, but it hasn't made any difference.
Here is the script I currently run at boot time (it's an Automator application). The workflow is a Get Specified Server block, followed by a Run AppleScript block. Here is the AppleScript:
on run {input, parameters}
try
set server to (item 1 of input) -- this gets the server address from the Get Specified Servers block
end try
set vol to "Streaming"
tell application "Finder"
set isConnected to disk vol exists
end tell
repeat while isConnected = false -- as long as the volume is not present, try to mount it
try
tell application "Finder"
mount volume server & "/" & vol -- This produces the error dialog and halts the script, if the server is unavailable
end tell
end try
delay 2
tell application "Finder"
set isConnected to disk vol exists
end tell
if isConnected = false then
delay 60 -- if the volume still doesn't exist, wait a minute before trying again
end if
end repeat
return input
end run
Everything works perfectly as long as the remote volume is ready and accessible. But if not, the error dialog appears.
Edit: I found this thread and this quite complicated thread elsewhere that are attempting to address the same issue, but no simple, definitive solution seems to have been found.
curl
will fail even if the server is available. In that case you could substitute withping
e.g.ping -t 2 -c 1 192.168.2.102 2>/dev/null | awk '/0 packets received/{print $4}'
returns0
if it fails. That said, and mentioning something from another post about the server hanging in the reboot... Depending on where in the process it hangs,ping
could succeed without the actual File Sharing resource being available.mount volume
command is a part of Standard Additions in AppleScript not Finder and should not be wrapped in atell application "Finder"
block. In other words, Finder doesn't understand themount volume
command and it actually fails silently witherror number -10004
and then has to run the the command again.tell application "Finder"
block because AppleScript eats the error and then runs themount volume
command again in its proper context. It's a non-fatal error and improper coding as it needlessly usurps CPU cycles.