Stop the script on error ...

set theDisk to "iMac G5"
do shell script "diskutil mount `diskutil list | awk '/ " & theDisk & " / {print $NF}'`"

Hi,

Using a script offered by StefanK in another MacScripter thread, I want to mount an external or a networked hard drive. If the drive is not available I want the script to quit. Mounting my local drive works but I don’t know how to stop the script if the drive is not connected.

Thanks in advance.

try this…

try
	set theDisk to "iMac G5"
	do shell script "diskutil mount `diskutil list | awk '/ " & theDisk & " / {print $NF}'`"
end try

Tom

Operating System: Mac OS X (10.5)

Hi,

just spilt the shell script line into 2 separate lines


set theDisk to "iMac G5"
set BSDname to do shell script "diskutil list | awk '/ " & theDisk & " / {print $NF}'"
if BSDname is "" then  quit -- or error number -128
do shell script "diskutil mount " & BSDname

How can I use your suggested scripts if there is more than one disk to mount ? I tried up to this script, but it dos not work:

property MyDiskList : {"iMac G5"}

set DisksToMount to do shell script "diskutil list | awk '/ " & MyDiskList & " / {print $NF}'"
if DisksToMount is not "" then
	
	repeat with i in DisksToMount
		do shell script "diskutil mount `diskutil list | awk '/ " & i & " / {print $NF}'`"
		delay 5
	end repeat
	
end if

Where do I go wrong ?

diskutil dosen’t work with a list of disks


property MyDiskList : {"iMac G5"}

repeat with i in MyDiskList
	set BSDname to do shell script "/usr/sbin/diskutil list | /usr/bin/awk '/ " & i & " / {print $NF}'"
	if BSDname is not "" then do shell script "/usr/sbin/diskutil mount " & BSDname
end repeat

Hi StefanK,

If I can … I have a few more questions:

¢ what does BSDname stand for ?
¢ why isn’t there any END IF statement in your script ?

Thanks again and alot !

Robert

¢ BSDname is the internal device node (e.g. disk0s5)
¢ if the whole if statement is in one line, end if is not needed

Thank you …

I have been trying to unmount my disks after the backup is completed with this script nested in a bigger script:

property MyDiskList : {"iMac G5"}

repeat with i in MyDiskList
	try
		with timeout of 5 seconds
			do shell script "diskutil unmount " & quoted form of ("/Volumes/" & i)
		end timeout
	end try
end repeat

It works on and off and is not reliable. Mounting disks works fine all the time. Do you have a clue on this one ?

the timeout block is useless, because only Apple Events sent to applications are affected by the timeout block

Your script should work anyway, but you’ll never get any error, if the volume is in use
try this


property MyDiskList : {"iMac G5"}

repeat with i in MyDiskList
	try
		do shell script "diskutil unmount " & quoted form of ("/Volumes/" & i)
	on error e
		display dialog e
	end try
end repeat

alternatively you can unmount a volume also by its BSD name

It works once and the second time, as before, I get a «Volume failed to unmount» error message. I tried many times and it unmounts one out of 2. Seems the disk is locked the first time …

I wait for the MOUNTING script to fully execute before running the UNMOUNTING one. This is the script I mount the volumes with:

property MyDiskList : {"iMac G5", "Volume 2"}

repeat with i in MyDiskList
	set theDisk to do shell script "/usr/sbin/diskutil list | /usr/bin/awk '/ " & i & " / {print $NF}'"
	if theDisk is not "" then
		do shell script "/usr/sbin/diskutil mount " & theDisk
	end if
end repeat

Having faced the same problem with a backup script for SuperDuper! (i.e. mounting works fine, dismounting is flakey) I discovered that if I simply waited for the dismount it worked as it should.

tell application "Finder" to eject disk "ACB-External"
	-- wait for it
	repeat until (list disks) does not contain "ACB-External"
		do shell script "sleep 3"
	end repeat

Hi Adam,

How do I use a disk with a space in it’s name with your script ? My disk is named “iMac G5”. Thanks in advance.

Robert Lespérance

quoted form of is the magic statement :wink:

Are you saying that that name doesn’t work? What do you see when you run:

tell application "Finder" to list disks

I get this result: {“Macintosh HD”, “Network”, “iMac G5”}. There was an error in the script I wrote using your logic. This new script, runned isolated (see below) from my global script, works perfectly:

property MyDiskList : {"iMac G5"}

repeat
	tell application "System Events" to exists process "Retrospect"
	if result is false then
		repeat with i in MyDiskList
			repeat until (list disks) does not contain i
				tell application "Finder" to eject disk i
			end repeat
		end repeat
		exit repeat
		
		--- wait 15 seconds and check if "Retrospect" is still running
	else
		delay 15
	end if
end repeat

Trying it in my global script without any manual intervention during the execution would require 3 hours. I was quitting “Retrospect” manually during the execution, and I think this has an effect on the way the script behaves. Since I don’t have any partition to test it to, I will see tonight when doing my full duplication backup.

Thanks for helping.

Robert Lespérance
Québec, Canada

Hi Adam,

When runned alone in a script, it works perfectly and always. Nested in an ON IDLE handler, I still get an error message 1 out of 2. Could you try it on your machine to see if you get the same result ? This is the script:

property MyDiskList : {"iMac G5"}

on idle
	
	repeat with i in MyDiskList
		repeat until (list disks) does not contain i
			tell application "Finder" to eject disk i
		end repeat
	end repeat
	delay 15
	
end idle

Regards.

Robert Lespérance