Submit Hint Search The Forums LinksStatsPollsHeadlinesRSS
14,000 hints and counting!

A script to open/close an external CD/DVD via keyboard UNIX
I have both an internal DVD drive on my G4 and an external CD burner connected via Firewire. The internal drive can always be opened using the F12 key, as this is built into OS X. I wanted the same, or at least similar, functionality for my external CD drive. I looked all over for an easy way to accomplish this task, but could not find any. So, in the end, I had to create my own.

First, I realized that the drutil terminal command allowed me to do exactly what I wanted as long as I knew the drive number of my external CD drive. For example, to eject drive 2, the command is drutil eject -drive 2, and to close drive 2 the command is drutil tray close -drive 2.

At first I thought, "Wow, that makes it easy!" All I needed to do then was hook these two commands into separate keyboard shortcuts using an application like Spark. I chose F6 for the Eject command and Option-F6 for the Close command. But not so fast. I soon discovered that my two drives did not always have the same drive number after a restart. This added a new wrinkle to my task. After some research into some advanced (at least it was advanced to me) Bash scripting, here is what I came up with.

First, you need to do a drutil list in Terminal to see your multiple drives. Mine looks like this:
   Vendor   Product           Rev   Bus       SupportLevel
1  PIONEER  DVD-RW  DVR-107D  1.13  ATAPI     Apple Shipping
2  LITE-ON  LTR-52327S        QS03  FireWire  Apple Supported
Take note of the vendor for your external drive. Above, my external drive vendor is listed as "LITE-ON" (capitalization is important!). Next, here is the script I came up with to eject and close the drive:
#!/bin/bash

driveselect=`/usr/bin/drutil list|grep "LITE-ON"|sed 's/  LITE-ON.*$//'`
if [ "$1" = "-c" ]; then
  drutil tray close -drive $driveselect;
elif [ "$1" = "-o" ]; then
  drutil eject -drive $driveselect;
fi
Notice that where LITE-ON appears in the code above, you need to substitute the name of the vendor for your particular external drive.

You can create this using your favorite Terminal text editor, or a free program like Bare Bones' TextWrangler. Give the file a name and save it. If you want to access this script via the Terminal, you need to save it somewhere in your defined file path. Personally, I named mine sesame and saved it to /Users/Shared/bin/ (a directory I created long ago for such purposes). Next make the file executable by doing a chmod a+x sesame in the Terminal. Now, using terminal, sesame -o ejects the drive and sesame -c closes the drive.

Finally, using Spark and a do shell script command, I hooked sesame -o to F6 and sesame -c to option-F6. Now go relax and enjoy your new keyboard shortcuts!
    •    
  • Currently 2.60 / 5
  You rated: 1 / 5 (5 votes cast)
 
[21,185 views]  

A script to open/close an external CD/DVD via keyboard | 15 comments | Create New Account
Click here to return to the 'A script to open/close an external CD/DVD via keyboard' hint
The following comments are owned by whoever posted them. This site is not responsible for what they say.
A script to open/close an external CD/DVD via keyboard
Authored by: Baggins on Feb 15, '05 12:05:12PM

This is seriously cool. Thanks.



[ Reply to This | # ]
A script to open/close an external CD/DVD via keyboard
Authored by: SlamMan on Feb 15, '05 12:33:54PM

Its been a while since I've used a second drive, but I thought that something like Control+Eject or Shift+Eject would work on a second drive.



[ Reply to This | # ]
A script to open/close an external CD/DVD via keyboard
Authored by: afb on Feb 15, '05 10:03:39PM

it would be very nice to map this to shift+ or option+eject. Anyone think this could be done by changing the key mappings?

I found out though that control+eject gives you a shut down dialog.

---
now is the time for all good cows to come to the aid of their pasture.



[ Reply to This | # ]
A script to open/close an external CD/DVD via keyboard
Authored by: rvamerongen on Feb 15, '05 12:37:14PM

This is a nice way to open the external drive.

But I have two pioneers and a cd drive

Vendor Product Rev Bus SupportLevel
1 PIONEER DVD-RW DVR-108 1.06 FireWire Vendor Supported
2 PIONEER DVD-RW DVR-104 A227 ATAPI Apple Shipping

The thirth is not found by drutil, but is also in the firewire case and works normal.

What do you think that I need to change to your script to let it work with my drive's? I did try several things but it didn't work

Thanks



[ Reply to This | # ]
Filter by bus type (FireWire, ATAPI)
Authored by: gatorparrots on Feb 15, '05 01:32:51PM
The change is fairly simple. Instead of relying on vendors, it would be better to filter by the bus type (the internal ATA, or external FireWire or USB):
#!/bin/sh
##==================================================
# opensesame -- open/close external optical drives
##==================================================
if [ -z $(uname -r | grep "7.") ]; then
    echo "Sorry, $(basename $0) require OS X 10.3+"
    exit 1
fi
if [ -z "$1" ]; then
    echo "usage: $(basename $0) -c (closes external optical drive)"
    echo "       $(basename $0) -o (opens external optical drive)"
    exit 1
fi
theDRIVE=$(/usr/bin/drutil list | awk '/(FireWire)|(USB)/ {print $1}')
if [ "$1" = "-c" ]; then
  drutil tray close -drive $theDRIVE
elif [ "$1" = "-o" ]; then
  drutil eject -drive $theDRIVE
fi


[ Reply to This | # ]
A script to open/close an external CD/DVD via keyboard
Authored by: sidekickr on Feb 15, '05 01:33:22PM

I believe that if you replace the "LITE_ON" with "PIONEER DVD-RW DVR-104" that the DVR-104 would be controled by the bash script. You may have to escape the spaces to be something along the lines of "\ " I can't test this due to lack of Mac access at the moment, but it should work.



[ Reply to This | # ]
A script to open/close an external CD/DVD via keyboard
Authored by: mark hunte on Feb 15, '05 02:04:09PM

For does who have only one external you could use

#!/bin/bash

if [ "$1" = "-c" ]; then
drutil tray close -drive external;
elif [ "$1" = "-o" ]; then
drutil eject -drive external ;
fi

---
mh



[ Reply to This | # ]
A script to open/close an external CD/DVD via keyboard
Authored by: jimhoyt on Feb 15, '05 03:59:57PM
If you've just got one external drive you could just fire off this Applescript:

property drawerstatus : true

if drawerstatus is true then
    do shell script "drutil eject -drive external"
else
    do shell script "drutil tray close -drive external"
end if
set drawerstatus to not drawerstatus
Attach it to a key using System Preferences or your method of choice. The first press will open the tray and the next will close it. Worst case, it takes two presses if something interferes with the tray.

[ Reply to This | # ]
A script to open/close an external CD/DVD via keyboard
Authored by: mark hunte on Feb 15, '05 04:59:11PM

I have tried before to attach a script to keys using the keyboard and mouse prefs. but never could get it to work. any clues as to how to do this.


---
mh



[ Reply to This | # ]
A script to open/close an external CD/DVD via keyboard
Authored by: pismomac on Feb 15, '05 03:58:16PM

My understanding is that this will only work with a "burner" drive.
CD-RW, DVD-RW, CD-R, DVD-R, etc.

If I am wrong I apologize.



[ Reply to This | # ]
A script to open/close an external CD/DVD via keyboard
Authored by: disinfor on Feb 15, '05 03:57:44PM

Ok..I've tried and tried, but now I post.

I have the command working to open the drive and close it, but if someone could give a little more info on using Spark to bind the keys to do the actions, that would be very helpful. I'm fairly green when it comes to terminal use and command line use (except for DOS, but who needs that? :)



[ Reply to This | # ]
A script to open/close an external CD/DVD via keyboard
Authored by: mari0 on Feb 15, '05 05:47:00PM

same here... done the terminal part, stuck at spark...



[ Reply to This | # ]
A script to open/close an external CD/DVD via keyboard
Authored by: disinfor on Feb 16, '05 02:43:03PM
Check out Jimhoyt reply in this thread. You can make the applescript from that and then link it using Spark. I'll repost it here: Make an applescript (either with Script Editor or within Spark)
property drawerstatus : true

if drawerstatus is true then
    do shell script "drutil eject -drive external"
else
    do shell script "drutil tray close -drive external"
end if
set drawerstatus to not drawerstatus
(I changed the
"drutil eject..." to "/users/shared/bin/opendisc -o" 
which is what I called my terminal action and the
 "drutil tray close to "/users/shared/bin/opendisc -c" 
...maybe a little more work than necessary, but it works) In Spark you choose new applescript or cmd-1 and you can enter the applescript there or import your script you created. Set the Shortcut key, give it a name, hit create and turn Spark on! Voila! That may make some sense. Hopefully it does. I got it to work.

[ Reply to This | # ]
A script to open/close an external CD/DVD via keyboard
Authored by: mickelsn on Feb 15, '05 06:22:52PM

This is cool, but you can also use the utility Ardiem to do this...

http://themacaddress.com/ardiem/



[ Reply to This | # ]
A script to open/close an external CD/DVD via keyboard
Authored by: mickelsn on Feb 15, '05 06:29:58PM

OK, Ardiem JUST updated today to v1.1, available here:

http://www.chaoticminds.org/modules/news/



[ Reply to This | # ]