Display Different Image Based On Popup Menu Choice

I am an elementary teacher trying create my first application with XCode. What I’m doing is making a simple “Compliment” application where the kid picks his name from a popup menu and picks a voice from another popup menu, then he clicks a button “Compliment Me”. That part works… the voice gives the kid a compliment. But in Interface Builder I also have a NSImageView box because I’d like the student’s picture to appear based on the name chosen in the popup menu (that’s why I made it a popup menu instead of a text box so I could match the menu choice with an image). I put the student’s pictures in the Resources folder… but I can’t figure out the code to get the NSImageView box to display the picture based on the name chosen. I want something like this (using AppleScript):

If the value of popup button studentNames=1 (first choice in popup menu) then set studentPicture (the name I gave to the NSImageView box) to display Alfonso.gif
If the value of popup button studentNames=2 then set studentPicture to display David.gif
If the value of popup button studentNames=3 then set studentPicture to display Jenn.gif
etc.

I’d appreciate any help you can give me. Thanks a lot! David from Virginia

on clicked theObject
	set theName to the title of popup button 1 of window 1
	set the image of image view "studentPicture" of window 1 to load image theName & ".gif"
end clicked

This script will set a varible to the correct name of the student (depending on who they choose) and load their respective image into the image view. For the pop up button, select on clicked and then select the script.

I would recommend window "main" (give the window a name of your choosing) or window of theObject (when dealing with objects in the same window); window 1 would fail if your application has any other window in front (e.g. a preferences window or an about box).

Thank you so much!!! It worked… the kids love it! Now on to more scripting :smiley:
Here’s my final version:


on clicked compButton
	tell window "main"
		set theName to (the title of popup button "teamNames") as string
		set theVoice to (the title of popup button "voiceButton") as string
		set the image of image view "teamPic" to load image theName & ".gif"
	end tell
	set theCompliment to "Hey, " & theName & ", you are the smartest kid in class!"
	say theCompliment using theVoice
end clicked

How about adding a list with different sayings so the kid gets a variety of answers.

Like:

You are great!
Nice work today!
etc etc

yeah, you could even make it choose a random phrase from a list

set theCompliment to "Hey, " & theName & some item of {", great work today!", ", keep it up!!", ", another phrase here!"}