首页 > 解决方案 > 如何在 applescript 中抑制 OS X 错误对话框或如何在使用挂载卷命令之前确保远程共享已连接和共享

问题描述

好的,我已经在这方面待了几天,我只发现了一个类似的问题。不幸的是,它没有完全解决。

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

该脚本按预期工作,并使用 try 块处理错误,但在 mountVolume() 处理程序中,当共享不可用时,我得到了与其他帖子中的 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

标签: error-handlingapplescriptremote-server

解决方案


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

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

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


推荐阅读