Apple Event: May 7th at 7 am PT

Looks like no one’s replied in a while. To start the conversation again, simply ask a new question.

(programmatically) populate pop-up list with range of values from another table

Hi there:


I've searched and scoured the forums here (including this), and I think I know the answer -- but thought I'd ask directly, in case I missed something obvious. :-)


I'd like to have a pop-up get its listed values automatically, with information from a range of rows in another table. Am I correct that this is not possible?


Reading through the Formulas and Functions User Guide & looking at the Numbers dictionary for Applescript, I don't see any reference to pop-ups.


At any rate, asking here before I start pecking out formulas/Applescripts and come up empty. Thanks in advance for a yes or no. :-)


--


My test case: I dragged a large group of contacts from Address Book into Numbers, creating a new table. I showed/hid various columns. I created another table with 2 columns: Company and Account #. In the Company column, I'd love to have a pop-up menu, listing the Company field from the Address Book table.


If there's another way to do this that I have just plain missed (other than grueling data entry), I'm all ears!


Thanks,


Patrick

Mac OS X (10.7.4), Numbers '09 v2.2 (516)

Posted on Sep 6, 2012 10:00 PM

Reply
14 replies

Sep 6, 2012 10:10 PM in response to patrickcheatham

Hi Patrick,


'All ears' won't do you a lot of good unless you are very talented and can type with them. 😉


Populating pop-up menus from existing data is an unsupported feature. You'll need to do the typing. Once done, you can Copy the cell containing the menu and Paste it into other cells where you want the same menu.


Feature enhancement requests (for future editions of the application) can be made through the Feedback channel. Go to the Application menu (in numbers, the "Numbers" menu) and choose Provide Numbers Feedback.


Regards,

Barry

Sep 7, 2012 12:42 PM in response to patrickcheatham

Hi,


patrickcheatham wrote:


looking at the Numbers dictionary for Applescript, I don't see any reference to pop-ups.

This is possible with the help of GUI Scripting, It's better than doing it manually.


You must enable the GUI Scripting by clicking the checkbox labeled "Enable access for assistive devices" in the Universal Access System Preference pane.


This script works on my system (iWorks 09, OSX 10.5.8)

Important, the selection must be contiguous.

--------------------------------------------

--This script works on multiple selections but only if the selection is contiguous.

settValuestomydoThis(1) -- get values of the selection

iftValuesisnot "" then

activate

display dialog "Select the cells where you want to create the PopUp." & return & "After that, click on the 'OK' button."

mydoThis(tValues) -- set the cell format of the new selection to "PopUp Menu" and set the values of the each menu item

tell application "Numbers" to display dialog "Done"

else

tell application "Numbers" to display dialog "You must select the cells in a table before running this script."

endif


ondoThis(n)

tell application "Numbers"

settTablesto (tablesofsheetsoffrontdocumentwhoseitsselection rangeisnotmissing value)

repeatwithtintTables -- t is a list of tables of a sheet

ifcontentsoftisnot {} then -- this list is not empty, it's the selected sheet

set activeTable to (get item 1 of t)

if n = 1 then return value of cells of selection range of activeTable -- return values of the selection

set format of (cells of selection range of activeTable) to pop up menu -- set the format to pop up menu

return my setValuePopUp(n) -- set value of each menu item

end if

end repeat

end tell

return ""

enddoThis


onsetValuePopUp(L)

tell application "System Events"

tell process "Numbers"

set frontmost to true

delay 0.3

set inspectorWindow to missing value

set tWindows to windows whose subrole is "AXFloatingWindow"

repeat with i in tWindows

if exists radio group 1 of i then

set inspectorWindow to i

exit repeat

end if

end repeat

if inspectorWindow is missing value then

keystroke "i" using {option down, command down} -- Show Inspector

else

perform action "AXRaise" of inspectorWindow -- raise the Inspector window to the front

end if

delay 0.3

tell window 1

click radio button 4 of radio group 1 -- the "cell format" tab

delay 0.3

tell group 2 of group 1

set tTable to table 1 of scroll area 1

set tc to count rows of tTable

set lenL to (count L)

if tc < lenL then -- ** add menu items **

repeat until (count rows of tTable) = lenL

click button 1 -- button [+]

end repeat

keystrokereturn -- validate the default name of the last menu item

else if tc > lenL then -- ** remove menu items **

repeat while exists row (lenL + 1) of tTable

select row (lenL + 1) of tTable

click button 2 -- button [-]

end repeat

end if

tell tTable to repeat with i from 1 to lenL -- ** change value of each menu item **

set value of text field 1 of row i to item i of L

end repeat

end tell

end tell

end tell

end tell

endsetValuePopUp

--------------------------------------------


1- You must select a range in a table.

2- Run the script

3- When a dialog is shown, don't click on any button.

Select another range where you want to create the PopUps (This can be in any table (even in another document))


4- Click the "OK" button in the dialog, do nothing with the keyboard or the mouse until is "Done"

Sep 7, 2012 2:16 PM in response to Jacques Rioux

This is pretty slick and very very useful but has one minor glitch. The popup is created correctly and puts the value if the first item into the cell but initially the inspector shows "new item" for the first item on the list. If you click on the inspector as if to change it, it changes from "new item" to the what it really is. I'm definitely going to keep this script around. The inability to pick items from a table has been a major drawback to popup menus. I still hope they end up implementing it the Excel way where the list is dynamic and changes as the table changes.


User uploaded file

Sep 7, 2012 2:21 PM in response to Jacques Rioux

Wow, Jacques -- that's cool! It works A-OK here (OS X 10.7.4, Numbers 2.2).


Step number 4, the "do nothing" is very important. It took a little bit of time to go from "New Item" to showing the proper values in the pop-up. (update: also, an empty cell value in the first range results in a "New Item" value in the pop-up -- which is sort of OK; better than an error ;-).


Ultimately, I'm after something more dynamic. Would it be possible to have a script similar to this which would update a pop-up?


...I ran the script today, but have added two new rows to my first table in your step 1. I'd like to add the new information from the table to the pop-up(s) -- a total of two new entries in each pop-up.


?


GUI Scripting is obviously very useful.

Sep 7, 2012 8:18 PM in response to patrickcheatham

Hi,


OK, I have not tested with blank cells or numbers, the title of the menu item are not changed in these two cases.


I modified the script to manage blank cells and numbers (assign a value to blank cell and convert number to a string), this doesn't change anything to the formats and values of the selected cells

Also, I modified the script for the minor glitch, I think he missed a delay, I add two delay of 0.1 second, I don't know if that's right, I did not have this issue.


Here is the revised script :

---------------------------

--This script works on multiple selections but only if the selection is contiguous.

settValuestomydoThis(1) -- get values of the selection

iftValuesisnot "" then

activate

display dialog "Select the cells where you want to create or update the PopUp." & return & "After that, click on the 'OK' button."

mydoThis(tValues) -- set the cell format of the new selection to "PopUp Menu" and if necessary then change value of the menu item in popup

tellapplication "Numbers" todisplay dialog "Done"

else

tell application "Numbers" to display dialog "You must select the cells in a table before running this script."

endif


ondoThis(n)

tell application "Numbers"

settTablesto (tablesofsheetsoffrontdocumentwhoseitsselection rangeisnotmissing value)

repeatwithtintTables -- t is a list of tables of a sheet

ifcontentsoftisnot {} then -- this list is not empty, it's the selected sheet

set activeTable to (get item 1 of t)

if n = 1 then

tell (selection range of activeTable)

set origFormat to {}

set blankCells to {}

tell (getcellswhosevalueis 0.0) -- to check blank cell, blank cell = 0.0, but maybe it's a number

repeat with i from 1 to (get count)

tell (get item i)

set oFormat to its format

set its format to text -- change format

if value = 0.0 then -- it's a blank cell, because 0.0 is not a string

set value to "__blank__"

set end of origFormat to oFormat

set end of blankCells to it

else -- it's a number, because 0.0 in text format is "0"

set its format to oFormat

end if

end tell

end repeat

end tell

set tValues to value of cells -- get values of the selection

tell blankCells to repeat with i from 1 to (get count)

tell item i to set {format, value} to {item i of origFormat, ""} -- reset format and value of blank cells

end repeat

returntValues -- ** return values of the selection **

end tell

else

setformatof (cellsofselection rangeofactiveTable) topop up menu -- set the format to pop up menu

returnmysetValuePopUp(n) -- set value of each menu item

end if

end if

end repeat

endtell

return ""

enddoThis


onsetValuePopUp(L)

tell application "System Events"

tell process "Numbers"

set frontmost to true

delay 0.3

set inspectorWindow to missing value

set tWindows to windows whose subrole is "AXFloatingWindow"

repeat with i in tWindows

if exists radio group 1 of i then

set inspectorWindow to i

exit repeat

end if

end repeat

if inspectorWindow is missing value then

keystroke "i" using {option down, command down} -- Show Inspector

else

performaction "AXRaise" ofinspectorWindow -- raise the Inspector window to the front

end if

delay 0.3

tell window 1

clickradio button 4 ofradio group 1 -- the "cell format" tab

delay 0.3

tell group 2 of group 1

set tTable to table 1 of scroll area 1

set tc to count rows of tTable

set lenL to (count L)


if tc < lenL then -- ** add menu items **

repeat until (count rows of tTable) = lenL

click button 1 -- button [+]

end repeat

delay 0.1

keystrokereturn -- validate the default name of the last menu item

else if tc > lenL then -- ** remove menu items **

set thisRow to row (lenL + 1) of tTable

repeat while exists thisRow

select thisRow

click button 2 -- button [-]

end repeat

end if

delay 0.1

set L2 to value of text field 1 of rows of tTable

repeatwithifrom 1 tolenL -- ** if necessary then change value of the menu item **

set tValue to (item i of L) as string

if (item i of L2) is not tValue then -- not the same value

if tValue = "__blank__" then

set value of text field 1 of row i of tTable to "_" --set value of this menu item to underscore

else

set value of text field 1 of row i of tTable to tValue

end if

end if

end repeat

end tell

end tell

end tell

endtell

endsetValuePopUp

---------------------------

if the cell is empty the menu title will be a underscore character

If you want it to be another character, replace the underscore character in this line ( set value of text field 1 of row i of tTable to "_") at the end of the script.

Don't use a empy string, you can not select any item in the popup menu.


But if you want to ignore the blank cells (to not to add unnecessary menu item in the popup), I can change the script.



patrickcheatham wrote:


Ultimately, I'm after something more dynamic. Would it be possible to have a script similar to this which would update a pop-up?


...I ran the script today, but have added two new rows to my first table in your step 1. I'd like to add the new information from the table to the pop-up(s) -- a total of two new entries in each pop-up?


With the current script, you can, but you must select all the cells, the script will update the menu items that will be different and add additional menu items.


But, here is the script wich append the values of the selection to popup menus, this script skip the blank cells :

---------------------------

--This script works on single selection or (multiple selections but only if the selection is contiguous).

-- this script append the values of the selection to popup menus (another selection)

settValuestomydoThis(1) -- get values of the selection


iftValuesisnot "" then

activate

display dialog "Select the cells where you want to update the PopUp." & return & "After that, click on the 'OK' button."

repeat

mydoThis(tValues) -- append the values to popup menu

if the result is "a cell is not a popup" then

activate

display dialog "The selection contains a cell which is not a popup menu." & return & "Select the cells where you want to update the PopUp." & return & "After that, click on the 'OK' button."

else

tell application "Numbers" to display dialog "Done"

return

end if

endrepeat

else

tell application "Numbers" to display dialog "You must select the cells in a table before running this script."

endif


ondoThis(n)

tell application "Numbers"

settTablesto (tablesofsheetsoffrontdocumentwhoseitsselection rangeisnotmissing value)

repeatwithtintTables -- t is a list of tables of a sheet

ifcontentsoftisnot {} then -- this list is not empty, it's the selected sheet

set activeTable to (get item 1 of t)

if n = 1 then

tell (selection range of activeTable)

set origFormat to {}

set blankCells to {}

tell (getcellswhosevalueis 0.0) -- to check blank cell, blank cell = 0.0, but maybe it's a number

repeat with i from 1 to (get count)

tell (get item i)

set oFormat to its format

set its format to text -- change format

if value = 0.0 then -- it's a blank cell, because 0.0 is not a string

set value to "__blank__"

set end of origFormat to oFormat

set end of blankCells to it

else -- it's a number, because 0.0 in text format is "0"

set its format to oFormat

end if

end tell

end repeat

end tell

set tValues to (value of cells whose value is not "__blank__") -- get values of the selection, skip blank cells

tell blankCells to repeat with i from 1 to (get count)

tell item i to set {format, value} to {item i of origFormat, ""} -- reset format and value of blank cells

end repeat

returntValues -- ** return values of the selection **

end tell

else

set x to (cells of selection range of activeTable whose its format is not pop up menu) -- check other format

if x is {} then

returnmysetValuePopUp(n) -- append these values to the popup menu

else

return "a cell is not a popup"

end if

end if

end if

end repeat

endtell

return ""

enddoThis


onsetValuePopUp(L)

tell application "System Events"

tell process "Numbers"

set frontmost to true

delay 0.3

set inspectorWindow to missing value

set tWindows to windows whose subrole is "AXFloatingWindow"

repeat with i in tWindows

if exists radio group 1 of i then

set inspectorWindow to i

exit repeat

end if

end repeat

if inspectorWindow is missing value then

keystroke "i" using {option down, command down} -- Show Inspector

else

performaction "AXRaise" ofinspectorWindow -- raise the Inspector window to the front

end if

delay 0.3

tell window 1

clickradio button 4 ofradio group 1 -- the "cell format" tab

delay 0.3

tell group 2 of group 1

set tTable to table 1 of scroll area 1

set tc to (count rows of tTable)

set lenL to tc + (count L)

set j to 1

repeat until (count rows of tTable) = lenL -- ** add menu items **

click button 1 -- button [+]

delay 0.1

set value of text field 1 of last row of tTable to (item j of L) as string

set j to j + 1

end repeat

end tell

end tell

end tell

endtell

return ""

endsetValuePopUp

---------------------------

Sep 7, 2012 9:53 PM in response to Jacques Rioux

Wow again... While this doesn't solve the issue of this (seemingly basic) functionality not being present already in Numbers... This answers the question at hand.


Having complete control over my tables, the append sript is great -- I can just add the rows I want when I want. That said, I tried out the revised first script in a few different scenarios; Since the current selected state of the pop-up is not changed when the pop-up is altered (as long as an identical entry exists in input), it's also just as easy to re-select the initial input range to include the new items and go from there.


Sweet!


Thanks Jacques for the scripts, BadUnit for the input and Barry for the insight. I love this forum! Now I need to bone up on Applescript. 😝


Best,


Patrick

Sep 8, 2012 6:58 AM in response to patrickcheatham

I have been trying to determine a method for updating a pop up menu without affecting the value (item) that was previously chosen. Until someone figures out how to set the value of a cell formatted as a pop up menu, I don't think it is possible. It does not seem to be directly accessible via Applescript and I don't know how to use GUI scripting to get to the menu at the cell.


Without that, the only modification that is guaranteed to not change any previously selected values is appending items to the end of the list, as the second script is designed to do (or if you append items to your list and run the first script again). What I noticed from testing the first script is that the value in the cell will be unchanged only if the menu item is still on the list and stays in the same position on the list.

Sep 28, 2016 3:20 AM in response to Jacques Rioux

Hi Jacques,


I stumble into your script when looking for solution to the same question as patrick's. I'm pretty new to Mac / Numbers / AppleScript so pardon me if my question to you might be noob.


I'm running macOS Sierra and Number 4.0. I copied your script (not the append script) to Script Editor and compile it with no error. However when I tried run it (in Script Editor) I got error message as below:

error "Numbers got an error: Can’t make missing value into type range." number -1700 frommissing valuetorange

May I know what have I done wrong? Thanks

Sep 28, 2016 10:33 AM in response to chi-dvsg

Hi,


chi-dvsg wrote:


I'm running macOS Sierra and Number 4.0. I copied your script (not the append script) to Script Editor and compile it with no error. However when I tried run it (in Script Editor) I got error message as below:

error "Numbers got an error: Can’t make missing value into type range." number -1700 frommissing valuetorange

May I know what have I done wrong? Thanks


The script doesn't work because it was for Numbers Version 2.x.

There are changes since, the inspector is not in the floating window, change in the UI Elements, AppleScript syntax of Numbers.


Here's the script for Numbers Version 4.0

--------

set tValues to my doThis(1) -- get values of the selection

if tValues is not "" then

activate

display dialog "Select the cells where you want to create or update the PopUp." & return & "After that, click on the 'OK' button."

my doThis(tValues) -- set the cell format of the new selection to "PopUp Menu" and if necessary then change value of the menu item in popup

display dialog "Done" buttons {"OK"}

else

activate

display dialog "You must select the cells in a table before running this script."

end if


on doThis(n)

tell application "Numbers"

set tTables to tables of active sheet of front document whose class of selection range is range

if tTables is {} then return "" -- no selection

set activeTable to item 1 of tTables

if n = 1 then

tell (selection range of activeTable) to return formatted value of cells -- return values of the selection

else

set mySel to selection range of activeTable

set format of (cells of mySel) to pop up menu -- set the format to pop up menu

set selection range of activeTable to mySel-- reselect the original selection because (maybe a bug ) the "set format" command select the last cell (on multiple selection only)

return my setValuePopUp(n) -- set value of each menu item

end if

end tell

end doThis


on setValuePopUp(L)

tell application "System Events"

tell process "Numbers"

tell window 1

tell radio button 1 of radio group 1 of toolbar 1 -- the "Format" button in the toolbar

if its value is not 1 then perform action "AXPress" -- show inspector

end tell

perform action "AXPress" of radio button 2 of radio group 1 -- the "cell" tab

delay 0.3

tell scroll area 4

set tTable to table 1 of scroll area 1

set tc to count rows of tTable

set lenL to (count L)

if tc < lenL then -- ** add menu items **

repeat until (count rows of tTable) = lenL

perform action "AXPress" of button 1 of group 1 -- button [+]

end repeat

else if tc > lenL then -- ** remove menu items **

set thisRow to row (lenL + 1) of tTable

repeat while exists thisRow

select thisRow

perform action "AXPress" of button 2 of group 1 -- button [-]

end repeat

end if

delay 0.3

set L2 to value of text field 1 of rows of tTable

repeat with i from 1 to lenL-- ** if necessary then change value of the menu item **

set tValue to (item i of L)

if (item i of L2) is not tValue then -- not the same value

if tValue = missing value then -- it's a blank cell

set value of text field 1 of row i of tTable to "_" --set value of this menu item to underscore

else

set value of text field 1 of row i of tTable to tValue

end if

end if

end repeat

end tell

end tell

end tell

end tell

end setValuePopUp

Sep 29, 2016 7:04 PM in response to Jacques Rioux

Hi Jac,


Thanks a lot for your reply.


I run your revised script, this time it can run, however it doesn't seem to work as intended.


Here is what I experience: I select a range of cells with values for the pop-up menu e.g. USD, AUD, SGD, EUR ... an then run the script and select a cell to create pop-up as prompt

1. if the cell is empty, after click 'OK' the pop-up is created on that cell but the values are default values i.e. Item 1, Item 2, Item 3 but not those on the range that I selected before.

2. if the cell is not empty, after click 'OK' the pop-up is created with one value of that cell only.


Do you experience the same? Thanks.

Sep 30, 2016 7:44 AM in response to chi-dvsg

Hi,


chi-dvsg wrote:



I run your revised script, this time it can run, however it doesn't seem to work as intended.


Here is what I experience: I select a range of cells with values for the pop-up menu e.g. USD, AUD, SGD, EUR ... an then run the script and select a cell to create pop-up as prompt

1. if the cell is empty, after click 'OK' the pop-up is created on that cell but the values are default values i.e. Item 1, Item 2, Item 3 but not those on the range that I selected before.

2. if the cell is not empty, after click 'OK' the pop-up is created with one value of that cell only.


Do you experience the same?


I just tested the script (with the delay commands and without the delay commands) and it works .



It seems that your script is not allowed to use the accessibility (the GUI Scripting part).

Put your script or your workflow in the "Security & Privacy" pane --> Privacy --> Accessibility, and check the checkbox

See https://support.apple.com/en-us/HT202866



But there is a small change to the script:

Use this

if tc < lenL then -- ** add menu items **
    repeat until (count rows of tTable) = lenL
        perform action "AXPress" of button 1 of group 1 -- button [+]
    end repeat
    perform action "AXConfirm" of text field 1 of row lenL of tTable
else if tc > lenL then -- ** remove menu items **


instead of

if tc < lenL then -- ** add menu items **
    repeat until (count rows of tTable) = lenL
        perform action "AXPress" of button 1 of group 1 -- button [+]
    end repeat
else if tc > lenL then -- ** remove menu items **

(programmatically) populate pop-up list with range of values from another table

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