PDA

View Full Version : Is it possible to get a NSTextDidChangeNotification in Xcode Cocoa-Applescript App?




jpefjr
Dec 11, 2011, 11:53 AM
I was wondering if it was possible for a Cocoa-Applescript app to receive NSTextDidChangeNotifications? I've looked at the NSNotificationCenter documentation and specifically at the addObserver:selector:name:object: method but don't see how I could get the parameters for it.

I assume I'd need something like this:

tell defaultCenter() of current application's NSNotificationCenter
addObserver_selector_name_object_(...)
end tell


But I'm not even sure it's possible to pass an Applescript function as an observer. And if so is there a syntax I could use for the textDidChange selector? And would I use the following for the name parameter?:
current application's NSTextDidChangeNotification
And the object parameter would be the property that I associated with the NSTextField in Interface Builder?

And just in case I'm looking at the problem wrong, I have a "Connect" button that I want to keep disabled until the user has at least typed something into both the username and hostname fields. If I could get the textDidChange notifications for those text fields I could check and see if both fields are not empty and then call connectButton's setEnabled_(true). Is there a better way to go about doing that?



Red Menace
Dec 11, 2011, 01:23 PM
You can add a delegate method to get the notification - see the NSTextDelegate Protocol Reference (http://developer.apple.com/library/mac/#documentation/cocoa/Reference/NSTextDelegate_Protocol/Reference/Reference.html). You might also be able to bind the value of the text field to the button's enabled property.

jpefjr
Dec 11, 2011, 02:09 PM
You can add a delegate method to get the notification - see the NSTextDelegate Protocol Reference (http://developer.apple.com/library/mac/#documentation/cocoa/Reference/NSTextDelegate_Protocol/Reference/Reference.html). You might also be able to bind the value of the text field to the button's enabled property.

Thanks for the reply. I know I could add a delegate in an objective-C program but I'm writing an Applescript program so I can't figure out how to add the delegate.

Partron22
Dec 11, 2011, 02:40 PM
Going thru NSWorkspace to get the NSNotificationCenter works for disk events:set ws to current application's NSWorkspace's sharedWorkspace()
set pNotificationCenter to ws's notificationCenter()

if selector is equal to "add" then
pNotificationCenter's addObserver_selector_name_object_(me, "diskMountedUnmountedorRenamed_:", "NSWorkspaceDidMountNotification", missing value)
end if
That beautifully titled 'diskMountedUnmountedorRenamed_:' is a straight Applescript function.on diskMountedUnmountedorRenamed__(sender)
beep ()
end diskMountedUnmountedorRenamed__
You may have to fiddle with the number of args the Applescript function takes depending on the notification you're after.

Red Menace
Dec 11, 2011, 03:09 PM
I know I could add a delegate in an objective-C program but I'm writing an Applescript program so I can't figure out how to add the delegate.

You connect the delegate to your script object the same as in Objective-C. The methods are also the same, just the naming conventions are a little bit different - refer to the AppleScriptObjC Release Notes (http://developer.apple.com/library/mac/#releasenotes/ScriptingAutomation/RN-AppleScriptObjC/_index.html) for translating the method definitions, for example:

on textDidChange_(aNotification)
-- do your stuff
end textDidChange_

jpefjr
Dec 11, 2011, 03:14 PM
Going thru NSWorkspace to get the NSNotificationCenter works for disk events
<SNIP>
You may have to fiddle with the number of args the Applescript function takes depending on the notification you're after.

You connect the delegate to your script object the same as in Objective-C. The methods are also the same, just the naming conventions are a little bit different - refer to the AppleScriptObjC Release Notes (http://developer.apple.com/library/mac/#releasenotes/ScriptingAutomation/RN-AppleScriptObjC/_index.html) for translating the method definitions, for example:
<SNIP>

Thanks guys. I'm just blown away by how powerful and flexible Applescript. Time to go play with my program. :)

jpefjr
Dec 11, 2011, 06:11 PM
Well, I can't seem to get notifications.

In my applicationWillFinishLanching_() I have:


property hostnameTextBox : missing value
.
.
.
set ws to current application's NSWorkspace's sharedWorkspace()
set pNotificationCenter to ws's notificationCenter()
pNotificationCenter's addObserver_selector_name_object_(me, "textChanged_:", "NSTextDidChangeNotification", hostnameTextBox)


And then after that I have:

on textChanged__(sender)
log "Text Changed"
end textChanged__


hostnameTextBox is the Referencing Outlet setup in IB by ctrl-dragging from the App Delegate box to the text field.

Any suggestions as to what I'm doing wrong? Thanks!

Red Menace
Dec 11, 2011, 06:45 PM
You have an extra underline in the selector parameter (the selector uses the same naming as the equivalent Objective-C method, i.e. textChanged:), but all you should have to do is connect the text field's delegate outlet to your AppDelegate and add a on textDidChange_(aNotification) handler.

chown33
Dec 11, 2011, 06:55 PM
I have this URL in my file of Frequently Pasted URLs:
http://www.macosxautomation.com/applescript/apps/

It's for a book on AppleScriptObjC ($30 USD), and also has links to some tools and stuff.

I also have this URL for an MR thread on it:
http://forums.macrumors.com/showthread.php?t=845454

Red Menace
Dec 11, 2011, 07:27 PM
oops, after looking at one of my old projects I had a bit of a goof there, and must apologize for sending you in the wrong direction - I referred you to the NSText Delegate stuff instead of the NSTextField Delegate stuff. NSObject has a controlTextDidChange: method that should be what you are looking for, so by connecting your text field's delegate outlet to the AppDelegate and adding

on controlTextDidChange_(aNotification)
log "controlTextDidChange_"
end textDidChange_

you should get a notification when the text field changes.

jpefjr
Dec 11, 2011, 11:17 PM
oops, after looking at one of my old projects I had a bit of a goof there, and must apologize for sending you in the wrong direction - I referred you to the NSText Delegate stuff instead of the NSTextField Delegate stuff. NSObject has a controlTextDidChange: method that should be what you are looking for, so by connecting your text field's delegate outlet to the AppDelegate and adding

on controlTextDidChange_(aNotification)
log "controlTextDidChange_"
end textDidChange_

you should get a notification when the text field changes.

Red Menace,
thank you very much - that's got it! I did as you said and created a controlTextDidChange_() method, used IB to connect the delegate of the my NSTextField to the AppDelegate and sure enough, every time I type into the text field the controlTextDidChange_() function gets called.

Thanks again for all the help.