0

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
Adam
  • 3
  • 3
  • Unrelated but remove all occurrences of `as boolean` after `true` and `false`. Both values **are** boolean. And `0` **is** `integer`. – vadian Mar 04 '19 at 19:51
  • @vadian Thanks for your help, I actually thoght that was the case but didn't want to take any chances. I put those in while troubleshooting another issue a while back, I guess I forgot to remove. :) – Adam Mar 04 '19 at 19:58
  • @vadian I edited the code and removed the extra code. – Adam Mar 04 '19 at 20:03

1 Answers1

0

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.

red_menace
  • 3,162
  • 2
  • 10
  • 18
  • Thanks for answering. That does make sense. I guess that's why there can be no resolution to that issue. I did have a older version that used that method, but this method has the advantages of not putting the password in the script and can be used for multiple users. I guess that leaves me with finding a way of verifying the remote computer is sharing a volume over the network before trying to mount it. I've been looking into some way of getting a list of shares from the remote computer but haven't found the right solution yet. – Adam Mar 04 '19 at 21:15
  • You can use the keychain for the password without it being in the script, just don't put it into a property or global variable. – red_menace Mar 04 '19 at 21:41