views:

43

answers:

1

I had a bookmark which described the process on how to do this - finding the name of a mounted CD in OS X - but I deleted the bookmark when I reformatted my Mac. :P

Reading up on the subject, this is what I think might work. Basically, I need to verify if a particular CD is mounted before continuing in the application

  1. Access NSWorkspace
  2. Perform 'checkForRemovableMedia'
  3. Grab array of mounted media paths from 'mountedRemoveableMedia'
  4. Run through array of mounted media paths to find one containing name of targeted disc

Anyway, this is what I've came up with as a possible solution. Anyone else have any other ideas/knowledge in this area in Cocoa? Suggestions :)

EDIT: I made this code below, but isn't working. It creates an NSCFArray which contains NSCFStrings, which I read up and shouldn't be doing.

 NSArray *mountedItems = [[NSWorkspace sharedWorkspace] mountedRemovableMedia];

 int count = [mountedItems count];
 int i = 0;

    for (i = 0; i < count; i++) {
         //line is not printing.  contains NSCFArray and NSCFStrings
            [NSLog print:[[mountedItems objectAtIndex:i] stringValue]];
 }
A: 

OK, so I'm an idiot.

[[NSWorkspace sharedWorkspace] checkForRemovableMedia];
NSArray *mountedItems = [[NSWorkspace sharedWorkspace] mountedRemovableMedia];

NSUInteger count = [mountedItems count];
NSUInteger i = 0;
for (i = 0; i < count; i++) {
    NSString *tempString = [mountedItems objectAtIndex:i];
    NSLog(@"%@",tempString);
}

I was not only using NSLog wrong, but completely didn't even realize that perhaps calling 'stringValue' on a string is redundant. And also what caused the code to break. :P

This works now; I also added 'checkForRemovableMedia' as an extra precaution.

Jeffrey Kern
“I also added 'checkForRemovableMedia' as an extra precaution.” No need; it currently does nothing and is deprecated since Snow Leopard. http://developer.apple.com/mac/library/documentation/Cocoa/Reference/ApplicationKit/Classes/NSWorkspace_Class/DeprecationAppendix/AppendixADeprecatedAPI.html#//apple_ref/occ/instm/NSWorkspace/checkForRemovableMedia
Peter Hosey