Applescript, how to mount remote server without error if unavailable?

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.


Using a macOS Catalina system as the file server with /Users/Shared/Videos as an SMB shared resource, and having done a bit of testing, found the following example AppleScript code to work well when testing using Script Editor from another system on the network in which the credentials have already been saved in its Keychain:

set ipAddress to "172.16.15.141"
set sharedResource to "Videos"

if workgroupAvailable(ipAddress) then
    mount volume "smb://" & ipAddress & "/" & sharedResource
end if

on workgroupAvailable(ipAddress)
    set workGroup to do shell script ¬
        "smbutil status " & ipAddress & " | awk '/Workgroup:/{print $2}'"
    if workGroup is equal to "" then
        return false
    else
        return true
    end if
end workgroupAvailable

The replies from the above example AppleScript code were:

When the shared resource was not available:

 tell current application
    do shell script "smbutil status 172.16.15.141 | awk '/Workgroup:/{print $2}'"
    --> ""
 end tell

When the shared resource was available:

tell current application
    do shell script "smbutil status 172.16.15.141 | awk '/Workgroup:/{print $2}'"
        --> "WORKGROUP"
end tell
tell application "Script Editor"
    mount volume "smb://172.16.15.141/Videos"
        --> file "Videos:"
end tell
Result:
file "Videos:"

What I found with using the smbutil status command over curl, which on my network required httpd running on the server, and ping, which could reply even if the shared resource wasn't available, was that smbutil status failed to return the Workgroup name if the shared resource wasn't available to be mounted and in turn no attempt to mount occurs if the Workgroup isn't available.

This example AppleScript code also did not cause any dialog boxes to appear at any point when the shared resource wasn't available.

Note that although testing was done with the rebooting of the file server and attempting to connect at various times during the reboot and the system actually being available, I was unable to test if the system had hung during the reboot, as it just has never hung during any reboots. As I know this is an unresolved issue with the rebooting of your server, you can give this a try if you so choose.

Obviously you can adapt the example AppleScript code to suite your specific needs.


Note: The example AppleScript code is just that and does not contain any error handling as may be appropriate. The onus is upon the user to add any error handling as may be appropriate, needed or wanted. Have a look at the try statement and error statement in the AppleScript Language Guide. See also, Working with Errors. Additionally, the use of the delay command may be necessary between events where appropriate, e.g. delay 0.5, with the value of the delay set appropriately.


The simplest and most portable way to do this would be via shell script. The reason being is that you avoid any of the GUI popup error dialogs completely which as you've found, breaks your script. It's better to handle them in the shell .

Below is a very short script bash/zsh script to connect to a particular server share. You can customize it by manipulating the variables in the top portion. The key here is to store the user's password in the Keychain so that it connects seamlessly on all subsequent connections.

The script is actually fairly short; you can remove all of the printf statements as I only included them so it would output each step to the console for your reference. It, like all other Unix commands exits with a 0 for a successful run and a 1 if there was a problem.

n=1                        # initialize iteration variable
max=3                      # max number of attempts
cflag=1                    # initialize connection flag / 0=true; 1=false
sleeptime=5                # number of seconds to sleep between retries
svr="sername.example"      # server name/IP address
loc="Network Share"        # directory/folder location
user="JVC"                 # username which to authenticate with 


#### Code is Below ###

while [ ${n} -le 3 ] && [ ${cflag} -gt 0 ]    #While not connected and attempts < max
do
if  [ ! $(ping -c 1 ${svr}) ]; then
  printf "Attempt ${n}: Device ${svr} not ready. Sleeping for retry...\n"
  sleep ${sleeptime}
  n=$(( ${n} + 1 ))               # increase count
else 
  cflag=0                         # Set connection flag to "true"
  printf "Device found.  Connecting to "
  printf "smb://${user}@${svr}/${loc}.\n"
  open "smb://${user}@${svr}"     # Connect and mount the share
fi 
done

# Print error message and exit with appropriate exit code.
if [ ${n} -eq ${max} ]; then
  printf "Could not connect to ${svr} in ${n} tries. Giving up."
  exit 1
fi
exit 0

To check if a server share is available and mount it if it is, a shell script would be much simpler than Applescript.

And to check the server, it is better to use smbutil rather than ping.

This example Bash script

  • defines server and share name
  • checks if the share is already mounted, and exits if it is.
  • then checks if the server is available and if yes, mounts it
#!/bin/bash

server=my_server
share=my_share

mount | grep -q "^//.*/$share on /Volumes/" && exit 0

if smbutil status $server 2>/dev/null | grep -q '^Server:'; then
    open smb://$server/$share
else
    exit 1
fi

If you need to provide credentials to mount the share, you can add them to the server variable like this:

server="Username:Password@my_server"