Search code examples
error-handlingapplescriptremote-server

How to suppress an OS X error dialog in applescript or how to make sure a remote share is connected and sharing before using a mount volume command


Okay so I've been a few days on this, and I've only found one similar issue. Unfortunately it wasn't resolved fully.

I have a script that checks for a network connection, checks for the remote computer, and mounts the volumes.

The script works as expected and is error handled with a try blocks but in the mountVolume() handler I get the same error dialog box that Daniel from the other post gets when the share is unavailable. e.g. external drive is unplugged from remote computer or hasn't finished connecting yet.

Once I dismiss OS X's error dialog(s), my error dialog comes up. I could just get rid of my dialog but the thing is that for every share (4 now) I've tried to mount that fails, I get a separate OS X error dialog that I have to dismiss.

If I can get the script to suppress these boxes I can use one box of my own in the script for all errors.

If I can't then I would like a way to check if the share exists on the REMOTE computer before I try to use mount volume thus circumnavigating the error all together.

Thanks any ideas would be appreciated.

Here is my code:

global userName
global userPass
global ipAddress

set ipAddress to "###.###.###.###"

set userName to short user name of (system info)
set userPass to do shell script ("security find-internet-password -a " & userName & " -s " & ipAddress & " -g")

on FileExists(theFile)
    tell application "System Events"
        if exists file theFile then
            return true
        else
            return false
        end if
    end tell
end FileExists

on FolderExists(theFolder)
    tell application "System Events"
        if exists folder theFolder then
            return true
        else
            return false
        end if
    end tell
end FolderExists

on doCleanUp()
    if FolderExists("/Volumes/SHARENAME") then
        tell application "Finder" to eject disk "SHARENAME"
    end if

    set checkPath to ((path to home folder as text) & "SHARENAME")
    if FileExists(checkPath) then
        do shell script ("rm ~/SHARENAME")
    end if
end doCleanUp

on checkNet()
    try
        do shell script ("nc -z " & ipAddress & " 445")
        return true
    on error
        return false
    end try
end checkNet

on mountVolume()
    try
        mount volume "smb://" & ipAddress & "/SHARENAME"
        return true
    on error errText number errNum
        log {errText, errNum}
        return false
    end try
end mountVolume

on makeAlias()
    if FolderExists("/Volumes/SHARENAME") then
        set checkPath to ((path to home folder as text) & "SHARENAME")
        tell application "Finder"
            if not (exists file checkPath) then
                make new alias to disk "SHARENAME" at path to home folder
            end if
        end tell
    end if  
end makeAlias

set tryAgain to 0
set ipValid to false
set doRetry to true

doCleanUp()

repeat while doRetry
    repeat 3 times
        if not ipValid then
            set ipValid to checkNet()
        end if
    end repeat

    if ipValid then
        set volMounted to mountVolume()
        if volMounted then
            set aliasCreated to makeAlias()
            if aliasCreated then
                return
            else
                set notificationMessage to "Could not create alias."
                display alert "An error has occurred." message notificationMessage as critical
                return
            end if
        else
            set notificationMessage to "Could not mount remote volume."
            display alert "An error has occurred." message notificationMessage as critical          
            return
        end if
    else
        set retryCheck to display alert "Can't connect. Do you want to retry?" buttons {"Yes", "No"} default button 1
        set doRetry to button returned of retryCheck as boolean

        if not doRetry then
            doCleanUp()
            set notificationMessage to "Could not connect to Local Area Network."
            display alert "An error has occurred." message notificationMessage as critical
        end if
    end if
end repeat

Solution

  • The error dialog is being generated outside of AppleScript, so you can’t trap it with a try statement. The only way I know of to avoid the dialog is to create the mount point and mount the volume yourself with the mount shell script instead of the mount volume command, for example:

    do shell script "mount -t smbfs //169.254.0.0/SHARENAME /path/to/sharepoint“
    

    If there is an error the try statement will still catch it, just without the external dialog.