Question: How Can I Select a Named Object in Keynote with AppleScript?

As you may know, Keynote provides the ability to name objects on a slide. I want to select one of those objects using AppleScript and then manipulate it further using KM. Does anyone know how to do this? I'd be grateful for a code snippet. Thanks.

A little more info is probably needed... and while I am by no means an AppleScript wizard I have learned enough over the last 6 months to know that trying to write a script for somebody else can be challenging without having the actual app/file or whatever the OP is using. That being said:

  1. what would you like to do with said object from Keyboard Maestro?
  2. any way you could share the Keynote slide?
  3. have you heard of UI Browser?

I am quite the rookie with AppleScript, but I'm not opposed to making an attempt to write something for you if I could get a little more info. :grin:

-Chris

Hey Michael,

You'll get more focused help on:

Thanks, Chris. I did post in MacScripter.net. So far, I haven't had a reply there.

I'm creating a digital planner in Keynote. It contains a ton of hyperlinks. The document is then exported as a PDF and imported into GoodNotes on the iPad. The hyperlinks provide a means of navigation throughout the PDF.

My Annual Calendar page has 377 hyperlinks (one for each month and one for each day). Changing them one at a time is tedious and time-consuming.

I was hoping to use AppleScript to select the named object, then use Keyboard Maestro to add the hyperlink. Then use AppleScript to move to the next named object. I can do everything in Keyboard Maestro with the exception of selecting the object.

If you cick the link above, you can download the Keynote page. Thanks for your willingness to take a look.

By the way, that UI Browser looks amazing.

Hey Michael, glad the UI Browser app piqued your interest! Also, that's quite the impressive planner you've built; kudos!

After taking a look at it via UI Browser I don't believe that you will be able to use GUI AppleScript to achieve this. No matter what part of the slide I inspect, UIB shows the same information and (apparently) none of the hyperlinks have their own accessibility data that AppleScript can read. The screenshot below shows that the deepest level you can interact with using GUI AppleScript is the slide itself (scroll area 3), but nothing else within that slide appears.

Screenshot of Keynote GUI AppleScript information (click to expand/collapse)

There may be a way to do it using Keynote's AppleScript dictionary but I am not very familiar with it's dictionary and a cursory glance at it doesn’t seem to indicate any way to interact with hyperlinks embedded in a page.

So essentially, I don't think it's possible, but I am very novice at AppleScript. As Chris said, the real experts are more likely to be found on other forums. I post occasionally on the Late Night Software forum (Script Debugger's forum) and the responses there, while very helpful, are generally slower than here.

You could likely accomplish this with the found Keyboard Maestro image action though! Perhaps running it through a loop like For Each found image... do X. But my personal use of the For Each action is pretty much limited to folders and files, so I wouldn’t be sure how to make it work with a (large) collection of images. Others here no doubt do however.

-Chris

I have used Find Image very extensively, for many purposes, including purposes like this. I don't even want to tell you how many hours I've used Find Image because you wouldn't believe me.

So I'm happy to help with that approach, if MH wants help with that approach. But I don't understand the problem. E.g., the original post said this is a problem with modifying links in Keynote, but the uploaded image ("My Annual Calendar Page") was a calendar which doesn't seem to be related to Keynote at all. What I need to see, at a minimum, is an image of the Keynote document that you want to edit.

Yea I recall at least a few posts where you shared some brilliant found image loops! @michaelhyatt Sleepy is the man for looping through found images.

Hey Sleepy, I had to wait a minute for the initial image to open in Dropbox's website, and then click the download link. It confused at first too.

Thanks for your willingness to help. You can download the Keynote document from the link in my response to Chris. This file has 377 rectangles. I’m attaching the hyperlinks to those, one per shape.

The problem is that all the shapes are identical—only the position on the page is different. My plan was to select the shape, invoke the Keynote hyperlink command, insert the slide number I want to link to, and then move to the next shape.

So for example, I would start the macro, and it would select the shape that covers January 1st, insert a hyperlink on that shape that links to the January 1st day page, then select the shape associated with January 2nd, and then keep looping until all 365 days are done.

Ok, I found the keynote file. Am I correct that you want to click on each hyperlink AND you want to edit each hyperlink with a string value that corresponds to the day or the month? Can you please show me an example of what string you want to set? Please show me one value for a calendar day and one value for a month.

As you can see from this quick screenshot, the Find Image action is missing a few of the images. This is not unusual behaviour, and I may have to pull a few tricks from my sleeve to make this work.

However I already have one idea/trick that I think you need. I notice that in your sample keynote file, all the links are currently empty. I think it would be very helpful if, instead of being empty, you had each one storing the name of the date or month that the link is pointing to. That would make solving this much easier. I suspect you would be willing to do that, right? That's because our solution could figure out what the found link represents by its initial contents. Does that make sense? Is that an initialization step you would agree to?

Using some of my tricks I have been able to increase the success of the Find Image from about 50% (see image above) to about 95%. But that's not good enough yet. If it isn't 100% you probably don't want it. I still haven't exhausted my bag of tricks yet.

EDIT: I thought those icons represented hyperlinks, especially since you called them hyperlinks. But they aren't. They are just links to slide numbers. Just a number. Now I'm confused about the goal. Are you just trying to put a number in these links? Wouldn't these numbers always be the same for each day of the year? I don't understand yet what you want to store in these links. You said "insert the slide number I want to link to"... is this determined programmatically or do you do this by hand? I need to understand what you are trying to do. SUPER EDIT: SIGH, I found the hyperlink option. So my original question stands... are you willing to pre-populate the links with hyperlinks that contain the text corresponding to the month/date of the link? This would be very helpful.

Even if I can't get Find Image to work, I have another approach that will work. It's just a little more work.

Here's a little psudeocode that shows how to solve this problem with using only a single Find Image action.

  1. Set up the Keynote document so that all dates are visible on the screen at once.
  2. Carefully make estimates as to the X,Y locations of all the cells in the spreadsheet. I can help explain this step. IMPORTANT: you don't have to worry about cells that are empty, like the empty spaces between months. All you need is a simple nested loop that moves the pointer over each cell briefly.
  3. Click on the "candidate cell" with the mouse.
  4. Use the Find Image action to see if an Edit button appears near the mouse. If it does, click on it, (otherwise just continue the loop.) From there, change the pull-down to Hyperlink. Use the hyperlink's default contents to know what day it is, and from there insert the data you want.
  5. Repeat until end of X and Y loops.

If that gives you a good idea to implement a solution, go for it. If you want further advice, ask away. My original plan took a back-burner because Find Image doesn't usually work well when there's over 300 qualifying images to find. I have tricks to make it work, but the above code is probably much simper and better.

A simpler way to move between objects is to just press the Tab key.

Thanks so much for spending this much time on this!

Here's the ultimate result I want. The user taps on one of the days, and it takes him to another slide in the deck where there’s a Day Page for that date. Each day of the year has it's own slide. So the "hyperlink" links to a specific slide number. For example, Jan 1 might be sllide 30, Jan 2 to slide 31, and so forth. Of course, the entre deck is exported as a PDF, so the the slides are essentially pages.

Alright. but why wouldn't the same dates (eg, Jan 1) always link to the same page (eg, page 30)? Is this just a task that you need done once, or do you need it done again every year, with different page numbers for the same dates? Until I understand how you are determining which page numbers correspond to which dates, I'm not sure what we are trying to do. Do you determine it manually or by some algorithm? Does the value change each year, or is this a one time job that you will never repeat? These things will affect how the code is written.

By the way, you are still using the term "hyperlinks." But we are not talking about web pages, just page numbers. Keynote doesn't refer to page numbers as hyperlinks, just as page numbers.

I was using the term hyperlink, because that is the term used in the PDF planner industry. It's a link that is external to the current slide. Keynote actually uses the term "Links." It uses the same linking dialog box for links to slides, webpages, emails, and phone numbers.

This is a task that will be done every quarter—that's why I want to automate it to save me the time of doing it manually. The values change with each year, because the dates are in a different place each year. For example, in 2022, January 1 was a Saturday. In 2023, January 1 is on Sunday.

I don't really need help with the algorithm part of asking which slide to link to. I can figure that out. I just need help with selecting a specific shape.

Maybe the best approach is to click on the shape for the first of the month, then use the tab key as @tiffle suggested to move to the next shape.

Thanks again for interacting with me. It’s forcing me to get more clear on what I'm trying to do.

Alright. I don't mind if you go with someone else's solution. But my pseudocode above should be 100% reliable and quite simple to code. The way my code works is by selecting every possible shape using the mouse and a pair of nested loops using X,Y values. When it clicks on a valid shape, a popup window appears. That's the basis of my solution.

I think I need to run an errand now.

I obviously tried doing that with your Keynote document before making my suggestion and while it works, the order in which the objects are “visited” might require you to be creative with your algorithm!

I'm wondering if I can rearrange the objects, so the tab moves to the next object in the right order.

Maybe, but with other drawing-type apps I’ve used, the objects are sequenced by order of creation but I don’t know if that’s true of Keynote. But you could certainly try moving them around as potentially you’ll be saving a lot of time in the long run.