envygeeks
1/15/2018 - 8:01 PM

Mount SMB on Boot

Mount SMB on Boot

set smbHost to "192.168.1.3"
set closeOnUnmount to {"iTunes"}
set volnames to {"Storage", "Movies"}
set tmVolname to "Backups"
set idleTime to 120
set maxWait to 60

on idle
	tell application "Finder"
		repeat with vol in volnames
			try
				if not (exists disk vol) then
					mount volume "smb://" & smbHost & "/" & vol
					
					set i to 0
					repeat until name of every disk contains vol
						delay 1
						
						set i to i + 1
						if i is greater than maxWait then
							error "Unable to mount the given volume '" & vol & "', there is a problem."
							exit repeat
						end if
					end repeat
				end if
				
				if not (exists disk tmVolname) then
					tell application "System Events"
						set volPath to the POSIX path of the alias ("Volumes:" & vol as string)
						set volPath to quoted form of volPath & "/" & tmVolname & ".sparsebundle"
						if exists file volPath then
							do shell script "hdiutil attach " & volPath
							
							set i to 0
							repeat until name of every disk contains vol
								delay 1
								
								set i to i + 1
								if i is greater than maxWait then
									error "Unable to mount the given volume '" & vol & "', there is a problem."
									exit repeat
								end if
							end repeat
						end if
					end tell
				end if
			on error errorStr
				log "Got '" & errorStr & "' while trying to mount '" & vol & "'"
			end try
		end repeat
	end tell
	
	return idleTime
end idle