A script to eject and spin down external drives
Apr 06, '04 10:05:00AM
Contributed by: Anonymous
This hint addresses two recurring problems of mine: the obnoxious loudness of my external hard drive, and its refusal to remount itself in a timely and orderly fashion. You'd think it would be enough to simply tell the Finder to Eject the HD -- but that won't do. Frequently, ejecting the drive will remove it from the system, but will NOT spin it down: the damn thing continues to whirr abusively in its casing! How might one silence an abrasive drive?
Read on for the answer, along with a handy re-mount script...
Open your Script Editor (in /Applications/Utilities), and paste the single line of code below:
do shell script "touch /Volumes/Your_Drivename/.dummy;
hdiutil eject `disktool -l | grep Your_Drivename |
awk '{ print substr($0,20,7) }'`"
As noted above, that should be one line -- replace the line breaks with a single space when entering into Script Editor.
Substitute the name of your external drive (simply as it appears on your Desktop and in the Finder) for Your_Drivename. Hit the Compile button in the Editor's toolbar. Select "Save As..." and be certain to indicate it as an "Application Bundle" in the "File Format" pull-down menu. Tick the "Run Only" checkbox, and name the thing "Eject FW Drive", or "Shutup!," or what have you, and tuck it away in your Utilities folder.
ASIDE: For those of you unfamiliar with the command line, there are two commands in there, separated by a semicolon. The first one, touch, will perform a trivial alteration on an invisible file named .dummy, bumping the "Last Modified" date to the current day and time -- if the file does not exist, touch will create it.
For some inexplicable reason, Mac OS X and its UNIX undercarriage absolutely refuse to spin down an external HD when asked to eject it, unless some file on the drive has been modified. A little touch is all that's needed: followed by the command to eject Your_Drivename (again, substituting in the name as it appears in/on the Desktop/Finder), this will vanish and silence your external drive!
Now, to keep this full-fledged application out of your Dock, simply perform the NSUIElement hack. It will now only run in the background, quite invisibly, never appearing in your Dock ... and thanks to Run Only, will also quit instantly. If you've got a hotkeying program like Butler, Spark, or (Steve help you) QuicKeys, set it up to run this. Bingo: whenever this is run, your external HD will disappear and become completely inaudible -- you'll receive no notification but its blissful silence.
Of course, this still leaves the problem of how to get the thing up and mounted again. I remember reading Hints about having to unplug the drive, then your laptop, then plug the drive and laptop in again, then run System Profiler ... and even then, you might have to logout and login again to get the damn thing to appear!
I said to myself, "Self, there's gotta be a better way..." And here it is ... get the Script Editor front and center once again. Paste this into it:
do shell script "diskutil mountDisk `disktool -l |
grep Your_Drivename | awk '{ print substr($0,20,7) }'`"
Again, that's one line (replace the return with a space), sub in your drive's real name for Your_Drivename, and hit Compile. Again, save it as "Run Only" and as an "Application Bundle" -- name it "Mount", or maybe "Arise!" Perform the NSUIElement trick, and bind it to a convenient hotkey. Whenever you run this thing, your detached and silent external HD will suddenly spring to life and mount itself. Presto: no more mucking about with cables or unneeded applications. NOTE: credit for this hint should be divided evenly among the members of Ars Technica's Macintoshian Achaia represented in this thread. Special thanks to new Achaian MitchM for his marvellous first-post contribution!
[robg adds: My external drive definitely spins down when unmounted, but the reattach script above is very useful -- no more pulling the power cable to get it to show itself again.]
Comments (15)
Mac OS X Hints
http://hints.macworld.com/article.php?story=20040401100219380