AppleScript:为不同的错误号

时间:2016-05-13 08:53:46

标签: macos applescript

所以我正在安装不同的smb共享,并希望根据错误编号显示相关文本,并且对我未定义的数字不执行任何操作。

我正在寻找的东西:

on error
if error_number - 5014 then
            display dialog "Can't connect to" & chosen as text
        else if error_number - 5016 then
            display dialog "Can't connect to" & chosen as text
        else if error_number then --do nothing
end if

现在我从MacOSX获得这个弹出窗口:

“连接服务器时出现问题.....”

然后我定义的错误消息。如何摆脱双重弹出?我更喜欢收到通知,而不是来自操作系统的通知。

1 个答案:

答案 0 :(得分:1)

这非常简单,因为error提供了可选参数number

on error errorMessage number errorNumber
    if errorNumber = -36 then
        display dialog "Can't connect to" & chosen as text
    else if errorNumber = 5016 then
        display dialog "Catch error 5016"
    end if -- ignore all other errors   
end try

修改

要安装没有错误的卷对话框,请使用shell命令mount_afpmount_smbfs

这是AFP的示例。 它会检查卷是否已安装。如果不是,则“手动”创建安装点并安装卷。失败时,将删除挂载点。 SMB版本可以正常运行。

property server : "myServer.local"
property serverVolume : "Server"
property user : "user"
property pass : "pass"

set isMounted to serverVolume is in (do shell script "/bin/ls /Volumes") or mountAFP(user, pass, server, serverVolume)

if isMounted then
    -- do something
end if

on mountAFP(user_name, pass_word, thehost, theVolume)
    set theAddress to quoted form of ("afp://" & user_name & ":" & pass_word & "@" & thehost & "/" & theVolume)
    set mountpoint to quoted form of ("/Volumes/" & theVolume)
    try
        do shell script "/bin/mkdir " & mountpoint & "; /sbin/mount_afp " & theAddress & space & mountpoint
        return true
    on error e
        log e
        do shell script "/bin/rm -r " & mountpoint
        return false
    end try
end mountAFP