Applescript for Keynote - move shapes/images/text

Hello,

Firstly, sorry if this is in the wrong section, I’m not sure I fully understand the distinction between the different types of applescript subforums here :slight_smile:

I make educational videos on Keynote - often I like to use the ‘move’ animation to slide something onto the screen (that may be text/a shape/an image). I have to use the ‘move’ animation, and not ‘build in’ option to make it look the way I want.

Please could someone advise me on whether the following would be possible to script:

  1. Duplicate the slide
  2. Place all objects where they ended up, after being moved

This is because I want to start a new slide and see where everything would be after all the movement animations have happened, and I don’t think theres a way to do that in Keynote as it currently stands.

What I currently do is duplicate the slide, and C+P the X/Y coordinates of every item’s last ‘build’ position to those items - but manually doing this is a huuuuge pain when you’ve got a lot going on haha. So really I want to automate that copy and pasting procedure from the last build of every item to the items on a new slide.

If this is possible, is there anyone about who might produce this script for me (paid of course)?

Many thanks,

Tom

Hi Tom. Welcome to MacScripter.

You’re in the right section for help with writing your own AppleScripts. MacScripter isn’t generally a place to come just to get a script written for you, even if you’re willing to pay. But members may possibly choose to write one anyway if they find the topic interesting enough.

I don’t use Keynote myself, but looking at it this morning, it seems that duplicating a slide to the same document is easy enough, as is changing the positions of the duplicate’s contents. Getting the end-position set in a “Move” animation is slightly more complicated and requires GUI Scripting, which isn’t guaranteed to work the same way on everyone’s systems or on the same system forever. The following works for me with a test slide I set up in Keynote 9.2 in Mojave. My known assumptions are listed at the top:

(* This script uses GUI Scripting and has been tested with Keynote 9.2 in Mojave.

Assumptions:
	The user's language is English.
	The order of iWork items in a duplicated slide is the same as in the original.
	The "Animate" inspector will open on the animation actually set for an animated item when the item's selected.
*)

-- Identify the currently showing slide, count its contents, and duplicate it to the end of the slide list.
tell application "Keynote"
	activate
	tell front document
		set currentSlide to current slide
		set iWorkItemCount to (count iWork items of currentSlide)
		duplicate currentSlide to end of slides
	end tell
end tell

-- Show the "Animate" inspector if it's not already showing.
tell application "System Events"
	tell application process "Keynote"
		set frontmost to true
		tell radio button 2 of radio group 1 of toolbar 1 of front window
			if (its value is 0) then click
		end tell
	end tell
end tell

-- Working though the items in the original slide:
repeat with i from 1 to iWorkItemCount
	-- Select each one in turn.
	tell front document of application "Keynote" to set selection to iWork item i of currentSlide
	
	-- See if the "Move" "Action" appears in the "Animate" inspector. If it does, read off its "Position" values.
	tell application "System Events"
		tell front window of application process "Keynote"
			set hasMoveAction to ((value of radio button 2 of radio group 1 is 1) and (static text "Move" exists))
			if (hasMoveAction) then set {x, y} to {value of text field 3, value of text field 2} of scroll area 1
		end tell
	end tell
	
	-- If a Move Action is involved, set the position of the corresponding iWork item in the duplicate slide to the numbers from the position strings just obtained.
	if (hasMoveAction) then
		tell application "Keynote"
			set position of iWork item i of last slide of front document to {word 1 of x as number, word 1 of y as number}
		end tell
	end if
end repeat

I use this for pasting images of playing cards into a presentation and then using Magic Move to animate them. For this to work, the images you want to move must have a description, or you must know the Posix path to them. I paste the images in, so I include a description with each one. You’ll need to change it to suit your needs.


activate application "Keynote"
tell application "Keynote" to tell the front document
	duplicate the last slide
	tell the current slide
		set transition properties to {transition effect:magic move}
	
-- finding the desired image
	set cards to description of its image
		tell cards
			set cardslength to its length
			-- set target to (the description of the image you want)
			repeat with i from 1 to cardslength
				if item i = target then exit repeat
			end repeat
		end tell
		set card to its image i
--/finding the desired image
		tell card
			-- you have to define X and Y in advance
			set its position to {X, Y}
		end tell
	end tell
end tell

I appropriated the find routine from another site.