error-handling - 如何在 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
解决方案
错误对话框是在 AppleScript 之外生成的,因此您不能使用try
语句来捕获它。我知道避免该对话框的唯一方法是创建挂载点并使用mount
shell 脚本而不是mount volume
命令自己挂载卷,例如:
do shell script "mount -t smbfs //169.254.0.0/SHARENAME /path/to/sharepoint“
如果有错误,try
语句仍然会捕获它,只是没有外部对话框。
推荐阅读
- javascript - Chartjs - 将图例更改为带有圆点样式的线?
- jboss - 如何从 Wildfly 的连接池中手动删除数据源连接?
- pandas - 如何处理熊猫中的同名列
- prolog - Prolog:为什么这个谓词没有失败,一个空列表作为参数传递
- python - Gensim LDA:如何获取每个主题中单词的数量(频率)?
- git - git hook 批量取消暂存路径中的所有文件
- mysql - MySQL:如何为 original_id 添加一列?
- c++ - NtQueryInformationProcess 返回 STATUS_ACCESS_VIOLATION
- html - 如果每个项目都有多个需要水平对齐的列,您将如何设置树视图的样式?(网络)
- amazon-web-services - 雅典娜分区限制以及如何最好地解决我所描述的问题