Mailing Lists: Apple Mailing Lists

Image of Mac OS face in stamp
 
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: Popup Button Unique ID



Hi,

Unfortunately my replies do bounce often since I'm on Tiger. So no idea whether or who got it.

@ Chris:
I'm not sure whether I understand why you fill the popup and delete the menus.
Depending on what you want to do maybe the following is an alternative. I did that with some of of my apps and does seem to work fine and also requires only a little bit of coding and the code is very easy to debug even with a lot buttons.
Use user defaults and Cocoa Bindings in IB. Just a simple example for one popup. (Names in UD and Cocoa Bindings are case sensitive).
In IB select the popup and press "command-4" for the bindings.
Select "content values" and "Shared User Defaults" and hit return - now you have connected the content of the popup with list or value from your user defaults. But there is no real path to the value yet. So type a name into the field "Model Key Path" like "popup1Content"
Then you have to take care of which item is selected. In bindings select either "selectedIndex" (or even maybe "selectedTag" , I haven't tried that), hit return again and set the "Model Key Path" to "popup1Index" or whatever.
You now have a popup which is automatically filled with values based on a (yet undefined) list, and has a special (yet undefined) index selected.
So there is the need to type some code in Xcode.
You have to create a basic list in User Defaults, so that the popup finds the above described value list, also there is a index or value needed. Best place is "on will finish launching", so the code could look like for one button (with many buttons you can do a repeat loop)


on will finish launching theObject
-- the below is for the first launch of the app
tell user defaults
make new default entry at the end of default entries with properties {name:"popup1Content", value:{""}}
make new default entry at the end of default entries with properties {name:"popup1Tags",value: {"0"}
make new default entry at the end of default entries with properties {name:"popup1Index", value:0}
end tell
-- if you want to reset it with every launch type the below or clear the entries any time during runtime
tell user defaults
set content of default entry "popup1Content" to {""}
set content of default entry "popup1Tags" to {""}
set content of default entry "popup1Index" to 0
end tell
end will finish launching


When launching your app the popup will have one entry - "" - which is selected. In case you would have done:
tell user defaults
make new default entry at the end of default entries with properties {name:"popup1Content",value: {"ABC","QRS","XYZ"}}
make new default entry at the end of default entries with properties {name:"popup1Tags",value: {"32433","26554","55456"}}
make new default entry at the end of default entries with properties {name:"popup1Index",value: 1}
...
...
end
the popup will have 3 entries - ""ABC","QRS","XYZ"" - where " QRS" which is selected.


You can now always reset or fill the default entries during runtime and the popup will reflect that like
set content of default entry "popup1Content" of user defaults to {"1","2","3"}
or
set content of default entry "popup1Content" of user defaults to myList -- myList = a list of text items


To make it easier to work with the records you can set up a data source at launch time, like
set theDataSource to make new data source at the end of data sources
tell theDataSource
make new data column at the end of data columns with properties {name:"the_vendor"}
make new data column at the end of data columns with properties {name:"id_vendor"}
end


to get the values during runtime you could use for example (for the text field, popup)
on action theObject
if name of theObject is "id_product" then
set id_product to content of theObject
set myParams to {id_product: id_product }


tell application "http://tm-myserver.local/xml-rpc/product_images.lasso";
set the_data to call xmlrpc {method name:"fetch_product_vendors", parameters:the_params}
end tell


-- then clear all data rows of the data source
delete every data row of theDataSource

-- then append your records
append theDataSource with the_data

-- now rebuild the lists
tell theDataSource
set popup1Content to data cell 1 of every data row
set popup1Tags to data cell 2 of every data row
end

-- now rebuild the UD entries
tell user defaults
set content of default entry "popup1Content" to popup1Content
set content of default entry "popup1Tags" to popup1Tags -- in case you haven't set up the list as a property
set content of default entry "popup1Index" to 0 -- selects the first vendor
end


else -- it's the popup
set myTag to item ((content of theObject)+1) of popup1Tags -- or content of the UD value, s.a.


end

Hopefully I did not made to many typing errors and my english is good enough to explain.
The advantage of the bindings is that you don't have to care about structures of the UI and it very often helps to type less or at least more simple code. In the above example you could have a second popup which displays the id and connect it to the UD as well, using the same index entry, but the tag list as content values. Selecting a tag value in this popup will set the selected value of the first popup to the right vendor, selecting a vendor in first one will select the correct id in the second.


Maybe this helps.

Regards
Andreas

On 08.07.2005, at 03:26, Neil Faiman wrote:


On Jul 7, 2005, at 1:30 PM, Chris Tracewell wrote:



I just feel like I am missing something - assigning a unique ID to a menu item seems like it is an essential requirement of a GUI. I mean I am just surpirsed that after hours of Googling I haven't run across anyone else who is trying to do this – it just has to be something I am missing - even simple HTML pop up menus have name/value properties. It just seems like I am making this too hard.



Here is some code from an AppleScript Studio application that I wrote. This is a function that initializes a popup button at runtime:


to initializePopup(theButton, theList, theSelectedItem, theFilter)
tell theButton
delete every menu item of menu 1
set auto enables items to false
local i
repeat with i from 1 to count theList
local theItem
set theItem to item i of theList
if shouldInclude(theItem) of theFilter then
make new menu item at the end of the menu items of menu 1 ¬
with properties {title:getMenuLabel() of theItem, tag:i, enabled:shouldEnable(theItem) of theFilter}
if isEqualTo(theSelectedItem) of theItem then
set the current menu item to the last menu item of menu 1
end if
end if
end repeat


if (count menu items) is 0 then
make new menu item at the end of the menu items of menu 1 ¬
with properties {title:"", tag:-1, enabled:false}
return missing value
else
return item (the tag of the current menu item) of theList
end if
end tell
end initializePopup


The parameters are a the popup button to be initialized, a list of items that will be the values of the menu, the item in the list that will be the initial selected item (if undefined, the first item in the list is used), and a script object with a handler "shouldInclude" that decides whether each item of the list should be included in the menu and a handler "shouldEnable" that decides whether each included item should initially be enabled. The items in the list are objects that must have a "getMenuLabel" handler that returns a string that will be used as the menu item name.

Then here is the "choose menu item" handler:

on choose menu item theButton
if the name of theButton is "Past Meeting" then
tell currentTabActions to choosePastMeeting(item (the tag of the current menu item of theButton) of meetings(0) of pastMeetings)
else if the name of theButton is "Agenda" then
tell currentTabActions to chooseAgenda(the tag of the current menu item of theButton)
else
error "Clicked unknown object " & the name of theButton
end if
end choose menu item


Hope this helps you some.

Neil Faiman _______________________________________________
Do not post admin requests to the list. They will be ignored.
Applescript-studio mailing list (Applescript- email@hidden)
Help/Unsubscribe/Update your Subscription:
http://lists.apple.com/mailman/options/applescript-studio/kiel% 40spherico.com


This email sent to email@hidden


_______________________________________________ Do not post admin requests to the list. They will be ignored. Applescript-studio mailing list (email@hidden) Help/Unsubscribe/Update your Subscription: http://lists.apple.com/mailman/options/applescript-studio/email@hidden

This email sent to email@hidden
References: 
 >RE: Popup Button Unique ID (From: "Christopher Hickman" <email@hidden>)
 >Re: Popup Button Unique ID (From: Chris Tracewell <email@hidden>)
 >Re: Popup Button Unique ID (From: Neil Faiman <email@hidden>)



Visit the Apple Store online or at retail locations.
1-800-MY-APPLE

Contact Apple | Terms of Use | Privacy Policy

Copyright © 2007 Apple Inc. All rights reserved.