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

问题描述 投票:0回答:1

好的,所以我已经有几天了,而且我只发现了一个类似的issue。不幸的是,它没有完全解决。

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

该脚本按预期工作,并使用try块进行错误处理,但在mountVolume()处理程序中,我获得了当共享不可用时Daniel从另一篇文章中获取的相同错误对话框。例如外部驱动器已从远程计算机上拔下或尚未完成连接。

一旦我解除OS X的错误对话框,我的错误对话框出现了。我可以摆脱我的对话但事情是,对于每一个共享(现在4)我试图挂载失败,我得到一个单独的OS X错误对话框,我不得不解雇。

如果我可以让脚本禁止这些框,我可以在脚本中使用我自己的一个框来解决所有错误。

如果我不能,那么我想要一种方法来检查REMOTE计算机上是否存在共享,然后我尝试使用安装量,从而共同环绕错误。

谢谢任何想法将不胜感激。

这是我的代码:

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-handling applescript remote-server
1个回答
0
投票

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

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

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

© www.soinside.com 2019 - 2024. All rights reserved.