How to handle a Popup Button

What is the correct way to reference a menu item of a popup button?

In my AS Studio app I have a popup button called ‘presets’ where the last three menu items are; New…, Rename…, and Delete… and named ‘newPreset’, ‘renamePreset’ and ‘deletePreset’ respectively; When one of these items is chosen I want to open a window panel.

I have linked the popup button and its menuitems to my applescript, and IB inserted the ‘on choose menu item theObject’ handler into my script, where I added the following:

on choose menu item theObject
	if the name of theObject is equal to "newPreset" then
		display panel window "newPanel" attached to window "main"
	end if
end choose menu item

When I choose ‘New…’ from my popup button I get a “NSReceiverEvaluationScriptError: 3 (1)” error message, and I am guessing that there is something wrong with how I have referenced my controls. I also have “New…, Rename…, and Delete…” menu items in a ‘Presets’ menu of my main menu, and these are working correctly.

Any tips or pointers on working with popup buttons would very much be appreciated.

Use “title” instead of “name” to get the selection.

Thanks for the tip, it would probably be easier in the long run to refer to items by their title, rather than their applescript name, but unfortunately it didn’t solve my problem. The solution it seems, is to select the popup button, and check ‘will pop up’ as well as the the applescript file. Then I just saved and built my app from XCode and everything works the way I want.

Didn’t even think to check the ‘Will pop up’ box as it didn’t seem to be related to what I wanted to do. :oops:

Well this is very weird. Sometimes I build and things work fine, and other times, I will get the NSReceiverEvaluationScriptError message.

My code now looks like:

on choose menu item theObject
	if title of theObject is equal to "New..." then
		display panel window "newPanel" attached to window "main"
	else if title of theObject is equal to "Rename..." then
		display panel window "renamePanel" attached to window "main"
	else if title of theObject is equal to "Delete..." then
		display panel window "deletePanel" attached to window "main"
	end if
end choose menu item

I’m sure I have everything checked and named in IB; the window is set to wake from nib, the popup button has action and will popup checked, and each menu item has its choose menu item checked.

What I’m not sure about is whether I need to specifically reference the popup button in any way in my ‘on choose menu item’ hander.

I have NO IDEA if the fix I applied :

  • Will fix the problem you got.
  • Why I had to apply this fix (LOL sorry was some month ago).

Quote that I am using OSX 10.2.x and last version of Project Builder, NOT XCode Tools (that I am dowloading).

Quote few things I did in Interface Builder:

  • I gave in Interface Builder an Applescript name to the List choice itself.
  • This list choice is static, ie all items are added in Interface Builder.
  • I gave in Interface Builder an Applescript name to all the items of the list.
  • I connected to my script the event : “Menu->choose menu item” ONLY for the list not for each item (I am too lazy).

I strongly doubt I did everything right and that’s why I had to do the fix.

Perhaps you need to connect the event to the script for all items but not the list itself (then its a bug in events management from my point of view).

But everything I did seems to me fine :slight_smile: and seems to be what you did (I used name which is the clean way to do it if you ask me).

Anyway as you seem to still have the problem, here the code. I don’t advise to use it but you could try to see if this fix your problem. This sample code is for a script that manages the “on choose menu item” event for only one list:


on choose menu item theObject
	-- Fix Begin
	if name of theObject is "ASNameOfTheListNotOfAnyItem" then
		set theObject to current menu item of theObject
	end if
	-- Fix End
	try
		if name of theObject is "ASItemName1" then
			mySelectItem1(theObject)
		else if name of theObject is "ASItemName2" then
			mySelectItem2(theObject)
		else
			-- A trace just do be sure
			log "choose menu item not managed for " & name of theObject
		end if
	-- A "try... on error" just to use my show_error "function"
	on error error_message number error_number
		show_error(error_message, error_number)
	end try
end choose menu item

Well well, I edited a window and put many components inside a tab view item an then got again the error!!

I end to drop my lazyness and connected to my script the event : “Menu->choose menu item” FOR ALL ITEMS but not the list itself.

After that I didn’t got the error anymore (one more time!) I hope this will stay like that for few month ;-).

I quoted weird thing as I had 2 lists but only one was connected as mentionned in previous post. Despite that, I was receiving the select menu item event for this second list too! LOL.

All that done on OSX 10.2.x and project builder/interface builder.

For me the error occured because the selected item wasn’t “missing value” but wasn’t an object agree to :

  • name of the Object
  • and other stuff like that.
    Little bastard! :wink: