If the ping fails 5 times, then display a dialog saying “File Server Not On” and quit the script.
Check to see if the volumes I want to mount are already mounted.
If it is not mounted then mount the volume.
If an error occurs, display a dialog saying “Some Volumes Failed to Mount” but still try and mount other volumes.
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)
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
.
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)
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…
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?
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
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…
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?
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…