How to detect the cancel button in a search field?

Do you know how when you have a search field, when you enter some text into it, a little “x” button shows up? The “x” button, when pressed, clears the text from the search field. After clearing the search field the button disappears.

How do you detect that button? I tried an on-clicked handler for the search field but that didn’t work. Any thoughts??? I want an action performed when it is clicked.

Although I’m not very familiar with using Cocoa methods, my first guess would be to somehow get access to one of the Cocoa methods/events.

These, for example, looks interesting:

http://developer.apple.com/documentation/Cocoa/Reference/ApplicationKit/Classes/NSSearchFieldCell_Class/Reference/Reference.html#//apple_ref/doc/uid/20001918-CJBHGGJD

http://developer.apple.com/documentation/Cocoa/Reference/ApplicationKit/Classes/NSSearchFieldCell_Class/Reference/Reference.html#//apple_ref/occ/instm/NSSearchFieldCell/cancelButtonCell

I know roughly how the “call method” command works, but it would seem like you need access to react to a click event for the cancelButtoncell, if such a thing exists.

Hope some of the Cocoa-experienced people here will be able to help more.

Thanks, I was looking through those as you were posting. There was noting there that could help me. Out of desperation I tried a few but they didn’t do what I wanted. There’s nothing to differentiate the cancel button event from the regular action that’s sent when using the search field itself.

At least I learned it’s called the cancel button rather than the little “x”! :smiley:

property SearchField : null
property SearchFieldCancel : null

script onClicked
	on clicked theObject
		if theObject is SearchFieldCancel then
			set content of SearchField to "" --> Clear the field
			(* Do more stuff here *)
		end if
	end clicked
end script

on awake from nib theObject
	if name of theObject is "SearchField" then
		set SearchField to theObject
		
		set SearchFieldCancel to (call method "cancelButtonCell" of (call method "cell" of theObject))
		set script of SearchFieldCancel to onClicked
	end if
end awake from nib

That’s really cool jobu but it’s not working. When I click the cancel button it does go into the script onClicked, but it doesn’t go into the “if theObject is SearchFieldCancel then” section. Which doesn’t make sense because obviously the object SearchFieldCancel exists otherwise it would never get to the script onClicked.

So I commented out the if statement so it would go straight to the commands. Then I get an error: can’t set content of null to “”. So it isn’t even seeing the object searchField. But I know that object exists because my search field is performing searches and I can log the searchField object in the ‘awake from nib’ handler.

So somehow the properties searchField and searchFieldCancel aren’t being seen by the script object.

Any ideas?

property SearchFieldCancel : null

script onClicked
	on clicked theObject
		if name of theObject is "SearchFieldCancel" then
			set content of control view of theObject to ""
			(* Do more stuff here *)
		end if
	end clicked
end script

on awake from nib theObject
	if name of theObject is "SearchField" then
		set SearchFieldCancel to (call method "cancelButtonCell" of (call method "cell" of theObject))
		set name of SearchFieldCancel to "SearchFieldCancel"
		set script of SearchFieldCancel to onClicked
	end if
end awake from nib