How to suppress / automatically dismiss error dialog in AppleScript

落花浮王杯 提交于 2019-12-06 07:49:44

问题


I have background process running as the logged on user that frequently tries to mount an AFP share to backup some data. If the share cannot be mounted this should just be ignored.

In my script (bash, actually) I am mounting the share via an AppleScript mount volume snippet. In contrast to the mount or mount_afp commands, this appears to be the only way to automatically authenticate the user at the respective server with credentials from the Kerberos ticket or the user's keychain. In particular, I do not want to have to store a password in the script:

try
    mount volume "afp://server/share"
on error errText number errNum
    log {errText, errNum}
end try

This works generally fine, but despite the try ... on error block, the 'mount volume' command always opens a dialog in case of an error:

I am looking for :

  • a way to suppress this dialog, or
  • a solution to automatically dismiss it (maybe involving some SystemEvents trickery?), or
  • an approach to teach mount, respectively mount_afp to use the credentials from the Kerberos ticket and the user's keychain without having to provide a password.

I have googled and tried for a couple of hours, but not yet found any solution.


回答1:


I have been fighting this problem on my mac mini media server for ages and believe i finally have a solution.

I have split it into two scripts:

the first one runs on idle (rather than a repeat loop) and calls a second script every 10 seconds that handles the drive mounting.

--------------------------------------------------------------------------------------
--"On Idle Launch Basic Drive Mounter.app"

on idle
    try
        --script loads on startup, so first we wait 5 seconds to ensure network
        delay 5
        --run the mounter script which is on the desktop
        run script file ":Users:localusername:Desktop:Basic Drive Mounter.app"

    on error errStr number errorNumber
        --listen for the apple quit command and quit
        if the errorNumber is equal to -128 then
            quit
            return 1
        --listen for the unknown error and ignore it
        else if the errorNumber is equal to -5014 then
            return 5
        else
            --all other errors are also ignored
            return 5
        end if
    end try
    --return with a wait of 5 seconds before next idle run
    return 5

end idle
--------------------------------------------------------------------------------------

the second script does the checking of the network, then tries to mount the volume using a shell mount. i originally used a finder "mount volume" and that codes exists as comments too, but I didn't like the dialog popping up on errors; even if only for a second, so i moved on to shell script.

--------------------------------------------------------------------------------------
--"Basic Drive Mounter.app"
try
    set IP_address to "xxx.xxx.xxx.xxx"

    set IP_Valid to true

    try
        do shell script ("ping -c 2 " & IP_address)
    on error
        set IP_Valid to false
    end try

    if IP_Valid then
        tell application "Finder"
            if disk "work" exists then
            else
                -->>shell script version
                try
                    do shell script "mkdir /Volumes/work"
                end try
                do shell script "mount_afp afp://xxx.xxx.xxx.xxx/work /Volumes/work/"
                --<<shell script version
                -->>finder mount volume version
                --with timeout of 1 second
                --  mount volume "afp://xxx.xxx.xxx.xxx/work"
                --end timeout
                --<<finder mount volume version
            end if
        end tell
    end if
on error
    -->>finder mount volume version
    --on error finder returns an error dialog which needs to be closed to go back and  retry
    --tell application "System Events"
    --  keystroke return
    --end tell
    --<<finder mount volume version
    return 0
end try    
--------------------------------------------------------------------------------------

not all of this is my own code, so many thanks goes out to the applescript community and google - remember to pay it forward




回答2:


Apologies for the crappy answer. I think if you try something like this you won't be bothered with dialogs (but you can still have your script respond to errors).

(below is a simple version. Documentation for mount_afp with username/password is here: http://developer.apple.com/library/mac/documentation/Darwin/Reference/Manpages/man8/mount_afp.8.html )

    try
        alias (POSIX file "/Volumes/yourMountedVolumeName")--hacky check for mount point
    on error
        --does not exist, so make dir
        do shell script "mkdir /Volumes/yourMountedVolumeName"
    end try

    --now use do shell to mount
    try
        do shell script "mount_afp 'afp://yourServer/yourMountedVolumeName/' /Volumes/yourMountedVolumeName"

    on error errText number errnum
        log {errText, errnum}
    end try

[insufficient answer below]

Some of this might be obvious, but you'll need to

  1. watch for that dialog (or, obviously, a successful mount), and
  2. if it comes up, dismiss it.

I believe this will work to kill the dialog w/o resorting to System Events ... in terminal or shell:

killall NetAuthAgent

or via AppleScript:

do shell script "killall NetAuthAgent"

Of course, you have to be careful not to kill it during the authentication process.




回答3:


To mount the drives, make an apple script like this:

tell application "Finder"

    try

        mount volume "afp://192.168.0.0/test"

    end try

end tell

Then, to close the warning message that comes up, use another script like this:

delay 2
tell application "System Events"
    click UI element "OK" of window 1 of application process "NetAuthAgent"
end tell

The delay just gives the message box a chance to appear, but you might not need it.

If you run this last script from the terminal using osacompile then osascript, you will need to give accessibility access to the terminal.



来源:https://stackoverflow.com/questions/22640297/how-to-suppress-automatically-dismiss-error-dialog-in-applescript

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!