Finder window alts the script ....

Hi,

I have an «idle» handler that at some point ejects a connected hard disk. Ejecting disks by script seems to be possible, but really works one out of 2. The «ejecting» part of the script returns a finder window reminding that the disks are in use (to my knowledge no app or script are running from the disk and the disk’s use is for backup purpose only). This window hangs radomly my script, and the Script Editor returns also a window telling that the Finder is busy.

When tried in the finder, this hang up seems to disappear after time for no apparent reason. I have no clue to what really happens.

Is there a way by script to prevent the first dialog window to appear ? The second is caused by the first. This is the «ejecting» part of my script:


property theDiskList : {"theNameOfMyDisk"}

repeat with i in diskList
		repeat 10 times -- in case the disk never ejects
			if (list disks) contains i then
				tell application "Finder" to eject disk i
				delay 10 -- to give time to the disk to let go
			else
				exit repeat
			end if
		end repeat
end repeat
delay 30 -- to give more time to the disk to end ejecting

Thanks in advance.

Robert Lespérance

Model: iMac G5 - 1.6 mgz
AppleScript: 2.1.1
Browser: Safari 525.18
Operating System: Mac OS X (10.4)

Hi Robert,

there is a difference between eject and unmount. Volumes like USB-Sticks or Netshares can be ejected, other volumes like internal hard disk partitions can not
Therefore the disk element of the Finder dictionary has a ejectable property.
You have to use two different methods to “remove” the disk like this example


property myVolume : "theNameOfMyDisk"

tell application "Finder" to tell disk theVolume to set isEjectable to ejectable
if isEjectable then
	try
		tell application "Finder"
			eject disk myVolume
			repeat while exists (disk myVolume)
				delay 0.5
			end repeat
		end tell
	on error e
		reportError(e)
	end try
else
	try
		do shell script "/usr/sbin/diskutil unmount /Volumes/" & myVolume
	on error e
		reportError(e)
	end try
end if

on reportError(e)
	-- do some error handling
end reportError

Hi StefanK,

Until now, your script runs perfectly. Thank you. The script returned an error message because the requested disks was not available, so I added a line to be sure it was and wrapped it in a handler:

on EjectDisks(diskList)
	repeat with i in diskList
		if (list disks) contains i then
			tell application "Finder" to tell disk i to set isEjectable to ejectable
			if isEjectable then
				try
					tell application "Finder"
						eject disk i
						repeat while exists (disk i)
							delay 0.5
						end repeat
					end tell
				on error e
					reportError(e)
				end try
			else
				try
					do shell script "/usr/sbin/diskutil unmount /Volumes/" & i
				on error e
					reportError(e)
				end try
			end if
		end if
	end repeat
	
	delay 30
end EjectDisks

Thanks again. Regards.

Robert