The tray of a CD drive I can open via applescript with
do shell script "drutil tray eject"
and with
do shell script "drutil tray close"
I can close it. But I want to create a script which opens the tray when closed and closes it when opened. For this I would like to know the current status of the tray - whether it’s open or closed. Any idea how I can find out via applescript or shell commands?
Many thanks in advance, Olli
Model: iMac 20", OSX 10.8.3
Browser: Firefox 23.0
Operating System: Mac OS X (10.8)
I can’t swear I saw something that detected the status of the CD-tray at MacOsX Hints, but I am fairly sure I did.
So if I were you, I’d search for it there.
I am sorry that I can’t do any better than that at the moment.
But one thing that slipped into my mind in this very moment: If the CD-tray is open, then the volume wouldn’t be mounted, or at least shouldn’t be mounted, so maybe you can check for the status of the tray implicitly, by checking if the CD drive can be found among the mounted volumes?
And if that doesn’t work, then maybe the drutil command exits with an error code you can use to check for to determine if the drive was open , and then issue a following close command.
I have also seen that the key code for the Eject button, at least has been 161, I haven’t tested it.
I found the information here.
If you have an tray that can open and close it means you have an MacPro. That also means you have an external keyboard connected to it. Which leads us that F12 is mapped to the eject key. At least key code 111 behaves on my MacPro as the eject button. The reason for F12 is that the eject button is missing on third party keyboards, so when Mac OS X detects an external keyboard it maps the eject key to the F12 button. It doesn’t work for internal keyboards like laptops.
When you have this software installed it’s even easier to press the eject button. Just press the key code that match with your remap of the eject button.
That is what I expected. I assumed that the TS doesn’t have an iMac because MacPro’s only have real trays, so for him a good solution. I didn’t say that F12 and the eject buttons are the same but the eject button is mapped to F12 (virtually). That is for support for external third party keyboards who doesn’t have an eject button. Press fn + F12 because probably you have turned off that Functions key doesn’t behave as normal keys in system preferences.
tell application "System Events"
key code 111
end tell
doesn’t eject the tray, and doesn’t change the sound level.
Just for info, the lull wired keyboard is exactly the same for macPro and iMac.
I found a valid list of codes for F13 thru F19 and for every keys of the numerical pad but was unable to find one for F1 thru F12 which may be different according to localization setting.
property last_choice : "Open"
if last_choice = "Open" then
do shell script "drutil tray open -drive 1"
set last_choice to "Close"
else
do shell script "drutil tray close -drive 1"
set last_choice to "Open"
end if
I actually found a way around this.
There are some assumptions here, and one is that the devicename for the CD-Rom player doesn’t change:
I mounted a CD, I saw that it turned up in Terminal by issuing ls -l /Volumes.
Ok, then I fired up disk-utility, and I noted the device number on my machine for the CD-Rom, that is /dev/disk1s2.
Ok, so when a CD is in the drive, then I can ls -l /dev/disk1s2 without error, that is, when I perform an echo $?, then it returns 0. When there isn’t any CD in the drive, then the ls -l command returns 1, and there is nothing to eject.
Personally, I prefer this scheme over the one with a script property, since there are so many ways to eject a drive.
Disc Events is a faceless scriptable application (like System Events) with a few rudimental features.
You can get the tray status and open/close the tray and eject media
tell application "Disc Events"
tell device "SuperDrive"
if tray is open then
close tray
else
open tray
end if
end tell
end tell
Thanks Stefan. That’s exactly what I needed! I use it for my little script which alternates between opening and closing:
#!/bin/bash
function usage {
cat<<EOT
usage: ${0##*/}
Open CD tray (eject Media beforehand if needed) when closed and close CD tray when opened
EOT
}
if [ "$1" == "--help" ]; then
usage
exit 60
fi
if [ $# -gt 0 ]; then
echo "error: to many arguments" >&2
usage
exit 65
fi
TRAYTESTCMD="TrayOpenStatus"
if which "$TRAYTESTCMD" >/dev/null; then
is_open=`"$TRAYTESTCMD"`
else
is_open=0
fi
if [ "$is_open" -eq "1" ]; then
drutil tray close
else
drutil tray eject
fi