SMB Automount Script

I’ve just started playing with AppleScript and here’s what I’ve been trying to do…

I’ve tried to combine these 3 into one:

  1. Ping the File Server to see if it exists then connect
    http://macscripter.net/viewtopic.php?pid=41577
  2. Try pinging 5 times before failing
    http://forums.macosxhints.com/showthread.php?t=96066
  3. Check to see if the volumes are already mounted
    http://macscripter.net/viewtopic.php?id=4588

Here’s what I want the script to do.

  1. Ping the server up to 5 times.
  2. If the ping fails 5 times, then display a dialog saying “File Server Not On” and quit the script.
  3. Check to see if the volumes I want to mount are already mounted.
  4. If it is not mounted then mount the volume.
  5. If an error occurs, display a dialog saying “Some Volumes Failed to Mount” but still try and mount other volumes.
  6. If no error occurs, display a dialog saying “All Volumes Mounted”

Now here’s the script I came up with:


set idx to 0
set gotPing to false
repeat with idx from 1 to 5
	set ping_result to (do shell script "ping -c 1 192.168.0.2")
	if "100% packet loss" is in ping_result then
		delay 5
	else
		exit repeat
	end if
end repeat
if idx ≥ 5 then
	display dialog "File Server Not On." buttons "OK" default button 1 with icon 2
	error number -128
end if

tell application "Finder"
	try
		set usr to "Administrator"
		set pwd to "password"
		set mountedDisks to list disks
		
		if mountedDisks does not contain "Films" then
			mount volume "smb://" & usr & ":" & pwd & "@192.168.0.2/Films"
		end if
		
		if mountedDisks does not contain "TV" then
			mount volume "smb://" & usr & ":" & pwd & "@192.168.0.2/TV"
		end if
		
		if mountedDisks does not contain "TV2" then
			mount volume "smb://" & usr & ":" & pwd & "@192.168.0.2/TV2"
		end if
		
		if mountedDisks does not contain "Downloads" then
			mount volume "smb://" & usr & ":" & pwd & "@192.168.0.2/Downloads"
		end if
		
		if mountedDisks does not contain "iTunes Music" then
			mount volume "smb://" & usr & ":" & pwd & "@192.168.0.2/iTunes Music"
		end if
		
		if mountedDisks does not contain "iTunes" then
			mount volume "smb://" & usr & ":" & pwd & "@192.168.0.2/iTunes"
		end if
		
		display dialog "All Volumes Mounted" buttons "OK" default button 1 with icon 2 giving up after 3
		
	on error number -55
		display dialog "Some Volumes Failed To Mount." buttons "OK" default button 1 with icon 2
	end try
end tell

But when I turn the wireless off to see if the pinging works… it turns out that I just get an AppleScript Error and not the dialog. Now why does this happen?
Also, if I can do it I would like to know a way to see which volumes failed to mount and list the ones that couldn’t be mounted in a dialog at the end.

Any help will be greatly appreciated!!!

Thanks,

Tak

Browser: Firefox 3.0.4
Operating System: Mac OS X (10.5)

Hi,

obviously the ping command returns an error if the host is not available.
Just catch the error


.
repeat with idx from 1 to 5
	try
		set ping_result to (do shell script "ping -c 1 192.168.0.2")
		if "100% packet loss" is in ping_result then
			delay 5
		else
			exit repeat
		end if
	on error
		set idx to 6
		exit repeat
	end try
end repeat
.

Thanks for the quick reply!

It works now with the following (I had to add “try” around though…)


set idx to 0
set gotPing to false

repeat with idx from 1 to 5
	try
		set ping_result to (do shell script "ping -c 1 192.168.0.2")
		if "100% packet loss" is in ping_result then
			delay 5
		else
			exit repeat
		end if
	on error
		set idx to 6
		exit repeat
	end try
end repeat

if idx ≥ 5 then
	display dialog "File Server Not On." buttons "OK" default button 1 with icon 2
	error number -128
end if


tell application "Finder"
	try
		set usr to "Administrator"
		set pwd to "password"
		set mountedDisks to list disks
		
		if mountedDisks does not contain "Films" then
			mount volume "smb://" & usr & ":" & pwd & "@192.168.0.2/Films"
		end if
		
		if mountedDisks does not contain "TV" then
			mount volume "smb://" & usr & ":" & pwd & "@192.168.0.2/TV"
		end if
		
		if mountedDisks does not contain "TV2" then
			mount volume "smb://" & usr & ":" & pwd & "@192.168.0.2/TV2"
		end if
		
		if mountedDisks does not contain "Downloads" then
			mount volume "smb://" & usr & ":" & pwd & "@192.168.0.2/Downloads"
		end if
		
		if mountedDisks does not contain "iTunes Music" then
			mount volume "smb://" & usr & ":" & pwd & "@192.168.0.2/iTunes Music"
		end if
		
		if mountedDisks does not contain "iTunes" then
			mount volume "smb://" & usr & ":" & pwd & "@192.168.0.2/iTunes"
		end if
		
		display dialog "All Volumes Mounted" buttons "OK" default button 1 with icon 1 giving up after 3
		
	on error number -55
		display dialog "Some Volumes Failed To Mount." buttons "OK" default button 1 with icon 2
	end try
end tell

Is there a way of putting the Volume Names into an array and do a while/for loop to mount it?
The script at the moment looks very untidy and also that way I’ll be able to do display a dialog which tells me which drives failed to mount easier… I think…

Also with regards to error numbers, I simply copy n pasted it from the scripts I mentioned but what do they mean? (-128 and -55)

Thanks,

Tak

try this


property IPaddress : "192.168.0.2"
property volumeList : {"Films", "TV", "TV2", "Downloads", "iTunes Music", "iTunes"}
property usr : "Administrator"
property pwd : "password"

set idx to 0
set gotPing to false

repeat with idx from 1 to 5
	try
		set ping_result to (do shell script "ping -c 1 " & IPaddress)
		if "100% packet loss" is in ping_result then
			delay 5
		else
			exit repeat
		end if
	on error
		set idx to 6
		exit repeat
	end try
end repeat

if idx ≥ 5 then
	display dialog "File Server Not On." buttons "OK" default button 1 with icon 2
	error number -128
end if

set failedList to {}
set mountedDisks to list disks
repeat with oneVolume in volumeList
	try
		if mountedDisks does not contain oneVolume then
			mount volume "smb://" & usr & ":" & pwd & "@" & IPaddress & "/" & oneVolume
		end if
	on error number -55
		set end of failedList to oneVolume
	end try
end repeat

tell application "Finder"
	activate
	if failedList is {} then
		display dialog "All Volumes Mounted" buttons "OK" default button 1 with icon 1 giving up after 3
	else
		set {ASTID, AppleScript's text item delimiters} to {AppleScript's text item delimiters, return}
		set failedList to failedList as text
		set AppleScript's text item delimiters to ASTID
		display dialog "Some Volumes Failed To Mount:" & return & failedList buttons "OK" default button 1 with icon 2
	end if
end tell

Wow that’s amazing… Hats off to you…
Now i was just testing it out and it works apart from one thing…
After it comes up with a dialog saying some volumes failed to mount there’s also an AppleScript Error of type -6602…

What is this and how can I resolve this…?

Thanks,

Tak

does this change help?


.
set end of failedList to contents of oneVolume
.

nope… It comes up saying *** volume could not be mounted as expected but then gives error type -6602 afterwards…

forgot to add after the error comes up it highlights this section

mount volume "smb://" & usr & ":" & pwd & "@" & IPaddress & "/" & oneVolume

Thanks,

Tak

then it might be a SMB specific error.
I have no idea about it, my network is windows free :wink:

thanks for all the help so far… Now I’ve been fiddling around with this and now I’ve got to this:

property IPaddress : "192.168.0.2"
property volumeList : {"Films", "TV", "TV2", "Downloads", "iTunes Musick", "iTunesk"}
property usr : "Administrator"
property pwd : "password"

set idx to 0
set gotPing to false

repeat with idx from 1 to 5
	try
		set ping_result to (do shell script "ping -c 1 " & IPaddress)
		if "100% packet loss" is in ping_result then
			delay 5
		else
			exit repeat
		end if
	on error
		set idx to 6
		exit repeat
	end try
end repeat

if idx ≥ 5 then
	display dialog "File Server Not On." buttons "OK" default button 1 with icon 2
	error number -128
end if

set failedList to {}
set mountedDisks to list disks
repeat with oneVolume in volumeList
	if mountedDisks does not contain oneVolume then
		try
			mount volume "smb://" & usr & ":" & pwd & "@" & IPaddress & "/" & oneVolume
		on error
			set end of failedList to contents of oneVolume
		end try
	end if
end repeat

tell application "Finder"
	activate
	if failedList is {} then
		display dialog "All Volumes Mounted" buttons "OK" default button 1 with icon 1 giving up after 3
	else
		set {ASTID, AppleScript's text item delimiters} to {AppleScript's text item delimiters, return}
		set failedList to failedList as text
		set AppleScript's text item delimiters to ASTID
		display dialog "Some Volumes Failed To Mount:" & return & failedList buttons "OK" default button 1 with icon 2
	end if
end tell

Now this is perfect apart from each time the mounting fails, it displays a mounting failed dialog box which isn’t called by the script itself and the after those come up it comes up with the dialog that is called by the script with the list. So I’m thinking is there a way of mounting volume AND supressing errors?

Thanks,

Tak

the only way to avoid the system error message is to create the mount point with mkdir
and mount the volume with mount_smbfs

the afp equivalent is


mountAFP("myName", "myPassword", "myServerl", "myVolume")

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
		do shell script "/bin/rm -r " & mountpoint
		return false
	end try
end mountAFP

the handler returns true, if the mount attempt succeeded, otherwise false

With a little help from http://forums.macosxhints.com/archive/index.php/t-62815.html, I’ve got it working perfectly!!!

property IPaddress : "192.168.0.2"
property volumeList : {"Films", "TV", "TV2", "Downloads", "iTunes Music", "iTunes"}
property usr : "Administrator"
property pwd : "password"

set idx to 0
set gotPing to false

repeat with idx from 1 to 5
	try
		set ping_result to (do shell script "ping -c 1 " & IPaddress)
		if "100% packet loss" is in ping_result then
			delay 5
		else
			exit repeat
		end if
	on error
		set idx to 6
		exit repeat
	end try
end repeat

if idx ≥ 5 then
	display dialog "File Server Not On." buttons "OK" default button 1 with icon 2
	error number -128
end if

set failedList to {}
set mountedDisks to list disks
repeat with oneVolume in volumeList
	if mountedDisks does not contain oneVolume then
		try
			do shell script "mkdir /Volumes/" & oneVolume & "; mount_smbfs //" & usr & ":" & pwd & "@" & IPaddress & "/" & oneVolume & " /Volumes/" & oneVolume
		on error
			do shell script "rm -r /Volumes/" & oneVolume
			set end of failedList to contents of oneVolume
		end try
	end if
end repeat

tell application "Finder"
	activate
	if failedList is {} then
		display dialog "All Volumes Mounted" buttons "OK" default button 1 with icon 1 giving up after 3
	else
		set {ASTID, AppleScript's text item delimiters} to {AppleScript's text item delimiters, return}
		set failedList to failedList as text
		set AppleScript's text item delimiters to ASTID
		display dialog "Some Volumes Failed To Mount:" & return & failedList buttons "OK" default button 1 with icon 2
	end if
end tell

Thank you so much!!!

p.s. is this worth posting it in Code Exchange? Possibly adding it onto http://macscripter.net/viewtopic.php?pid=41577?
p.s.2 I know this was me being dumb but could you just get rid of my password on your post? I only use it on this computer but it still worries me a lil… Thank you…

Tak

I keep jumping to conclusion it wasn’t perfect… Now it can’t load “iTunes Music” because it has a space in it!

Now i know what to put into terminal to fix it:

But then now I need to look for a space in oneVolume and if there is one create a new variable one with the space replaced with %20 and then use that variable in… so kinda like this…


if oneVolume contains " " then
	set oneVolume2 to oneVolume --oneVolume2 is the one with %20 instead of a space
	do shell script "mkdir \"/Volumes/" & oneVolume & "\"; mount_smbfs //" & usr & ":" & pwd & "@" & IPaddress & "/" & oneVolume2 & " \"/Volumes/" & oneVolume & "\""
else
	do shell script "mkdir \"/Volumes/" & oneVolume & "\"; mount_smbfs //" & usr & ":" & pwd & "@" & IPaddress & "/" & oneVolume & " \"/Volumes/" & oneVolume & "\""
end if	

Any idea on how I could replace the space with %20 please?

Thanks,

Tak

Got it :D. Used Find&Replace function from http://macscripter.net/viewtopic.php?id=13008. I just used the first one because I don’t have any issues with Unicode etc. But I would like it to be shorter though so if you have any idea please tell. But for now, below code worked perfect.


on findAndReplace(tofind, toreplace, TheString)
	set ditd to text item delimiters
	set text item delimiters to tofind
	set textItems to text items of TheString
	set text item delimiters to toreplace
	if (class of TheString is string) then
		set res to textItems as string
	else -- if (class of TheString is Unicode text) then
		set res to textItems as Unicode text
	end if
	set text item delimiters to ditd
	return res
end findAndReplace

property IPaddress : "192.168.0.2"
property volumeList : {"Films", "TV", "TV2", "Downloads", "iTunes Music", "iTunes"}
property usr : "Administrator"
property pwd : "password"

set idx to 0
set gotPing to false

repeat with idx from 1 to 5
	try
		set ping_result to (do shell script "ping -c 1 " & IPaddress)
		if "100% packet loss" is in ping_result then
			delay 5
		else
			exit repeat
		end if
	on error
		set idx to 6
		exit repeat
	end try
end repeat

if idx ≥ 5 then
	display dialog "File Server Not On." buttons "OK" default button 1 with icon 2
	error number -128
end if

set failedList to {}
set mountedDisks to list disks
repeat with oneVolume in volumeList
	if mountedDisks does not contain oneVolume then
		try
			if oneVolume contains " " then
				set oneVolume2 to findAndReplace(" ", "%20", oneVolume)
				do shell script "mkdir \"/Volumes/" & oneVolume & "\"; mount_smbfs //" & usr & ":" & pwd & "@" & IPaddress & "/" & oneVolume2 & " \"/Volumes/" & oneVolume & "\""
			else
				do shell script "mkdir \"/Volumes/" & oneVolume & "\"; mount_smbfs //" & usr & ":" & pwd & "@" & IPaddress & "/" & oneVolume & " \"/Volumes/" & oneVolume & "\""
			end if
		on error
			do shell script "rm -r \"/Volumes/" & oneVolume & "\""
			set end of failedList to contents of oneVolume
		end try
	end if
end repeat

tell application "Finder"
	activate
	if failedList is {} then
		display dialog "All Volumes Mounted" buttons "OK" default button 1 with icon 1 giving up after 3
	else
		set {ASTID, AppleScript's text item delimiters} to {AppleScript's text item delimiters, return}
		set failedList to failedList as text
		set AppleScript's text item delimiters to ASTID
		display dialog "Some Volumes Failed To Mount:" & return & failedList buttons "OK" default button 1 with icon 2
	end if
end tell

Because of the lack of " around rm… it deleted my iTunes library on the server :(…
Very annoying but anyway I got everything working in the end so… at least there’s some comfort…

Sorry for that.
You can also use quoted form of to escape the spaces

set x to "iTunes Music"
set y to quoted form of ("/Volumes/" & x)
--> '/Volumes/iTunes Music'

Didn’t realize that’s what it did!
Thanks for everything :smiley: Couldn’t have done it without your help

Thanks,

Tak