the following script works and is a subset of more complex script:
set pathVolume to “myServerPath”
set nomeVolume to “myVolumePath”
try
mount volume (“afp://” & pathVolume & “/” & nomeVolume) as user name “myUser” with password “myPwd”
on error msg number errnum
display dialog “My Error”
end try
but if for any reason the server or the network is down, an error that is NOT captured by the try on error is generated. After user press manually “OK” the display dialog “My Error” appear.
How to prevent the dialog error generated by operating system?
The script should work on machine that mount a lot of volumes.
I can check also if the volume is present in the list disks but if network goes down and after the 600 seconds timeout (10 minutes) the volume are automatically unmounted by OS X and if network is again down or server is down the script generate the above dialog.
as far as I know the only way to avoid the error message is to create the mount point explicitly with mkdir
and mount the volume with mount_afp
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
property serverList : missing value
property logPath : “Mountain Lion:Users:adminosx:Desktop:mountServerLog.txt”
set serverList to {}
copy {“myVolume_1”, “myServerAddress_1”} to end of serverList
copy {“myVolume_2”, “myServerAddress_2”} to end of serverList
copy {“myVolume_3”, “myServerAddress_3”} to end of serverList – any server can be simply commented so the serverList array will not contains it
on idle
repeat with j from 1 to count serverList
set volumeName to item 1 of item j of serverList
set serverAddress to item 2 of item j of serverList
try
do shell script "ping -c 2 " & serverAddress
set serverIsAvailable to true
on error msg number errnum
set serverIsAvailable to false
set timeString to do shell script "/bin/date '+%d %b %Y %H:%M:%S'"
set fileRef to open for access file logPath with write permission
write (timeString & tab & serverAddress & tab & "NOT AVAILABLE" & return) to fileRef starting at eof
close access fileRef
end try
if serverIsAvailable is true then
try
if volumeName is not in (list disks) then
mount volume ("afp://" & serverAddress & "/" & volumeName) as user name "myUser" with password "myPassword"
end if
on error msg number errnum
display dialog "Error:" & msg giving up after 60
end try
end if
end repeat
return 120
end idle
on quit
continue quit
end quit
I tested also removing Ethenet cable. A dialog that list all server will be dismissed after 600 seconds taht is timeout of OS X related to afp.
The same dialog is dismissed when you reconnect the Ethernet cable.
The dialog emntioned in previou post never appear because now there is a “ping” controlling that prevent the modal dialog that can’t be dismissed.
Also I added a log to verify when a particular server was not reachable.
I will move in production…
Stefan’s solution is indeed one way to suppress error messages. However I prefer an extra check if the node is an directory or really mounted. I posted it a few years ago here