How do I populate a popup button?

Well, I've found this snippet of code here and in various place around the 'net:

tell window 1 tell menu of popup button 1 delete every menu item repeat with catListItem in catList make new menu item at end of menu items with properties {title:catListItem} end repeat end tell end tell


When I use it in my AppDelegate script in a Cocoa-AppleScript Application, Xcode gives me an error: "t2t_AppDelegate.applescript:25: error: Expected end of line but found identifier. (-2741)" Line 25 is "tell menu..."


I'm not sure what I'm missing that would allow me to dynamically populate the popup button with a list of terms (catList) that I'm drawing from another application. Any suggestions?

Mac OS X (10.7.4), iMac (20-inch, Early 2008)

Posted on Sep 3, 2012 9:55 PM

Reply
12 replies

Sep 3, 2012 10:20 PM in response to Don Morris

Hard to tell for sure, but a 'menu' is a listed element of a 'pop up button'. As an element there may be more than one - of course, in your case you might have only one, but that's not a forced limit.

Therefore, within AppleScript, you need to be more definitive as to which menu you want to target, e.g.


tell menu 1 of pop up button 1

By identifying the specific menu associated with the popup you should have more success.

Sep 4, 2012 9:42 AM in response to Camelot

Identifying the menu numerically didn't change the error given. Changing "popup button" to "pop up button" (as I've seen in other examples) does give me a different error: Expected end of line but found application constant or consideration.


I decided to take my script over to AppleScript Editor to see if I could get some more information. (Xcode is unhelpful in this regard, since it doesn't highlight the offending term.) There I found that "button" chokes the compiler; I presume it's the same point at which Xcode chokes. ("up" chokes the compiler in your example.)

Sep 4, 2012 4:13 PM in response to Don Morris

It looks like you are using AppleScript Studio terminology, which was deprecated in Snow Leopard. Using the current AppleScriptObjC framework, the user interface items are referenced via outlet properties, for example:


property myPopUp : missing value


From the Interface Editor, this property is connected to your popup button, which allows you to use it with various methods in the NSPopupButton class and its parents, such as addItemsWithTitles. Once everything is defined and connected, you would use something like:


set catList to {"next item", "another item", "Items added"}

myPopUp's addItemsWithTitles_(catList)

Sep 4, 2012 9:39 PM in response to red_menace

Thanks for answering, Red Menace, here and elsewhere. 🙂 Yes, this code was found in connection with AppleScript Studio. Silly me, I thought a Cocoa-AppleScript Application would be similar.


I've added the

property popCatList : missing value

declaration and connected the Pop Up Button in my view: what I'm seeing is (Menu)-(Menu - popCatList). Then I added the

popCatList's addItemsWithTitles_(catList)

command. Leaving out my code to populate the initial array, here's what I've got:


script t2t_AppDelegate property parent : class "NSObject" property popCatList : missing value set catList to {"1","2","3"} popCatList's addItemsWithTitles_(catList) on applicationWillFinishLaunching_(aNotification) -- Insert code here to initialize your application before any files are opened end applicationWillFinishLaunching_ on applicationShouldTerminate_(sender) -- Insert code here to do any housekeeping before your application quits return current application's NSTerminateNow end applicationShouldTerminate_ end script


The build succeeds but I get nothing in the popup. Have I created the connection improperly (I can't figure out how to remove it -- clicking the 'x' doesn't do anything) or is there some additional code I'm missing?

Sep 6, 2012 4:03 PM in response to red_menace

So I have since found -- nice to know that I have to completely unlearn what little I knew about AppleScript. 🙂


I also found that I did have a bad connection, but I've learned to do those properly now. My menu is displaying as desired.


Last issue: adding a separator to the menu. I found some AppleScript Studio code and an example that indicated that I needed to subclass NSObject, etc. I tried it, didn't work. Someone over at MacScripters suggested this tiny snippet:



set theSep to current application's NSMenuItem's separatorItem()


thePopup's menu()'s addItem_(theSep)



but I'm seeing an error "unrecognized function addItem_. (error -10000)" when I use that. How would I correctly add a separator to my popup dynamically?

Feb 13, 2015 4:33 AM in response to red_menace

Hi red_menace,

I'm following on this example but I've a doubt; how I could assign, the selected item, to variable.


Example.

property myPopUp : missing value --referencing outlets to appdelegate.applescript
property myExit : "" -- input to show the selected item on myPopUp, binding Value (AppDelegate.myExit)
..........

  on applicationWillFinishLaunching_(aNotification)
  -- Insert code here to initialize your application before any files are opened
        
        set catList to {"angora", "romano", "egipcio"}
        elPopUp's addItemsWithTitles_(catList)
    
        set exit to (myPopUp's selectedItem_(catList)) -- It's doesn't work,
                              -- [NSPopUpButton selectedItem:]: unrecognized selector sent to instance 0x1044304a0 (error -10000)
        setMyExit_(exit as string)

  end applicationWillFinishLaunching_


My first steps on cocoa-applescript. I've a lot of scripts on applescripts :-(

Anyone can help me, thanks a lot!!

Feb 13, 2015 5:37 AM in response to malacate

Here I'm again, I'm sorry.


property myPopUp : missing value --referencing outlets to appdelegate.applescript  
property myExit : "" -- input to show the selected item on myPopUp, binding Value (AppDelegate.myExit)  
..........  
  
  on applicationWillFinishLaunching_(aNotification)  
  -- Insert code here to initialize your application before any files are opened  
          
        set catList to {"angora", "romano", "egipcio"}  
        elPopUp's addItemsWithTitles_(catList)  
     
        set exit to myPopUp's titleOfSelectedItem() as text  
        setMyExit_(exit) 
  
  end applicationWillFinishLaunching_ 

This script works fine, but I don't get the dinamic refresh of the input, changing the selection in the popup 😟

Only show the first item

This thread has been closed by the system or the community team. You may vote for any posts you find helpful, or search the Community for additional answers.

How do I populate a popup button?

Welcome to Apple Support Community
A forum where Apple customers help each other with their products. Get started with your Apple Account.