• Open Menu Close Menu
  • Apple
  • Shopping Bag
  • Apple
  • Mac
  • iPad
  • iPhone
  • Watch
  • TV
  • Music
  • Support
  • Search apple.com
  • Shopping Bag
 

Lists

Open Menu Close Menu
  • Terms and Conditions
  • Lists hosted on this site
  • Email the Postmaster
  • Tips for posting to public mailing lists
Re: Getting Carbon Volume Mount Events in a Cocoa (Core Foundation) process
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: Getting Carbon Volume Mount Events in a Cocoa (Core Foundation) process


  • Subject: Re: Getting Carbon Volume Mount Events in a Cocoa (Core Foundation) process
  • From: Aaron Burghardt <email@hidden>
  • Date: Tue, 9 Oct 2007 20:47:47 -0400

If you don't mind linking against the AppKit, you can use NSWorkspace manager without an instance of NSApplication:

#import <Cocoa/Cocoa.h>

@interface Listener : NSObject
- (void)volumeDidMount:(NSNotification *)notif;
- (void)volumeDidUnmount:(NSNotification *)notif;
@end

@implementation Listener
- (void)volumeDidMount:(NSNotification *)notif
{
NSLog(@"Volume mounted: %@", [[notif userInfo] valueForKey:@"NSDevicePath"]);
}
- (void)volumeDidUnmount:(NSNotification *)notif
{
NSLog(@"Volume unmounted: %@", [[notif userInfo] valueForKey:@"NSDevicePath"]);
}
@end

int main( int argc, char *argv[]) {


	NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];

Listener *listener = [[Listener alloc] init];

NSWorkspace *sharedWorkspace = [NSWorkspace sharedWorkspace];

[[sharedWorkspace notificationCenter] addObserver:listener selector:@selector(volumeDidMount:) name:NSWorkspaceDidMountNotification object:nil];
[[sharedWorkspace notificationCenter] addObserver:listener selector:@selector(volumeDidUnmount:) name:NSWorkspaceDidUnmountNotification object:nil];

NSRunLoop *runLoop = [NSRunLoop currentRunLoop];
[runLoop runUntilDate:[NSDate dateWithTimeIntervalSinceNow:60.0]];


	[listener release];
	[pool release];
	exit(0);
}

compile and run:

$ gcc -framework Cocoa testworkspace.m -o testworkspace
$ ./testworkspace

While this is was running, I used diskutil to mount and unmount a thumbdrive (without physically removing it) in another Terminal window:

$ diskutil unmount disk1s1
Volume Lexar on disk1s1 unmounted
$ diskutil mount disk1s1
Volume Lexar on disk1s1 mounted

and received this output from testworkspace:

2007-10-09 20:34:35.070 testworkspace[4960:10b] Volume unmounted: / Volumes/Lexar
2007-10-09 20:34:43.246 testworkspace[4960:10b] Volume mounted: / Volumes/Lexar


HTH,
----
Aaron Burghardt
email@hidden

On Oct 9, 2007, at 5:40 PM, James Bucanek wrote:

email@hidden <mailto:email@hidden> wrote (Tuesday, October 9, 2007 12:29 PM -0400):

NSWorkspace posts notifications when volumes are mounted and unmounted
and includes the device path. If you need more info, you could
combine that with the information provided by the disk arbitration
framework.

As I mentioned in my original posting:

James Bucanek <mailto:email@hidden> wrote (Monday, October 8, 2007 10:45 PM -0700):
I'm trying to get the Carbon volume mount and unmount events in a Foundation process (daemon).

NSWorkspace isn't available in a Foundation application. I wish it was. It would solve a *lot* of problems.


--
James Bucanek



_______________________________________________

Cocoa-dev mailing list (email@hidden)

Please do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com

Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden


  • Follow-Ups:
    • Re: Getting Carbon Volume Mount Events in a Cocoa (Core Foundation) process
      • From: James Bucanek <email@hidden>
References: 
 >Re: Getting Carbon Volume Mount Events in a Cocoa (Core Foundation) process (From: James Bucanek <email@hidden>)

  • Prev by Date: Re: Data Source ignored after binding sortDescriptors
  • Next by Date: Re: Drag an drop in a custom NSView
  • Previous by thread: Re: Getting Carbon Volume Mount Events in a Cocoa (Core Foundation) process
  • Next by thread: Re: Getting Carbon Volume Mount Events in a Cocoa (Core Foundation) process
  • Index(es):
    • Date
    • Thread