 |
 |
Eject CD with Cocoa
|
 |
|
 |
|
Fresh-Faced Recruit
Join Date: Jul 2001
Location: Germany
Status:
Offline
|
|
Hello,
it seems to be quite a difficult problem, to eject a CD without manual user interaction.
The problem in deep is following:
All Carbon and Classic Toolbox routines seem not to work (i tried all i suppose), the same way of course the AppleScript Put Away donīt works either.
As well there isnīt a UNIX eject command (the one and only UNIX distribution iknow, which hasnīt got this command per default)
so i figured out a cocoa class, which i tried to, here my code snippet:
#import <Foundation/Foundation.h>
#import <AppKit/AppKit.h>
int main (int argc, const char * argv[])
{
BOOL *myBool;
NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];
NSWorkspace * mySpace = [[NSWorkspace alloc] init];
myBool = [mySpace unmountAndEjectDeviceAtPath:@"/Volumes/NEU"];
NSLog(@"Hello, World!", myBool);
[pool release];
return myBool;
}
So to me the syntax seems quite right, the code compile quite fine, and should unmount and eject the media mounted under /Volumes/NEU (Where "NEU" is the name of the CD).
Actually it return 1 and no error, but the CD keeps staying in the drive.
If i change the path to a non existing mount point, i get the return value of 0, which is quite OK to me.
Any idea ?

|
|
|
| |
|
|
|
 |
|
 |
|
Junior Member
Join Date: Apr 2001
Location: Seattle, WA, USA
Status:
Offline
|
|
You were very close!
<BLOCKQUOTE><font size="1"face="Geneva, Verdana, Arial">code:</font><HR><pre><font size=1 face=courier>
[[NSWorkspace sharedWorkspace] unmountAndEjectDeviceAtPath: path];
</font>[/code]
You don't get to create your own instance of workspace, you have to share the ONE singleton instance created by Cocoa.
|
|
|
| |
|
|
|
 |
|
 |
|
Fresh-Faced Recruit
Join Date: Jul 2001
Location: Germany
Status:
Offline
|
|
Originally posted by tom_waters:
<STRONG>You were very close!
<BLOCKQUOTE><font size="1"face="Geneva, Verdana, Arial">code:</font><HR><pre>& amp;lt;font size=1 face=courier>
[[NSWorkspace sharedWorkspace] unmountAndEjectDeviceAtPath: path];
</font></pre><HR></BLOCKQUOTE>
You don't get to create your own instance of workspace, you have to share the ONE singleton instance created by Cocoa.</STRONG>
Thanks a lot for your reply, i figured it out and replaced the mismathed line like
myBool = [[NSWorkspace sharedWorkspace] unmountAndEjectDeviceAtPath:@"/Volumes/NEU/"];
but unfortunately the CD will stay in the drive
|
|
|
| |
|
|
|
 |
|
 |
|
Dedicated MacNNer
Join Date: Jan 2001
Location: Virginia, US
Status:
Offline
|
|
Hm. volume mounting/unmounting/ejecting is all part of the DiskArbitration framework, for which the APIs are all private at this point (though you can get the code from Darwin). I would think the NSWorkspace method would use DiskArbitration though... in which case, it's possible that something else has a reference to the volume and won't let you unmount it. Forcing the unmount is likely a bad idea.
Try using
/usr/sbin/disktool -e diskX 0
from the command line,
where X is the disk number for that volume in /dev. If you type 'mount' from the command line, you'll see the mapping between the device's path and the mounted filesystem -- for a line like "/dev/disk1s9 on /Volumes/NEU", then you want "disk1" as the argument to disktool.
If that doesn't work, something else doesn't want you to unmount the volume, and so it may not be the best idea to try. If it does work, then likely there's a bug in the NSWorkspace method, and I'd cop out by just invoking disktool with NSTask :-) [And maybe file a bug report with Apple.]
For other avenues, you could try the BSD APIs, i.e. open the device (fd = open("/dev/disk1", O_RDONLY)) and send the eject ioctl to it. Look in /usr/include/dev/disk.h. This may eject the device without unmounting the volume first though, which would leave things in an inconsistent state...
|
|
|
| |
|
|
|
 |
|
 |
|
Fresh-Faced Recruit
Join Date: Jul 2001
Location: Germany
Status:
Offline
|
|
Thanks a lot, You really did help me out!
|
|
|
| |
|
|
|
 |
|
 |
|
Fresh-Faced Recruit
Join Date: Jul 2001
Location: Germany
Status:
Offline
|
|
Finally here the single lineof code to eject a cd with given name:
[[NSWorkspace sharedWorkspace] unmountAndEjectDeviceAtPath:@"/Volumes/CDNAME"];
where CDNAME must be the Name of the CD as shown in the Finder
(listed under /Volumes) 
|
|
|
| |
|
|
|
 |
|
 |
|
Addicted to MacNN
Join Date: Mar 2000
Location: London, UK
Status:
Offline
|
|
Didn't you just say that that DOESN'T work a few lines up?! 
|
|
|
| |
|
|
|
 |
|
 |
|
Fresh-Faced Recruit
Join Date: Jul 2001
Location: Germany
Status:
Offline
|
|
I supposed so, because the code i referred to did not work.
The AppKit seems to be a little buggy, as so it is not easy to become what is documented.
As I am not that experienced in Cocoa, it is quite important to figure out those problems, and of course how to pass by...
Thank you all for your replies 
|
|
|
| |
|
|
|
 |
 |
|
 |
|
|
|
|
|

|
|
 |
Forum Rules
|
 |
 |
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
|
HTML code is Off
|
|
|
|
|
|
 |
 |
 |
 |
|
 |
|