A way to eject CDs without knowing the name of the CD.

I just wanted to post my hack to get a keystroke to eject CDs (I don’t have a keyboard with an eject button on it). AppleScript seems to require a name of a disk or disk reference in order to eject it. Obvoiusly, with CDs you can’t pre-program what names they will have.

I discovered that the df command line utility could be used. I realized that my CDs were always /dev/disk1. (Your machine will probably be different, but I think it will be consistent,nonetheless). So, using /dev/disk1 as a handle for accessing whatever CD rom I might have in my drive, I wrote the following script:

tell application “Finder”
set cdName to do shell script “df | grep /dev/disk1 | sed -e ‘s@^.*/Volumes/@@’”
if cdName is not equal to “” then
set cdRef to disk cdName
– Test this volume to make sure it’s not a crucial volume before ejecting.
if local volume of cdRef is true and startup of cdRef is false and ejectable of cdRef is true then
eject cdRef
end if
display dialog “It doesn’t look like there’s a CD to eject.” buttons {“Oh well”} default button 1 with icon 1 giving up after 5
end if
end tell

  1. I need such a script that will also open an empty CD tray.

  2. What are you using to get a keystroke to do the applet?
    OSA Menu doesn’t work on Mac OSuX.
    Script Menu.menu won’t do it.
    I need a freeware keystroke launcher I can distribute to people.

I couldn’t think of any hooks in AppleScript that would allow me to do that, so rather than bang my head against the odd lacunas in AppleScript (which I’ve wasted far too many years of my life doing), I just opted for an eject of an already loaded disk. If you find a way to do this, please post it here.

I’m using HotApp. It’s not free, but it’s pretty good at launching AppleScripts. I know there are many other keystroke launchers out there. Take a look at http://macshareware.net or http://versiontracker.com

You should be able to get the “ejectable” property of the discs. I believe I’ve been able to eject all “mounted” CDs/Zip disks in the past by looking at all disks whose ejectable was true. I’d then eject those names.

If you need additional help, I’ll try to recreate the original scripts.


Thank you for your input. The point of this exercise, though, is to see if there’s a way to reliably unmount/eject only CDs. That’s the difficulty I was trying to solve with this little code snippet.

if you’re using 10.3.x, just use this handy command:

drutil tray eject

or if you want to wrap it in AS:

do shell script "drutil tray eject"

check out the man page for drutil. it didn’t ship with 10.2.x, but you can probably download it somewhere if you want the functionality.

to close the tray:

drutil tray close

Do I understand you correctly to be saying that there’s a way to get …

drutil tray eject
drutil tray close

… to work in Jaguar if I download something from somewhere?
(Or only merely to read the man pages in Jaguar?)

I desperately need to be able to eject CDs and open an empty tray in Jaguar from AppleScript!

Unfortunately, according to the man page, drutil uses the DiscRecording framewor to access attached Disc Burning devices. I assume this means that drutil is meaningless to a Mac without a CD burner, such as my PowerBook G3 Lombard. UPDATE: As I feared, the response I get when using drutil is “Could not find a valid device”.

However, if you are in the happy group of Mac users who can afford recent hardware, you most likely do have at least CD-R, if not CD-RW capabilities, in which case, this is definitely the better way to implement this.

One more issue: It would be nice for the keystroke (in order to truly replicate the eject key) to simply toggle the open and close. As far as I can see, drutil does not seem to generate a status code on whether the door is already open or not, so it might be difficult to achieve. Update: I still have not found a way for it to generate a meaningful status as to its being open or closed.