Handling Menu Items

Why oh why doesn’t this work?!

on choose menu item theObject
	set objname to name of theObject as string
	
	if objname = "excuse selector" then
		set menu_selection to title of theObject
		
		if menu_selection = "Forgot Homework" then
			set visible of text field "input one" of window "main" to true
			set contents of text field "input one" of window "main" to "Assignment"
			set visible of text field "input two" of window "main" to true
			set contents of text field "input two" of window "main" to "Teacher's Name"
		end if
		
		if menu_selection = "shaved" then
			display dialog "pet"
		end if
		
		if menu_selection = "gas station" then
			display dialog "gas station"
		end if	
	end if
end choose menu item

You should be using “else if” statements rather than multiple if’s in this case. Even though you technically shouldn’t be able to get multiple matches for ‘menu_selection’, if you did it would throw an error because I’m pretty sure only one dialog can exist at a time. Also, I tend to use grammatical operators for strings (is, is equal to, is not, etc.) and arithmetic operators for numbers (=, <, ≠, etc.). It shouldn’t usually matter, but it cleans up the code, clarifies what sort of information you’re expecting to receive, and may help applescript better understand what you want it to interpret. If I were to clean up your code I’d start with…

on choose menu item theObject
	set objname to name of theObject
	set menu_selection to title of theObject

	if objname is "excuse selector" then
		if menu_selection is "Forgot Homework" then
			tell window "main"
				set visible of text field "input one" to true
				set contents of text field "input one" to "Assignment"
				set visible of text field "input two" to true
				set contents of text field "input two" to "Teacher's Name"
			end tell
		else if menu_selection is "shaved" then
			display dialog "pet"
		else if menu_selection is "gas station" then
			display dialog "gas station"
		end if
	end if
end choose menu item

Obviously you want to make sure your objects are named correctly and your connections are all made to match what your handlers are expecting.

j

I was having similar troubles (I think…) I had a popup button that on certain selections set text fields, and I thought it was the popup button… but I found it was the text fields. (or maybe I changed something else…)

try changing from setting contents to setting the string value ie:

set the string value of the text field “input_one” to “Teacher” – or whatever

Don’t count on this, but try it before you do something drastic.


I am personally in favor of the arithmetical expressions, but to each there own. However, I have found a good reason to use the grammatical expression instead. I have found that some of the expressions don’t work when used in Applescript Studio. Specifically ?, ?, ? don’t seem like they wan to compile. So I have started using the grammatical expression just so my code can be transferred to Studio.


I am personally in favor of the arithmetical expressions, but to each there own. However, I have found a good reason to use the grammatical expression instead. I have found that some of the expressions don’t work when used in Applescript Studio. Specifically ?, ?, ? don’t seem like they wan to compile. So I have started using the grammatical expression just so my code can be transferred to Studio.
[/quote]

The Greeks were hampered in their ability to computate by a lack of symbolic notation. The introduction of symbolic arithmetical expressions by the Arabs was an extraordinary breakthough in the history of math. It’s curious that Apple hasn’t learned the lessons of history… :wink:

Still, the popup button does nothing. Could anyone give me a quick tutorial on this, because I want to make sure I have all the proper connections and code. All I need is a script do do something when an item is selected from the popup button.

Thanks.

First, try commenting out all of the lines of code and then uncommenting them one (or a couple) at a time until you find out what code is not working. This will let us know which bit of code needs attention.

A quick ‘tutorial’…

  1. Create your window; applescript name ‘main’.
  2. Create your popup button; AS name ‘excuse selector’ (I usually prefer something like ‘ExcuseSelector’… but I’m a goober :rolleyes: )
  3. Connect ONLY the popup button’s “choose menu item” event to the script.

** NOTE: DO NOT connect the individual menu items to the choose menu item handler. This has been difficult to implement and buggy for me. If you’re getting the following error, it may be because you’re using the menu items’ events rather than that of the menu itself…

  1. Add the code below to your script, making sure to change the quoted parts that I’ve highlighted to match the exact title’s you’ve used.
on choose menu item theObject
	set objname to name of theObject
	set menu_selection to title of theObject

	if objname is "excuse selector" then --(Make sure the popup button is named "excuse selector" exactly
		--> Remember, everything you try to match below here...
		--> ...refers to the TITLE, not the AS name!
		if menu_selection is "Forgot Homework" then --("Forgot Homework" spelled exactly?
			tell window "main"
				set visible of text field "input one" to true
				set contents of text field "input one" to "Assignment"
				set visible of text field "input two" to true
				set contents of text field "input two" to "Teacher's Name"
			end tell
		else if menu_selection is "shaved" then --("shaved" spelled exacticly?
			display dialog "pet"
		else if menu_selection is "gas station" then --(...exactly?, etc....
			display dialog "gas station"
		end if
	end if
end choose menu

This is about as far as I can go with what you’ve provided. It seems that all of the code (at least that which I’ve provided) works fine. If it continues to misbehave, it is either in your implementation, or you are missing a step or misnaming items. Try just working with the first item and getting that to work without any bells and whistles. Then add in your more complex actions and more menu items, once you’ve worked out all your kinks.

Good luck…
j

Try adding a log command to see exactly what is getting passed (if anything) to your script:

on choose menu item theObject
	set objname to name of theObject
	set menu_selection to title of theObject
	log "selected menu item "" & menu_selection & "" of object "" & objname & """
	--rest of code...
end choose menu item

Jon