Articles Blog

Creating Dynamic Pop-Up Menus In Numbers With AppleScript (#1703)

Creating Dynamic Pop-Up Menus In Numbers With AppleScript (#1703)


So in my previous video I showed you how to
get pop-up menus in Numbers to actually work with a list in another table. It takes some work. Every time you update you have to, kind of,
redo the pop-up menus. So isn’t there a way to do it truly dynamically
where it’s going to pull from this list no matter what’s in it. The answer is yes, there is a way to do it
but it requires you to use Automator and AppleScript and it’s kind of buggy and it kind of works. I’m going to show you how and maybe it’s something
you can build off of especially if you’re good with AppleScripts. I have my Table 2 here. It’s got these items in it. I’ve got Table 1 where I want to put stuff
and here’s what I’ve done in AppleScript. I’ve actually started a new Automator task. I’ve created a new workflow and I’ve set it
as a Service. The Service receives no input and it’s used
in Numbers only. Then I added one thing to it, run AppleScript. So I’m basically using Automator as a way
to compose AppleScript and set this up as a Service. So what’s going to happen. Well, so the real AppleScript is right in
here, the on run that’s kind of added in there by Automator. It’s going to tell Application Numbers and
tell Table 2 of sheet 1 of document 1. What is that? Well, that is this Table 2 here. So the items that I want to get input into
this list. Very clunky to have it always look at Table
2 of sheet 1 of document 1. You might need to adjust that. Probably do some clever AppleScript to maybe
get the table by name or some other way. Then it’s going to create an options list. It’s going to start counting at number 2. why? Because the first item here is just called
item so I want to start in row 2. It’s going to repeat while n is less than
the number of cells in that column. So in other words repeat from 2 all the way
to 12. It’s going to grab the value of that cell. Store it in something called option. Then it’s going to add that to the options
list so options to options and option. In other words the current list plus the new
item. It’s going to increase n by 1 and it’s going
to repeat here until it gets all of them. Then it’s done with that part. So it calculated this option’s variable with
a list of items. Now it’s going to tell the front document’s
active sheet to get the first table whose selection range’s class is range. That’s a really weird way in AppleScrip of
figuring out of what cell is currently selected. Basically saying what table has a range in
it, a range of cells. The only one that is going to have one is
the one where you have selected a cell. Then tell that selection range to set the
variable c to that first cell. So that first cell is going to be c. Set a variable called mychoice to choose from
list options with prompt please choose default items None. So it’s going to pop-up a little list of all
the stuff we collected here. It’s going to set the value of that cell to
that. That’s what it does. So Save that and save it as a Service that
works in Numbers that should appear in Numbers, Services and we should see it here. Choose From List. There it is. So I’m going to select a cell here and I’m
going to say Numbers, Services, Choose from List. Now notice that I can set a keyboard shortcut
for that. So you do that in System Preferences, Keyboard,
Shortcuts, and then in Services I can set the keyboard shortcut so I don’t have to go
through this whole thing of Numbers, Services, Choose from List. It’s going to pop-up a list. There it is. Please choose and notice it has all the items
that are here. I’ll choose one and hit Okay and it populates
that. Is it dynamic? Yes. So I’ll add something else here. Let’s just add Other to the list and I’ll
go here and say Choose from List and notice Other now is at the bottom of the list. So it will add items. It will update as you change items here. The keyboard shortcut would make this really
easy to just hit the keyboard shortcut and click on an item. You can even, I think, use the keyboard here. So yep I can use the keyboard, down arrow,
okay. So with the keyboard shortcut it makes it
real easy to enter items and do that. All using AppleScript. I’ll include the script that you can Copy
and Paste into Automator in the description for this video at the MacMost site.

Leave a Reply

Your email address will not be published. Required fields are marked *