You can make a difference in the Apple Support Community!

When you sign up with your Apple Account, you can provide valuable feedback to other community members by upvoting helpful replies and User Tips.

Apple script for keynote to resize photos and place them on a slide with exact margins and alignments

Hey everyone,

I was wondering if it is possible to make my work a bit easier using Apple script which I am absolutely new to. So here is the task I am making a lot.


So here is an example:

  • I put from 4 to 8 photos on the same slide
  • resize them to height of 300
  • x margin = 40, y margin = 80, with 20 in between pictures
  • if it's more than 4, place them in 2 rows, second row is bellow the first row
  • put consecutive numbers in the left upper corner


So this is something I need to do for 30-50 slides a few times a month and I get the photos sometimes really late before the meeting to do that. Is there any way to make it automatically? I already use masks etc, but maybe placing, resizing and numbering could be a bit faster than just copy/paste and move manually?

MacBook Pro 13″, macOS 14.5

Posted on Oct 12, 2024 3:41 PM

Reply
5 replies

Oct 14, 2024 7:26 AM in response to Mina_Khlechyan

The AppleScript scripting dictionary for Keynote may harbor complexities that would make your goal of scripted automation either difficult to support or even implement. What I suggest you do is create one or two Keynote templates.


You make one template (aka Theme) with room for four pictures across, and the second Theme with two rows of four pictures. Save each as Keynote Themes from File menu > Save Theme… > Add to Theme Chooser.


Visit Keynote > Settings > Rulers and make these settings:



Close that panel and from the Keynote View menu > Show Rulers.


Begin by dragging/dropping an arbitrary image that you would normally use on to the Keynote slide. From the Keynote arrange panel, you set its initial position, height, and width to your specs. Next, you visit the Format menu > Advanced > Define as Media Placeholder. This means that when you reuse this template in the future, you can just drag/drop a new image on to that placeholder it will automatically become the replacement image.


Now, press the option key, click and drag the first Media Placeholder to your right with a 20 pt gap. The settings from that Ruler panel will provide automatic guides and measurements to assist you. Then, repeat this duplication effort for the next two media placeholder locations. For numbering, you will select a rectangular or circular shape from the Shape tool, double-click within it, and enter the centered number in it. Scale to need. Your choice of the background color for the shape and foreground color of the number text. You will need to place these numbered media items above the media placeholder as placing them within the media placeholder will prevent image replacement in future usage.


With your mouse, drag an imaginary box around your four images and numbered shapes to select them. On the Arrange panel, select group. Save this new Theme per above with a unique theme name.


Now, with this Keynote document remaining open, you will make the 8-image theme. Press the option key and click once on the 4-image group and simply drag downward to copy it. The automatic guides will help you. With the second row still selected, visit the Arrange panel and select Ungroup. Edit the number Shapes for 5 - 8, select all of the second row again, and from the Arrange panel, choose Group again.


Then, you will select File menu > Save Theme… and Add to Theme Chooser, but this time with a new name indicating 8-images.


In the future, you select which of these Themes you need and then drag and drop your new images over their respective placeholder media target. Dragging and dropping 4 - 8 images onto the appropriate Keynote slide should be more beneficial to your productive time than what you have now.

Oct 15, 2024 5:46 AM in response to Camelot

I had never written any AppleScript solution for Keynote and didn't have the personal time to tackle it as you have done. If the images are identical in size, your script works brilliantly to evenly space them, whether four or eight are selected. If the images are variously sized then this is the Keynote v14.2 result on Sequoia v15.0.1 using the Basic White theme:



Unfortunately, Keynote's scripting dictionary does not offer the ability to create media placeholders that would automatically scale new images to a specific size and clear up this alignment issue.

Oct 16, 2024 7:21 AM in response to Camelot

Not to detract from your excellent code effort, I knew that identically sized and placed media placeholders in a Keynote theme (e.g. template) would eliminate the irregular image size formatting issue. If all of the OP's images are the same dimensions, your code would suffice.


And with eight images of equal dimension, your script produces the following (for the OP's view) where the black couch nearly conceals the black image numbering text boxes:


Oct 14, 2024 12:09 PM in response to Mina_Khlechyan

Viking and I rarely disagree, but I thought this was entirely doable via AppleScript (although, granted, maybe not for the novice).


This is the script I came up with:


use AppleScript version "2.4" -- Yosemite (10.10) or later
use scripting additions

-- preset values
property marginX : 40
property marginY : 80
property margin_mid : 20
property imageHeight : 300


-- prompt the user for the image(s) to add
set the_images to choose file of type "public.image" with multiple selections allowed

-- count how many
set imageCount to count the_images

-- if more than 4 we need multiple rows
set numRows to 1 + (imageCount div 4)

-- work out how many images per row
if numRows > 1 then
	set images_per_row to 4
else
	set images_per_row to imageCount
end if

-- now the fun part
tell application "Keynote"
	-- target the front document
	tell document 1
		-- grab its dimensions
		set slide_w to its width
		set slide_h to its height
		
		-- make a new blank slide
		set newSlide to make new slide with properties {base layout:slide layout "Blank"}
		tell newSlide
			-- work out the area we have to work with, based on the margins defined above
			set canvasWidth to (slide_w - (2 * marginX))
			set imageWidth to (canvasWidth - (20 * (images_per_row - 1))) div images_per_row
			
			-- work out where to start
			set rowNum to 1
			set cur_x to marginX
			set cur_y to marginY
			
			-- now loop through the images
			repeat with i from 1 to imageCount
				-- add the image to the slide
				set new_image to make new image with properties {file:item i of the_images}
				tell new_image
					-- set its height
					set its height to imageHeight
					-- set its position
					set position to {cur_x, cur_y}
					
				end tell
				
				-- add the image label
				set imageLabel to make new text item with properties {object text:i, position:{cur_x + 10, cur_y + 10}}
				
				-- now work out where the next image should go
				-- are we at the end of the row?
				if (i mod 4) = 0 then -- end of row
					-- roll to the next row
					set cur_y to cur_y + imageHeight + margin_mid
					set cur_x to marginX
				else
					-- otherwise just move along this row
					set cur_x to cur_x + imageWidth + margin_mid
				end if
			end repeat
		end tell
	end tell
end tell



Oct 15, 2024 10:47 AM in response to VikingOSX

Yes, there is an issue with irregular-sized images. This is inherent to Keynote's dictionary where images scaled proportionately, and there is no way to set height and width independently without manually turning off proportional scaling on each image.


Since the OP only asked for a height setting, that seemed to fit the bill, but if the images are irregular, or if it needs different width settings, there may be some extra work involved.

Apple script for keynote to resize photos and place them on a slide with exact margins and alignments

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