Popup menus and getting name of theObject...

This is driving me crazy. I’m trying to do something when the user selects a menu item in a pop-up menu. All I get is an error.

on choose menu item theObject
	if name of theObject is "stuff" then
		display dialog "It works!"
	end if
end choose menu item

Uh… From what I know, that should have worked. Or maybe menus are different?

This is just off the top of my head, but I believe that “name” should be “title” i.e.:

if title of theObject is "stuff" then

Hope this helps,
Brad Bumgarner, CTA

Thanks :smiley:

I wonder why I can’t call it by it’s name…

You gave the menu item an AppleScript name and it still doesn’t work?

To what are you connecting the “choose menu item” handler? There are some conflicts, such as with popup buttons, having the handler attached to the menu item’s themselves. You need to connect the handler to the menu that the items are in instead, and then further evaluate which item was selected by looking at the current menu item that was selected.

on choose menu item theObject
	try --> If the item sending the action is a menu item, find out what the item's name is
		set MenuItem to (name of (current menu item of theObject)) as string
	end try
	
	if name of theObject is "Actions" then --> Was the 'actions' popup button selected.
		if MenuItem is "ImageFromFrame" then  --> Was a particular menu item selected
			--> do something
		end if
	end if
end on choose menu item

I have found in my experience that using the menu to send the choose menu item handler is much cleaner and easier to manage than by connecting the handler to the item’s themselves. Not only are there some conflicts, such as that found in the popup buttons, changing your items and managing them in your code is much easier. I also NEVER reference items of any kind by their title. Ugly and waiting for problems getting into that habit. Just give the items applescript names and use that as your references to everything.

j

Yes, I was connecting the handler to the menu item, not the popup menu itself. I guess i’ll fix that.

Same here. I just didn’t know why it the script wasn’t working. When Brad Bumgarner posted, I though that was the only way to do it.