0

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!

Alan
  • 3,815
  • 1
  • 26
  • 35

2 Answers2

1

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
Mockman
  • 966
  • 2
  • 6
  • 11
0

Upon further review, the earlier answer doesn't work, or no longer works. Apparently the name specifier...

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

... doesn't actually specify a name in that manner. Here is my ugly workaround:

tell the current slide
    set imageCount to the count of images
    repeat with i from 1 to imageCount
        set thisImage to image i
        if file name of thisImage = "myTargetFile.png" then
            set position of thisImage to {10, 10}
            exit repeat -- Ugly programming, look away.
        end if
    end repeat
end tell
Alan
  • 3,815
  • 1
  • 26
  • 35