|
|
#1 |
|
Major Leaguer
Join Date: Jan 2002
Posts: 311
|
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...)
|
|
|
|
|
|
#2 |
|
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. |
|
|
|
|
|
#3 |
|
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.
|
|
|
|
|
|
#4 |
|
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. |
|
|
|
|
|
#5 |
|
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. |
|
|
|
|
|
#6 |
|
Major Leaguer
Join Date: Jan 2002
Posts: 311
|
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 |
|
|
|
|
|
#7 |
|
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! |
|
|
|
|
|
#8 |
|
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. |
|
|
|
|
|
#9 |
|
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
__________________
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+ |
|
|
|
|
|
#10 |
|
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 |
|
|
|
|
|
#11 |
|
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. |
|
|
|
|
|
#12 |
|
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 Last edited by mervTormel; 02-05-2003 at 11:24 PM. |
|
|
|
|
|
#13 |
|
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 :: |
|
|
|
|
|
#14 |
|
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+ |
|
|
|
|
|
#15 |
|
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 see no reason a FW drive should not be similarly ejected. Eric |
|
|
|
|
|
#16 |
|
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
Chris |
|
|
|
|
|
#17 | |||||||||||||||||||
|
Major Leaguer
Join Date: Jan 2002
Posts: 311
|
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? |
|||||||||||||||||||
|
|
|
|
|
#18 |
|
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?
|
|
|
|
|
|
#19 |
|
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!)
|
|
|
|
|
|
#20 | |||||||||||||||||||
|
Major Leaguer
Join Date: Jul 2002
Location: Germany
Posts: 441
|
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.) |
|||||||||||||||||||
|
|
|
![]() |
|
|