Set Popup Button's item

When using the popup button for a preference and you want to save that preference for next time how would you set the current item of the popup button? Heres what I have so far: (ITEM 1, 2, and 3 are the titles of the 3 items in the popup button)

tell NSUserDefaults
tell its standardUserDefaults()
set menuitem to its objectForKey_(“ITEMS”) as string
end tell
end tell

(menuitem is loaded as “ITEM 2”)

if menuitem is “item1” then
display dialog “ITEM 1”
else if menuitem is “item2” then
display dialog “ITEM 2”
else if menuitem is item3" then
display dialog “ITEM 3”
end if

The dialog containing “ITEM 2” is shown, but the popup button shows item1 selected in the preference window.

Here is what I tried but has not worked:

EDIT----PopupButton’s setSelectedItem_(menuitem)

Try removing the quotes around menuitem.

The quotes around menuitem was a typo in the post, in the actual script there are no quotes around it.

I just had a look at NSPopupItem, and it doesn’t have a setSelectedItem: method. Have a play with selectItemWithTitle: or perhaps setTitle:.

selectItemWithTitle:
Selects the item with the specified title.

  • (void)selectItemWithTitle:(NSString *)title

Take look at :

NSPopUpButton Class Reference

selectItemWithTitle:
Selects the item with the specified title.

  • (void)selectItemWithTitle:(NSString *)title

Parameters
title
The title of the item to select. If you specify nil, an empty string, or a string that does not match the title of a menu item, this method deselects the currently selected item.

Availability
Available in Mac OS X v10.0 and later.
See Also
“ indexOfItemWithTitle:
“ addItemWithTitle:
“ setTitle:
Related Sample Code

Setting the Current Selection
“ selectItem:
“ selectItemAtIndex:
“ selectItemWithTag:
“ selectItemWithTitle:
“ setObjectValue:

I just created a popup menu with the following items in IB, and this works fine…

Front Window
Rear Window
All Open Windows

I then did CNTRL-DRAG from the blue AppController “SetWindowAppDelegate” icon to the popup menu in order to link it to the controller.

Even though the NSPopUpButton Class Reference shows the following :

  • (void)selectItemWithTitle:(NSString *)title

and also shows :

Setting the Current Selection
“ selectItem:
“ selectItemAtIndex:
“ selectItemWithTag:
“ selectItemWithTitle:
“ setObjectValue:

REMEMBER TO : add the underscore to the end of the method name as shown below.

script SetWindowAppDelegate
– ±--------±--------±--------±--------±--------±--------±--------±--------+
– IB_Outlets
– Interface Builder considers an outlet as any
– property with “missing value” as its value
– ±--------±--------±--------±--------±--------±--------±--------±--------+
property aPopupMenu : missing value

-- +---------+---------+---------+---------+---------+---------+---------+---------+
on awakeFromNib()
	set choice to 2		-- change this number {1, 2, 3, 4} to test, These all work...

	if(choice is 1) then
		aPopupMenu's selectItemAtIndex_(1)
	
	else if(choice is 2) then
		aPopupMenu's selectItemAtIndex_(2)
	
	else if(choice is 3) then
		aPopupMenu's selectItemWithTitle_("Rear Window")
	
	else if(choice is 4) then
		aPopupMenu's selectItemWithTitle_("All Open Windows")
	end if

end awakeFromNib
-- +---------+---------+---------+---------+---------+---------+---------+---------+

end script

Best Regards,

Bill Hernandez
Plano, Texas