我已经在我的 mac mini 媒体服务器上解决这个问题很久了,相信我终于找到了解决方案。
我把它分成两个脚本:
第一个在空闲时运行(而不是重复循环)并每 10 秒调用第二个脚本来处理驱动器安装。
--------------------------------------------------------------------------------------
--"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 脚本。
--------------------------------------------------------------------------------------
--"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 社区和谷歌 - 记得转发