Go Back   The macosxhints Forums > OS X Help Requests > System



Reply
 
Thread Tools Rate Thread Display Modes
Old 02-05-2003, 11:28 AM   #1
kerim
Major Leaguer
 
Join Date: Jan 2002
Posts: 311
Question Eject all

Whenever I take my iBook out with me I need to eject all my firewire drives. I would love to have a script that automatically ejected all removable drives for me. Anyone know how I might do this? (Applescript or Shell Script...)
kerim is offline   Reply With Quote
Old 02-05-2003, 01:07 PM   #2
mclbruce
Hall of Famer
 
Join Date: Mar 2002
Posts: 3,878
There is an eject command in AppleScript. The format for a script looks something like this:

tell application "Finder"
eject "diskname"
eject "diskname2"
end tell

This will work if you have a fixed list of disks that you wish to eject. If the disks to be ejected vary from time to time than it might get more complicated.
mclbruce is offline   Reply With Quote
Old 02-05-2003, 04:05 PM   #3
kerim
Major Leaguer
 
Join Date: Jan 2002
Posts: 311
Yeah, that's the problem (although I didn't know about that command- thanks!) I am plugging in different sets of firewire drives in different places or at different times... But I want all of them to eject at once when I am ready to go.
kerim is offline   Reply With Quote
Old 02-05-2003, 05:46 PM   #4
sbur
Major Leaguer
 
Join Date: Apr 2002
Location: Saint Peter, MN
Posts: 330
You might be able to write a script like the one above, but base it on the /Volumes directory and exempt the ones that shouldn't be ejected and are presumably "local."

I don't use apple script, so I don't know the details. Just trying to add some strategy.
sbur is offline   Reply With Quote
Old 02-05-2003, 07:02 PM   #5
mclbruce
Hall of Famer
 
Join Date: Mar 2002
Posts: 3,878
Possibly you could have AppleScript find out what the mounted volume names are, what the startup volume name is, and have it eject all mounted volumes but the startup volume. That's beyond my meager AppleScript skills though.

Another approach:

tell application "Finder"
try
eject "diskname"
eject "diskname2"
end try
end tell

You could then just list all of your drives. if AppleScript couldn't find the drive it would just go on to the next one. I don't know how long it would take the script to give up on a drive though.
mclbruce is offline   Reply With Quote
Old 02-05-2003, 08:13 PM   #6
kerim
Major Leaguer
 
Join Date: Jan 2002
Posts: 311
Cool

Thanks everyone. I added some lines to check if the disks are mounted and it now seems to work great! (Before it crashed the Finder in a bad way - but this is quite stable as far as I can tell.)


Code:
tell application "Finder"
	try
		if "Disk1" exists then
			eject "Disk1"
		end if
		if "Disk2" exists then
			eject "Disk2"
		end if		
	end try
end tell
kerim is offline   Reply With Quote
Old 02-05-2003, 08:22 PM   #7
kerim
Major Leaguer
 
Join Date: Jan 2002
Posts: 311
Now it would just be good to have something to double check if there are any unknown volumes attached that might cause trouble if the Firewire cable is removed. As well as a confirmation dialog to let you know whether or not it is safe to remove the cable. (That part is easy: "display dialog "OK, you can remove the cable".)

I suppose a shell script could check the "Volumes" directory, but this seems more complicated.

Fortunately, I think I'll be fine with the script as it is - as I don't have that many firewire drives. But if someone adds such a checking feature please post it to here!
kerim is offline   Reply With Quote
Old 02-05-2003, 08:33 PM   #8
at_sym
Triple-A Player
 
Join Date: Jan 2002
Posts: 179
Do you always have a set number of disks connected? Because you may want to make the script more flexible with a loop. Something like this:

tell application "Finder"
set noEject to name of startup disk
set ejectMe to every disk whose (name is not noEject and ejectable is
true)
repeat with x in ejectMe
set ejectee to name of x
try
eject disk ejectee
end try
end repeat
end tell

I think that'll work, but I'm Macless at the moment.
at_sym is offline   Reply With Quote
Old 02-05-2003, 10:22 PM   #9
djn1
MVP
 
Join Date: Apr 2002
Location: UK
Posts: 1,212
alternative applescript solution

Here's an alternative AppleScript solution:
Code:
tell application "Finder"
	set theDisks to name of every disk
	if {"disk1"} is in theDisks then
		eject disk "disk1"
	end if
	if {"disk2"} is in theDisks then
		eject disk "disk2"
	end if
end tell
I use a variant of this to eject either my camera or Apacer Drive and have had no problems. Providing you have an 'if' statement for each of the named disks might have attached at any point the script should eject them without difficulty.
__________________
chromasia

G4/800, OS 10.3.x, 1.25GB RAM, 2x80GB HD, 60GB firewire, Geforce4MX, Iiyama VMpro 455+413
Goldtouch keyboard, Cirque EasyCat trackpad, D-Link DSL-604+
djn1 is offline   Reply With Quote
Old 02-05-2003, 10:23 PM   #10
chabig
Hall of Famer
 
Join Date: Jan 2002
Posts: 3,016
tell application "Finder"
eject the disks
end tell


It looks really simple but it seems to work for me. Of course, the startup disk is just ignored.

Save this as an application and just keep it in your dock.

Chris
chabig is offline   Reply With Quote
Old 02-05-2003, 11:03 PM   #11
mclbruce
Hall of Famer
 
Join Date: Mar 2002
Posts: 3,878
This is a great thread, I'm learning a lot. Thanks, everybody.

If you don't want to keep the script in the dock you could have the "eject all" script or applet accessable from the menu bar. Look in the AppleScript appliction folder for Script Menu.menu. It displays the contents of your home: Library: Scripts folder in the menu bar. I think it displays what's in Library: Scripts as well.

Last edited by mclbruce; 02-05-2003 at 11:16 PM.
mclbruce is offline   Reply With Quote
Old 02-05-2003, 11:17 PM   #12
kerim
Major Leaguer
 
Join Date: Jan 2002
Posts: 311
Here is a script that might be the basis for a little mini-app. I still haven't figured out how to add an "eject all" button to the dialog (for some reason button {"1","2","3"} doesn't seem to work in a list dialog), or to make it select all the items by deafult ("default items 2 thru n" doesn't work), but the script itself is a good way to see all your disks and select which ones you want to eject. I eliminated the startup disk by simply removing the first item from the list - I think the startup disk will always be the first item.

I hope someone can improve on this!

Code:
tell application "Finder"
	set theDisks to name of every disk as list
	set n to number of items in theDisks
	if n = 1 then
		display dialog "Sorry, no ejectable disks"
	else
		set my_disks to items 2 thru n of theDisks
		choose from list my_disks with prompt 
"Select which disks to eject" OK button name
 "Eject" with multiple selections allowed and
 empty selection allowed
		set to_eject to result
		eject to_eject
	end if
end tell
[edit: fold -mt]

Last edited by mervTormel; 02-05-2003 at 11:24 PM.
kerim is offline   Reply With Quote
Old 02-06-2003, 06:43 AM   #13
rusto
MVP
 
Join Date: Jan 2002
Location: Boston, MA
Posts: 1,489
Kerim,

Your script returns

Finder got an error: Handler can't handle objects of this class.

when you click "Cancel" in the dialog box. No problems if you hit "Eject" with no selection though.

Looks like it chokes on the line,

Code:
eject to_eject
__________________
:: 3.4GHz Core i7 iMac 4GB RAM :: Black MacBook SR :: 10.7.2 :: iPhone 4 / iOS 5 ::
rusto is offline   Reply With Quote
Old 02-06-2003, 06:49 AM   #14
djn1
MVP
 
Join Date: Apr 2002
Location: UK
Posts: 1,212
fix

which can be fixed as follows:
Code:
tell application "Finder"
	set theDisks to name of every disk as list
	set n to number of items in theDisks
	if n = 1 then
		display dialog "Sorry, no ejectable disks"
	else
		try
			set my_disks to items 2 thru n of theDisks
			choose from list my_disks with prompt "Select which disks to eject" OK button name "Eject" with multiple selections allowed and empty selection allowed
			set to_eject to result
			eject to_eject
		end try
	end if
end tell
__________________
chromasia

G4/800, OS 10.3.x, 1.25GB RAM, 2x80GB HD, 60GB firewire, Geforce4MX, Iiyama VMpro 455+413
Goldtouch keyboard, Cirque EasyCat trackpad, D-Link DSL-604+
djn1 is offline   Reply With Quote
Old 02-06-2003, 08:56 AM   #15
ericw13
Major Leaguer
 
Join Date: Oct 2002
Posts: 268
If anyone cares, here's a bash script that should work:
Code:
#!/bin/bash
# The following df | awk will print the first and last columns of df separated by a colon.
# eg. /dev/disk1s1s2:/Volumes/Civilization III

for vol in `df | awk '{print $1":"$6}'`
do
    # Sets mp (mountpoint) to the part trailing the colon... /Volumes/Civilization III
    mp=${vol##*:}

    # The first part of the conditional trims off the smallest string matching a / 
    # and anything trailing it and returns the left overs... 
    # We only care about mountpoints in /Volumes.
    if [ "${mp%/*}" == "/Volumes" ]
    then
        # Returning to $vol, we want to remove anything matching a : and all trailing 
        # characters to leave just the device name... /dev/disk1s1s2.

        Echo ejecting ${vol%%:*}
        hdiutil detach ${vol%%:*}
    fi
done
I just tried this with only a CD mounted, and it found /Volumes/<CDname>, used the df output to determine the actual device name (/dev/disk1s1s2) and ejected it.

I see no reason a FW drive should not be similarly ejected.

Eric
ericw13 is offline   Reply With Quote
Old 02-06-2003, 09:34 AM   #16
chabig
Hall of Famer
 
Join Date: Jan 2002
Posts: 3,016
I think you're all making this too hard. It needs to be simpler than just dragging the disks to the trash or else there no benefit.

Code:
tell application "Finder"
         eject the disks
 end tell
This works. Try it.

Chris
chabig is offline   Reply With Quote
Old 02-06-2003, 10:22 AM   #17
kerim
Major Leaguer
 
Join Date: Jan 2002
Posts: 311
Quote:
Originally posted by chabig
I think you're all making this too hard. It needs to be simpler than just dragging the disks to the trash or else there no benefit.

SOme people don't know how to have fun

I think it is good to have some feedback so you know what you are doing - and maybe even an option to not eject everything. One comprimise might be to have a script that ejects all if the option key is held down during lauch - otherwise it presents the menu above. Anyone know how to do this?
kerim is offline   Reply With Quote
Old 02-06-2003, 10:43 AM   #18
V-tach
Prospect
 
Join Date: Jun 2002
Posts: 20
Well, what I would like to know is how to get any of these scripts to run automatically on shut down or logout. Is there anything that corresponds to "Startup Items" for use at the "other end" of the day?
V-tach is offline   Reply With Quote
Old 02-06-2003, 10:51 AM   #19
kerim
Major Leaguer
 
Join Date: Jan 2002
Posts: 311
I've seen some third-party solutions for this on VersionTracker. Can't remember what they were called. It would actually be nice to have something like this for when the computer goes to sleep. With a bus-powered firewire drive, it gets disconnected when the power gets cut off, resulting in an error message... (Apple should really fix this!)
kerim is offline   Reply With Quote
Old 02-06-2003, 12:15 PM   #20
pink
Major Leaguer
 
Join Date: Jul 2002
Location: Germany
Posts: 441
Quote:
anything that corresponds to "Startup Items" for use at the "other end" of the day?

You can use a "LogoutHook";
there's is crude description in this hint
http://www.macosxhints.com/article.p...30116061349986
the link therein has been removed by apple, but maybe you can find the information somewhere else at apple's site.

cheers, pink
__________________
"And what have we got in here ? - Ahh, things.." (Louis (2), inspecting kitchen cupboards.)
pink is offline   Reply With Quote
Reply


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump



All times are GMT -5. The time now is 05:36 PM.


Powered by vBulletin® Version 3.8.7
Copyright ©2000 - 2014, vBulletin Solutions, Inc.
Site design © IDG Consumer & SMB; individuals retain copyright of their postings
but consent to the possible use of their material in other areas of IDG Consumer & SMB.