applescript keynote

Trying to delete all images on a slide with applescript without a repeat statement, which is slow.

tried this, but it does not select the scroll area. I t deletes the new slide:

activate application "Keynote"

tell application "Keynote"

tell the front document

duplicate the first slide

end tell

tell the last slide

end tell

tell application "System Events"

tell process "Keynote"

click scroll area 1 of front window

keystroke "a" using {shift down, command down}

key code 51

end tell

end tell

end tell

Mac mini, macOS 11.6

Posted on Feb 12, 2023 1:29 AM

Reply
Question marked as Top-ranking reply

Posted on Feb 13, 2023 12:38 PM

I tried, but I didn't understand what you're trying to do...


> Trying to delete all images on a slide with applescript without a repeat statement


Why do you think you need a repeat statement?


This script duplicates the first slide, then deletes every image from the new copy:


tell application "Keynote"
	activate
	tell the front document
		duplicate the first slide
		tell the last slide
			delete every image
		end tell
	end tell
end tell


Generally, using UI Scripting (keystrokes, clicks, etc.) is a last resort, only to be used when there isn't a more direct way of doing something.

3 replies
Question marked as Top-ranking reply

Feb 13, 2023 12:38 PM in response to curtisfromhouston

I tried, but I didn't understand what you're trying to do...


> Trying to delete all images on a slide with applescript without a repeat statement


Why do you think you need a repeat statement?


This script duplicates the first slide, then deletes every image from the new copy:


tell application "Keynote"
	activate
	tell the front document
		duplicate the first slide
		tell the last slide
			delete every image
		end tell
	end tell
end tell


Generally, using UI Scripting (keystrokes, clicks, etc.) is a last resort, only to be used when there isn't a more direct way of doing something.

Feb 13, 2023 2:40 PM in response to curtisfromhouston

> but I can't find Cut, Copy, or Paste commands in the Keynote dictionary


My advice - forget copy and paste.


Copy and Paste constitute a distinctly human interactive model that dates back, what, 40 years at this point.


Instead, consider AppleScript variables as an infinite field of clipboards. Compared to a single clipboard.


Or, if it helps, think of 'the clipboard' as a single AppleScript variable á là Highlander - i.e. "there can be only one". This is especially true when you think system-wide... there may be other data on the clipboard that other applications are using, and you just walked all over that.


At the end of the day, you can simply set a variable to any data - that's the equivalent of 'copy' into a named clipboard

You can usually make new objects with those variables to provide the input data - that's the equivalent of 'paste'

You can usually duplicate an object in place (the equivalent of copy/paste in one step.


None of the above affect the system clipboard.


For manipulating slides, in Keynote, assuming you have the images on disk you can use something like this to import an image onto the current slide:


		tell the last slide
			delete every image
			make new image with properties {file:choose file}
		end tell


You also have flexibility in things like sizing and location:


		tell the last slide
			delete every image
			make new image with properties {file:choose file, width:400, height:250, position:{250, 90}, rotation:15, reflection showing:true, opacity:80}
		end tell


Although, for reasons I don't understand, Keynote doesn't permit duplicating an image from one slide to another. This makes using placeholders a bit of a PITA (and something Apple should fix, IMHO (are you listening, Apple?). So if you have a slide with the target image on it, I'd start with that, or you can use something like this to replace in-situ:


tell application "Keynote"
	activate
	tell the front document
		duplicate the first slide
		tell the last slide
			set currentImage to image 1
			set {x, y, pos, rot, opac, reflec} to currentImage's {width, height, position, rotation, opacity, reflection showing}
			delete currentImage
			make new image with properties {file:choose file, width:x, height:y, position:pos, rotation:rot, opacity:opac, reflection showing:reflec}
		end tell
		
	end tell
end tell


This script 'copies' several parameters of the image (size, location, orientation, etc.) into a number of variables which are then used as the inputs to a make new image command. The new image takes on those parameters.


Feb 13, 2023 1:50 PM in response to curtisfromhouston

Thanks for your help. I found a similar solution. I deleted everything from the first slide, then duplicated it in the first script that copies and pastes images to it. I don't like using System Events, but I can't find Cut, Copy, or Paste commands in the Keynote dictionary, so I copy before activating Keynote, then use System events to send the keystrokes to paste.

This thread has been closed by the system or the community team. You may vote for any posts you find helpful, or search the Community for additional answers.

applescript keynote

Welcome to Apple Support Community
A forum where Apple customers help each other with their products. Get started with your Apple Account.