Search code examples
applescriptkeynoteiwork

How do I select one specific image on the current slide in the current Keynote document?


Using AppleScript, I want to alter the x and y position of a particular image on every slide of a Keynote document. Once Keynote is running and activated, the following code works...

tell the front document
   set the current slide to slide 1
   tell the current slide
      set thisImage to image 1
      set position of thisImage to {10,10}
   end tell -- current slide
end tell -- front document

... but it's too fragile. If my image is not the first one ("image 1") then I'll be changing the position of the wrong object. I can't find anything in the scripting dictionaries or in on-line examples about expressing an object specifier for a particular image. I tried several variations on things like...

set thisImage to image with file name "my-file.png"

... to no avail. Any and all advice is appreciated. Thanks!


Solution

  • This works in Keynote 8.2, but to be honest, I'm not sure why.

    set thisImage to image {name:"my-file.png"}
    

    However, if you want to then ask thisImage what its name is, you have to ask for its file name, e.g.…

    file name of thisImage
    

    Every time I use any presentation software, I somehow like them all less.

    You have some redundancy (around 'current slide') so here is my take:

    tell slide 1 of the front document
        set thisImage to image {name:"my-file.png"}
        set position of thisImage to {10, 10}
    end tell
    

    Finally, since your objective is to cycle through every slide in the deck, you could try this:

    tell front document
        repeat with i from 1 to count of slides
    
            tell slide i to set position of image {name:"my-file.png"} to {10, 10}
    
        end repeat
    end tell