Help with notifications on text view updates

I’m just getting into a complex ASObjC project for the first time. I’m trying to trigger a handler/method when a scrolling text view is updated. The NSTextView has a NSTextDidChangeNotification, which I’m trying to trigger a method on when the text in the field is updated.

Here’s the relevant code that’s NOT working:

on applicationWillFinishLaunching_(aNotification)
        tell current application's NSWorkspace to set theWorkspace to sharedWorkspace()
        set notifCenter to theWorkspace's notificationCenter()
        tell notifCenter to addObserver_selector_name_object_(me, "didChangeTextNotification:", "NSTextDidChangeNotification", missing value)
end applicationWillFinishLaunching_

on didChangeTextNotification_(aNotification)
        log "controlTextDidChange_"
end didChangeTextNotification_

on applicationShouldTerminate_(sender)
		return current application's NSTerminateNow
end applicationShouldTerminate_

I have also bound the appropriate NSTextView to the script as a delegate.

When I run the code, I get no logged errors on setting up the observer, so that seems to be all right. But updating the text does not trigger the didChangeTextNotification_ handler.

Additionally, when I quit the app, I get this error from the stopObserving_ handler:


NSTextDidChangeNotification is sent by the standard NSNotificationCenter (not the one that belongs to NSWorkspace).
Setting the observer is actually not necessary, there is a delegate method as part of NSTextDelegate Protocol.

stopObserving must be a custom method, which is missing in the app delegate class

Thank you, StefanK!

My new code is MUCH simpler (provided here for fellow noobs):

on textDidChange_(aNotification)
        log "Text has changed"
        end textDidChange_

One additional question: How do I restrict this delegate handler to a specific text view, rather than all delegated views?

the object of the notification

aNotification's object()

contains the reference to the sender object

Thank you so much! It’s working great.

A follow-up question:

I’ve put this approach into practice on an NSTableView, so that I can handle some changes every time a text value is changed in a table row. Specifically, I’m removing the edited object’s old values from an array, and then adding in new values after it’s edited. These values are derived within the object referenced in the table, so I can’t just use the new text that’s typed in as-is.

The way I’m doing it now is to have a tableView_textShouldEndEditing delegate method that’s removing the old values before the changes are committed, and then a tableViewTextDidEndEditing method that inserts the new values.

This works, but I wonder if there’s an easier way.

I have tried to do this within a custom set method in the object, but I can’t figure out a way to reference the main app delegate’s script’s properties or methods. tell theDataArray, nor tell Current Application’s theDataArray, nor tell Current Application’s appNameAppDelegate’s theDataArray work for me. So that’s why I’m stuck with these delegate methods.

Are the two delegate methods the right way to handle this, or am I overlooking a simpler approach?

tell current application’s NSApp’s delegate() to…