如何在 applescript 中抑制 OS X 错误对话框或如何在使用 mount volume 命令之前确保远程共享已连接和共享

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

好的,我已经研究了几天了,我只找到了一个类似的 issue。不幸的是,它没有完全解决。

我有一个脚本可以检查网络连接、检查远程计算机并安装卷。

该脚本按预期工作,并使用 try 块处理错误,但在 mountVolume() 处理程序中,我收到与共享不可用时其他 post 的 Daniel 收到的错误对话框相同的错误对话框。例如外部驱动器已从远程计算机拔出或尚未完成连接。

一旦我关闭 OS X 的错误对话框,我的错误对话框就会出现。我可以摆脱我的对话框,但问题是对于我尝试安装失败的每个共享(现在 4 个),我得到一个单独的 OS X 错误对话框,我必须关闭它。

如果我可以获得抑制这些框的脚本,我可以在脚本中使用我自己的一个框来处理所有错误。

如果我不能,那么我想要一种方法来检查远程计算机上是否存在共享,然后再尝试使用装载卷,从而一起绕过错误。

谢谢,如有任何想法,我们将不胜感激。

这是我的代码:

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

错误对话框是在 AppleScript 之外生成的,因此您不能用 try 语句来捕获它。我知道避免该对话框的唯一方法是创建安装点并使用 mount shell 脚本而不是 mount volume 命令自行安装卷,例如:

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

如果出现错误,try 语句仍会捕获它,只是没有外部对话框。