Mounting volumes with a working timeout mechanism

While creating an application for our DTP department to batch manipulate certain symbols in InDesign documents I needed the functionality to mount several server volumes from within the app.

I implemented this by calling an AppleScritpt, which uses the «mount volume» command. But unfortunately this command tries to mount the volume for a long time, even if I only want to wait for 10 seconds and then abort the process. With all those deadlines I don’t want my colleagues to watch the beach ball for 60 seconds…

And here is how you can overcome the problem and successfully abort the «mount volume» command after a certain period of time. Maybe someone else can make good use of it.

1. script, named mount.scpt


try
	mount volume "afp://user:password@192.9.200.154/Volume-Name"
end try

2. script, calling script 1


property volumename : "Test"

on run
	-- the mount script is expected to reside in the same directory as this script
	set mypath to (path to me) as text
	set parentfolderpath to my getparentfolderpath(mypath)
	set mountscriptpath to POSIX path of (parentfolderpath & "mount.scpt")
	
	-- we are asking AppleScript Runner to execute the mount script
	ignoring application responses
		tell application "AppleScript Runner"
			launch
			do script mountscriptpath
		end tell
	end ignoring
	
	-- will the volume be mounted?
	set volumepath to (volumename & ":")
	set mountsuccess to false
	repeat 10 times
		if my itempathexists(volumepath) then
			set mountsuccess to true
			exit repeat
		end if
		delay 1
	end repeat
	
	-- the volume could not be mounted in time...
	if mountsuccess is false then
		ignoring application responses
			tell application "AppleScript Runner" to quit
		end ignoring
		
		set errmsg to "The volume " & volumename & " could not be mounted."
		error errmsg number 42001
	end if
end run

-- I am indicating if a given item path exists
on itempathexists(itempath)
	try
		set itemalias to itempath as alias
		return true
	on error
		return false
	end try
end itempathexists

-- I am returning the parent folder path of a given item path
on getparentfolderpath(itempath)
	set oldDelims to AppleScript's text item delimiters
	set AppleScript's text item delimiters to ":"
	set itemcount to (count text items of itempath)
	set lastitem to the last text item of itempath
	if lastitem = "" then
		set itemcount to itemcount - 2 -- folder path
	else
		set itemcount to itemcount - 1 -- file path
	end if
	set parentfolderpath to text 1 thru text item itemcount of itempath & ":"
	set AppleScript's text item delimiters to oldDelims
	return parentfolderpath
end getparentfolderpath

Happy Scripting :smiley: