NSWorkspaceDidMountNotification

Hello everyone,

I’m trying to wrap my head around AppleScriptObjC at the moment (which, quite frankly, I do have some problems with).

The application is supposed to perform an action on a thumb drive which is being attached (those drives are pre-formatted). However I’m stuck at getting it to receive the Notifications in question. The diskMounted_ method doesn’t seem to get called when I insert a SD Card.

Please have a look at the code:


	property pNotificationCenter : class "NSWorkSpace"
	
	on applicationWillFinishLaunching_(aNotification)
		mainWindow's registerForDraggedTypes_({"public.directory", "public.data"})
		
		
		set pNotificationCenter to application's NSWorkspace
		tell pNotificationCenter's sharedWorkspace() to addObserver_selector_name_object_(me, "diskMounted:", "NSWorkspaceDidMountNotification", missing value)
	end applicationWillFinishLaunching_

	on diskMounted_(sender)
		tell application "Finder" to display dialog (sender as string)
	end diskMounted_


I’m quite sure you can help me - thanks in advance.

Felix

Try this:

property parent : class "NSObject"
	property pNotificationCenter : missing value
	property mainWindow : missing value -- IBOutlet for the main window
	
	on applicationWillFinishLaunching_(aNotification)
		mainWindow's registerForDraggedTypes_({"public.directory", "public.data"})
		set ws to current application's NSWorkspace's sharedWorkspace()
		set pNotificationCenter to ws's notificationCenter()
		pNotificationCenter's addObserver_selector_name_object_(me, "diskMounted:", "NSWorkspaceDidMountNotification", missing value)
	end applicationWillFinishLaunching_
	
	on diskMounted_(sender)
		log sender
	end diskMounted_

You have to get the sharedWorkspace instance first, which is done with the “set ws to …” line.
You have “tell pNotificationCenter’s sharedWorkspace()” which is backwards. The notification center belongs to the shared workspace, not the other way around. Finally, the display dialog line didn’t work, because the coercion to a string didn’t work. You can log it to the console however, which will show you what kind of information is returned.

Ric

Thank you very much for your help, this works perfectly!