Fun with NSCantCreateScrptCmdErr & NSRcvrEvaluationScrpt

I was given someone else’s Xcode project with a rather large AppleScript attached that has been compiled and built but never successfully executed. A problem I’ve come up against in trying to get this running is that I’m unable to get a valid value from this expression:

title of current menu item of theObject

where theObject is a popup button. If I create a dummy project with nothing but a popup button, then in the awake from nib handler I can get the title successfully, but in this larger project I get either NSCannotCreateScriptCommandError (10) or NSReceiverEvaluationScriptError: 3 (1).

I tried a couple things: (1) Build->Clean, then a rebuild, (2) dividing it up into two statements: set cmi to current menu item of theObject, and set ttl to title of cmi, and (3) replacing “title” with «class titl». Believe it or not, IIRC it worked the first time after each of these, but thereafter it reverted to producing one of the above error messages, and after a while it wouldn’t work at all, even after any of those 3 flailing attempts.

Does anyone have any ideas? Is there something specific I should look for anywhere in the project?

Thanks very much…

  • Dan

Make sure that the ‘choose menu item’ handler is only attached to the popup button, NOT the items inside of it. That’s the only way to catch the current menu item. If you try to get the current menu item of a menu item… well, there isn’t one… so you’ll get an error. Also, the connections between individual menu items in popup buttons and the CMI handler can be flaky sometimes. It’s best to use the CMI handler for the popup button, and use it’s current menu item name as a test to identify the selection… rather than linking every individual item to the handler.

At the top of my choose menu item handlers, I usually use ‘try’ blocks to gather any information I can about the current menu item (this assumes all items are named). Then, if you need them later they’re already collected. By using the try statement, it errors out gracefully when it’s handling a menu item, allowing you to use the handler for both menu’s AND menu items, while keeping the code neat and tidy. For example…

on choose menu item theObject
	try
		set objectName to name of current menu item of theObject
	end try
	try
		set objectTitle to title of current menu item of theObject
	end try

	if name of theObject is "exampleMenuItem" then
		log "A menu item was selected"
	else if name of theObject is "exampleMenu" then
		set logString to (("Name:" & objectName & "; Title:" & objectTitle) as string)
		log logString
	end if
end choose menu item

j

Thanks jobu. I verified that it is indeed attached only to the popup button, and I check the class of theObject at the top of the CMI handler and find that it’s “popup button”.
I like the try block idea, but I’m getting a failure on both of these tries:

try
	set objectName to name of current menu item of theObject
on error errmsg1
end try
try
	set objectTitle to title of current menu item of theObject
on error errmsg2
end try

with NSCannotCreateScriptCommandError (10) popping up for both of these statements. If I get name of theObject it does return the correct name, so I know I’m looking at the right control. Arghh…
Thanks very much…

  • Dan