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


Click here to return to the 'Use disktool and System Profiler to eject stubborn CDROM discs' hint
The following comments are owned by whoever posted them. This site is not responsible for what they say.
Use disktool and System Profiler to eject stubborn CDROM discs
Authored by: Eravau on Dec 11, '03 06:14:05PM
Save the following perl script where you like, make it executable, and then run it from the command line to find an ATA/IDE CD and eject it. It will eject the first CD it finds and then exit...although with a little more work, it could continue to eject disks until they're all ejected. I only have ATA/IDE drives, so I can't test with other kinds...but it should work with others since it's checking the IDE, USB, and FireWire buses. If you find that it doesn't, please email me a spit-out of your /usr/sbin/system_profiler SPIDEDataType SPUSBDataType SPFireWireDataType and I'll work on any necessary modifications. This script will only work on OS X 10.3 Panther. The AppleSystemProfiler from pre-10.3 does not provide the necessary BSD name to make this work...and doesn't appear to take any of the flags of the 10.3 system_profiler.

--- Script Begins Below This line ---


#!/usr/bin/perl

#  Get The Info For All IDE, USB, & FireWire Devices
# ---------------------------------------------------
@profile_data = `/usr/sbin/system_profiler SPIDEDataType SPUSBDataType SPFireWireDataType`;

#  Misc. Settings
# ----------------
$no_of_lines  = @profile_data;
$line_no      = 0;

#  Find The First CD/DVD and Eject It
# ------------------------------------
while ($line_no <= $no_of_lines) {
	$profile_line = $profile_data[$line_no];
	if ($profile_line =~ /Drive Type.* CD-R/) {
		++ $line_no;
		while ($line_no <= $no_of_lines) {
			$profile_line = $profile_data[$line_no];
			if ($profile_line =~ /BSD Name.* (disk\S+)/) {
				$bsd_name = $1;
				print "Ejecting $bsd_name\n";
				system ("/usr/sbin/disktool -e $bsd_name > /dev/null");
				exit;
			}
			++ $line_no;
		}
	}
	++ $line_no;
}
print "No disk found to eject.\n";
--- Code ends above this line ---

If you want a double-clickable, keyboard-shortcutable, etc. version, you can wrap it in the following AppleScript (which assumes you saved the perl script at /usr/local/bin/eject_disk):
--- Code begins below this line ---


do shell script "/usr/local/bin/eject_disk"
set resp to the result
display dialog (resp as text) buttons {"OK"} default button "OK"
--- Code ends above this line ---

If you just want the AppleScript to eject and not tell you what it's doing, then take out the display dialog... line. If you want the perl script to shutup from the command line, just comment out any print lines (or take them out altogether).

-Eravau
See my Photo of the Day

[ Reply to This | # ]

Use disktool and System Profiler to eject stubborn CDROM discs
Authored by: pmccann on Dec 11, '03 07:32:08PM
Just a quick suggestion: instead of chasing line numbers (that you're not really interested in anyway), maybe just set a flag for when you've hit the "CD-R" line, then search for the next "BSD Name" line after that. Here's a rewrite of the script with just those changes.

#!/usr/bin/perl
use warnings;
use strict;
#  Get The Info For All IDE, USB, & FireWire Devices
# ---------------------------------------------------
my @profile_data = `/usr/sbin/system_profiler SPIDEDataType SPUSBDataType SPFireWireDataType`;
my $found;

#  Find The First CD/DVD and Eject It
# ------------------------------------
foreach (@profile_data){
$found=1 if /Drive Type.* CD-R/;
if ($found && /BSD Name.* (disk\S+)/){
my $bsd_name=$1; 
print "Ejecting $bsd_name\n";
system ("/usr/sbin/disktool -e $bsd_name > /dev/null");
exit;
}
}
print "No disk found to eject.\n";
Cheers, Paul

[ Reply to This | # ]
Use disktool and System Profiler to eject stubborn CDROM discs
Authored by: ber on Dec 11, '03 09:28:39PM

If your CD isn't mounted for some reason (like - I thought there was a CD in there but I guess not) this program will eject the next BSD device it finds. In my case it was a firewire disk.



[ Reply to This | # ]
Use disktool and System Profiler to eject stubborn CDROM discs
Authored by: Eravau on Dec 12, '03 09:47:11AM
Ok...it took me a few minutes to realize what the problem was (i.e. that there is no BSD name for a disk that's not there, so it just went 'til it found any BSD name after finding a removable media drive). Here's the re-write (on top of pmccann's edits) with the appropriate modification to stop it from hitting the wrong disk when there's no removable media in the first drive it hits...


#!/usr/bin/perl

use warnings;
use strict;

#  Get The Info For All IDE, USB, & FireWire Devices
# ---------------------------------------------------
my @profile_data = `/usr/sbin/system_profiler SPIDEDataType SPUSBDataType SPFireWireDataType`;
my $found;

#  Find The First CD/DVD and Eject It
# ------------------------------------
foreach (@profile_data) {
	$found = 1 if /Drive Type.* CD-R/;
	$found = 0 if ($found && /^\S/);
	if ($found && /BSD Name.* (disk\S+)/){
		my $bsd_name=$1; 
		print "Ejecting $bsd_name\n";
		system ("/usr/sbin/disktool -e $bsd_name > /dev/null");
		exit;
	}
}
print "No disk found to eject.\n";

Sorry about the problems there.

[ Reply to This | # ]
Use disktool and System Profiler to eject stubborn CDROM discs
Authored by: ber on Apr 01, '04 11:34:11PM

No harm at all. Thanks for the update. If I knew how to get mail notification on responses to things I post I'd have checked back here long ago. Appreciate your efforts.

brian



[ Reply to This | # ]