Applescript, how to mount remote server without error if unavailable

applescriptautomatormountsmb

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.

Best Answer

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.