如何在AppleScript中禁止/自动关闭错误对话框 [英] How to suppress / automatically dismiss error dialog in AppleScript
问题描述
我以登录用户的身份运行后台进程,该进程经常尝试挂载AFP共享以备份某些数据.如果无法挂载共享,则应将其忽略.
I have background process running as the logged on user that frequently tries to mount an AFP share to backup some data. If the share cannot be mounted this should just be ignored.
在我的脚本中(实际上是重击),我正在通过AppleScript mount volume
代码段安装共享.与mount
或mount_afp
命令相反,这似乎是使用Kerberos票证或用户钥匙串中的凭据自动验证相应服务器上的用户的唯一方法.特别是,我不不想在脚本中存储密码:
In my script (bash, actually) I am mounting the share via an AppleScript mount volume
snippet. In contrast to the mount
or mount_afp
commands, this appears to be the only way to automatically authenticate the user at the respective server with credentials from the Kerberos ticket or the user's keychain. In particular, I do not want to have to store a password in the script:
try
mount volume "afp://server/share"
on error errText number errNum
log {errText, errNum}
end try
这通常可以正常工作,但是尽管有try ... on error
块,但是在发生错误的情况下,"mount volume"命令始终会打开一个对话框:
This works generally fine, but despite the try ... on error
block, the 'mount volume' command always opens a dialog in case of an error:
我正在寻找:
- 隐藏此对话框的方法,或
- 自动解散它的解决方案(可能涉及一些SystemEvents的诡计?),或者
- 一种教导
mount
和mount_afp
分别使用Kerberos票证和用户钥匙串中的凭据而无需提供密码的方法.
- a way to suppress this dialog, or
- a solution to automatically dismiss it (maybe involving some SystemEvents trickery?), or
- an approach to teach
mount
, respectivelymount_afp
to use the credentials from the Kerberos ticket and the user's keychain without having to provide a password.
我已经用Google搜索并尝试了几个小时,但尚未找到任何解决方案.
I have googled and tried for a couple of hours, but not yet found any solution.
推荐答案
多年来,我一直在Mac mini媒体服务器上解决此问题,并相信我终于有解决方案.
I have been fighting this problem on my mac mini media server for ages and believe i finally have a solution.
我将其分为两个脚本:
第一个脚本在空闲(而不是重复循环)上运行,并每隔10秒调用第二个脚本来处理驱动器的安装.
the first one runs on idle (rather than a repeat loop) and calls a second script every 10 seconds that handles the drive mounting.
--------------------------------------------------------------------------------------
--"On Idle Launch Basic Drive Mounter.app"
on idle
try
--script loads on startup, so first we wait 5 seconds to ensure network
delay 5
--run the mounter script which is on the desktop
run script file ":Users:localusername:Desktop:Basic Drive Mounter.app"
on error errStr number errorNumber
--listen for the apple quit command and quit
if the errorNumber is equal to -128 then
quit
return 1
--listen for the unknown error and ignore it
else if the errorNumber is equal to -5014 then
return 5
else
--all other errors are also ignored
return 5
end if
end try
--return with a wait of 5 seconds before next idle run
return 5
end idle
--------------------------------------------------------------------------------------
第二个脚本进行网络检查,然后尝试使用shell挂载卷 山.我最初使用的是查找器装载量",该代码也以注释的形式存在,但我不喜欢出现错误的对话框.即使只是一秒钟,所以我继续使用shell脚本.
the second script does the checking of the network, then tries to mount the volume using a shell mount. i originally used a finder "mount volume" and that codes exists as comments too, but I didn't like the dialog popping up on errors; even if only for a second, so i moved on to shell script.
--------------------------------------------------------------------------------------
--"Basic Drive Mounter.app"
try
set IP_address to "xxx.xxx.xxx.xxx"
set IP_Valid to true
try
do shell script ("ping -c 2 " & IP_address)
on error
set IP_Valid to false
end try
if IP_Valid then
tell application "Finder"
if disk "work" exists then
else
-->>shell script version
try
do shell script "mkdir /Volumes/work"
end try
do shell script "mount_afp afp://xxx.xxx.xxx.xxx/work /Volumes/work/"
--<<shell script version
-->>finder mount volume version
--with timeout of 1 second
-- mount volume "afp://xxx.xxx.xxx.xxx/work"
--end timeout
--<<finder mount volume version
end if
end tell
end if
on error
-->>finder mount volume version
--on error finder returns an error dialog which needs to be closed to go back and retry
--tell application "System Events"
-- keystroke return
--end tell
--<<finder mount volume version
return 0
end try
--------------------------------------------------------------------------------------
并非所有这些都是我自己的代码,所以非常感谢applescript社区和google-请记住将其支付
not all of this is my own code, so many thanks goes out to the applescript community and google - remember to pay it forward
这篇关于如何在AppleScript中禁止/自动关闭错误对话框的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!