Mounting - What if the Macintosh the folder resides on is offline?

I have mounted several folders successfully in past scripts but I guess I never tried to run the scripts unless I knew the local Mac(s) were online. What I am running into is a pop-up window stating “Connection Failed! The server may not exist or it is not operational at this time. Check the server name or IP address and your network connection and try again.” and stops the script until I hit “Ok” when the target Mac is offline

I can’t be sure at any given time that the Mac(s) in question will be online because at any given time they may be taken offline or perhaps due to mechanical failure drop offline. There are any number of possible problems such as power outages that cannot be forseen so I am trying to set it up so the computer will:

  1. try to mount “afp:// lMyName : lMyPassword @DMT001.local/Target/”
  2. if it succeeds continue on to try to mount “afp:// lMyName : lMyPassword @DMT002(3, 4, 5…etc).local/Target/”
  3. if it fails suppress the warning pop-up and try to mount “afp:// lMyName : lMyPassword @DMT002(3, 4, 5…etc).local/Target/”
--> Mount all of the necessary drives

on sMounts(lThisLocDigit, lMyName, lMyPassword, lCompNameChars, gScriptsFolder)
	(*if ((item -1 of lCompNameChars ≠ "0") or (item -2 of lCompNameChars ≠ "0")) then -- If this is a client machine 
			mount volume ("afp://" & lMyName & ":" & lMyPassword & "@DMT" & lThisLocDigit & "00.local/Target/") as text -- Mount the local servers folder
		end try*)
	if ((item -1 of lCompNameChars = "0") and (item -2 of lCompNameChars = "0")) then -- If this is the server
		repeat with i from 1 to 10 -- Maximum possible number of mount points
			if (i < 9) then set i to ("0" & i) as text -- Must be a two digit number
			set lIDDigits to i
				mount volume ("afp://" & lMyName & ":" & lMyPassword & "@DMT" & lThisLocDigit & lIDDigits & ".local/Target/") as text -- Mount client folders
			on error 
			     exit repeat
			end try
		end repeat
	end if
end sMounts


use the shell to avoid the Finder message. With this form the scripter must care about creating (and removing) the mount point
The handler mounts the volume and returns true, or returns false if an error occurs

mountAFP("lMyName", "lMyPassword", "DMT001.local", "Target")

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)
		do shell script "/bin/mkdir " & mountPoint & "; /sbin/mount_afp " & theAddress & space & mountPoint
		return true
	on error
		do shell script "/bin/rm -rf " & mountPoint
		return false
	end try
end mountAFP

Okay, to my untutored eye I believe what this is saying is:

  1. Make a directory called Target in Macintosh HD/Volumes
  2. Mount afp://user_name:pass_word@DMT001.local/Target
  3. I have no idea why /Volumes/Target comes after the preceding (unless… Is this linking the mount that appears on my desktop with the path to the mount target?)
  4. Remove recursively and forcibly the directory Target from Macintosh HD/Volumes - but only in case of an error.
do shell script "/bin/mkdir /Volumes/Target; /sbin/mount_afp afp://user_name:pass_word@DMT001.local/Target /Volumes/Target"

do shell script "/bin/rm -rf /Volumes/Target"

I will check this out when I get back to my Mac. Thanks again StefanK.

Unlike the mount volume command, which politely does all the administrative stuff for you,
you have to create the mounting point (basically a directory) in /Volumes by yourself and tell the OS to mount the volume at this mounting point (that’s the /Volumes/Target)

If an error occurs, you have to take care removing the mounting point again

This is lovely. Thank you!