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)
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_
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.
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).