Adding an observer

I would like to monitor when the bounds of a window have changed.

Is it necessary to do this using objective-c?

I know this works in Cocoa and need a little advice for ASOC.

Can anyone help please.



	NSNotificationCenter *nc = [NSNotificationCenter defaultCenter];
	[nc addObserver:self selector:@selector(boundsChanged) name:NSViewFrameDidChangeNotification object:nil];



Thanks

Terry

I have not been able to get anything working in ASOC that requires a constant.
eg “name:NSViewFrameDidChangeNotification”

I would put this in an Objective-C class, create a pointer from the Objective-C class in IB to an instance of the AppleScript file, set the @selector to a method in the Objective-C file that calls the on handler in the AppleScript file. Unless you can handle it the Objective-C class.

I’ve used notifications OK. In this case it was NSWorkspace’s notification center, but the principal should be the same:

At top level of script:

property workspaceClass : class “NSWorkspace”

Inside script object:

	set ws to workspaceClass's sharedWorkspace()
	set nc to ws's notificationCenter()
	tell nc to addObserver_selector_name_object_(me, "activeAppHasChanged:", "NSWorkspaceDidDeactivateApplicationNotification", missing value)

Thanks for your response.

Unable to try it as I am at work.

Will try tonight and post feedback.

–Terry

Nice Shane. I had not thought of putting the constant in quotes.

Cheers,

Craig

Hi,

Not tried it yet but picking up on the constant statement and I tried this:


tell pImage to drawAtPoint_fromRect_operation_fraction_(newPoint, pImageRect, "NSCompositeSourceOver", pOpacity)

Failure.

Back to the drawing board.

–Terry

Hi all,

This now seems to be working in an NSView, thanks Shane:



property pNotificationCenter : class "NSNotificationCenter"

on awakeFromNib()
		
    tell me to setPostsFrameChangedNotifications_(true)

    set pNotificationCenter to pNotificationCenter's defaultCenter

    tell pNotificationCenter to addObserver_selector_name_object_(me, "boundsChanged:", "NSViewFrameDidChangeNotification", missing value)
			
end awakeFromNib


on boundsChanged_()
    beep
end boundsChanged_



All the best

Terry

The docs say the name is an NSString, so I assumed it had to be text.

Has anyone any idea why this doesn’t work?

property NSNotificationCenter : class "NSNotificationCenter"

script myappAppDelegate
	property parent : class "NSObject"
	
	on applicationWillFinishLaunching_(aNotification)
		-- Insert code here to initialize your application before any files are opened
		set defaultNotificationCenter to current application's NSNotificationCenter's defaultCenter()
		tell defaultNotificationCenter to addObserver_selector_name_object(me, "preferencesChanged:", "VAPreferencesChanged", missing value)
	end applicationWillFinishLaunching_
	
	on applicationShouldTerminate_(sender)
		-- Insert code here to do any housekeeping before your application quits 
		return current application's NSTerminateNow
	end applicationShouldTerminate_
	
	on preferencesChanged_(aNotification)
		log "Update"
	end preferencesChanged_
end script

I get the error: unrecognized selector sent to instance

Well it seems that I sent my selector right, right?

In Objective-C my selector should be defined as @selector(“preferencesChanged:”) to tell the compiler to look for this selector. Looking at the example above it isn’t necessary but maybe I’m doing something else wrong. I looked in aaron hillegass’s book and shane stanley’s book (I bought yesterday) but I can’t find anything I do wrong.

Sorry guy’s to bother you with my dumb question.

the line

 tell defaultNotificationCenter to addObserver_selector_name_object(me, "preferencesChanged:", "VAPreferencesChanged", missing value)

must be:

 tell defaultNotificationCenter to addObserver_selector_name_object_(me, "preferencesChanged:", "VAPreferencesChanged", missing value)

I forgot the underscore at the end of addObserver_selector_name_object_

I just overlooked it completely

p.s. I’m staring to enjoy ASOC more and more

Als, you wrote:

and:

You can either delete the first, or delete the “current application’s” from the second – there’s no need for both.

In this case you can actually replace the constant. The expressio represents an integer constant. I can’t look it up right now, but it should be in the documentation. And if it isn’t there, try to grep it in the AppKit framework’s headers. If you find it, you should be able to replace NSCompositeSourceOver by the numeric constant (e.g. 6).

I think you can use: current application’s NSCompositeSourceOver

Ric