Image replacement in Keynote document using applescript

Hi Friends,


I have a task for replace the content [text] and the images in the keynote document.


Replacement text is in the pages document.


Replacement images are in csv format as like below.



I tried the below code, but I can't get the solution.


-- Path to the CSV file with slide numbers, old image names, and replacement image names
set csvFilePath to "/Users/xxx/Desktop/Keynote Image Report1.csv"
set replacementImageFolder to "/Users/xxx/Desktop/Replacement_image" -- Folder containing the replacement images


-- Read the CSV file
set imageData to read file (POSIX file csvFilePath) as «class utf8»


-- Split the data by line
set imageEntries to paragraphs of imageData


tell application "Keynote"
	if (count of documents) > 0 then
		set currentDoc to the front document
		
		-- Loop through each line in the CSV (skipping header row)
		repeat with i from 2 to count of imageEntries
			set imageEntry to item i of imageEntries
			set textItems to my splitText(imageEntry, ",")
			
			if (count of textItems) ≥ 3 then
				-- Extract slide number, old image name, and replacement image name
				set slideNumber to item 1 of textItems
				set slideNumber to slideNumber as integer -- Ensure slideNumber is an integer
				set oldImageName to item 2 of textItems
				set replacementImageName to item 3 of textItems
				
				-- Find the target slide
				try
					if (slideNumber > 0 and slideNumber ≤ (count of slides of currentDoc)) then
						set targetSlide to slide slideNumber of currentDoc
						
						-- Loop through all images in the target slide to find the image
						set imageFound to false
						repeat with j from 1 to (count of images of targetSlide)
							
							set currentImage to image j of targetSlide
							
							set currentShape to shape j of targetSlide
							
							set imageName to file name of currentImage
							
							display alert "imageName: " & imageName
							
							if (imageName is oldImageName) then
								set imageFound to true
							end if
							--set currentImage to image j of targetSlide
							
							-- Attempt to match the image by `name`, fallback to other criteria
							
							if imageFound then
								-- Get the current position and size of the old image
								set imagePosition to position of currentImage
								
								display alert "imagePosition: " & imagePosition
								
								set imageSize to size of currentShape
								
								display alert "imageSize: " & imageSize
								
								
								-- Delete the old image
								delete currentImage
								
								-- Construct the path for the replacement image
								set replacementImagePath to (replacementImageFolder & "/" & replacementImageName)
								
								-- Make sure the replacement image file exists
								set replacementImageFile to POSIX file replacementImagePath
								if not (do shell script "test -f " & quoted form of replacementImagePath & "; echo $?") is "0" then
									display dialog "Replacement image file not found: " & replacementImagePath buttons {"OK"} default button "OK"
									exit repeat
								end if
								
								-- Add the replacement image at the same position and size
								set newImage to make new image at targetSlide with properties {file:replacementImageFile, position:imagePosition}
								set size of newImage to imageSize
								
								display dialog "Replaced image on slide " & slideNumber
								exit repeat -- Stop after replacing the first matching image
							end if
						end repeat
						
						if not imageFound then
							display dialog "Image " & oldImageName & " not found on slide " & slideNumber buttons {"OK"} default button "OK"
						end if
					else
						display dialog "Slide " & slideNumber & " is out of bounds or not found." buttons {"OK"} default button "OK"
					end if
				on error errMsg number errNum
					display dialog "Error on slide " & slideNumber & ": " & errMsg buttons {"OK"} default button "OK"
				end try
			end if
		end repeat
	else
		display dialog "No Keynote document is open." buttons {"OK"} default button "OK"
	end if
end tell


-- Helper function to split text by a delimiter
on splitText(theText, delimiter)
	set {oldDelims, AppleScript's text item delimiters} to {AppleScript's text item delimiters, delimiter}
	set theItems to every text item of theText
	set AppleScript's text item delimiters to oldDelims
	return theItems
end splitText


I planned to write the code in applescript to achieve this, but I can't able to get the solution.


Can you please help me to out from this?


Thanks

Asuvath

MacBook Pro (M1, 2020)

Posted on Nov 19, 2024 08:43 AM

Reply
3 replies

Nov 26, 2024 10:18 AM in response to asuvathdhaman

There's no formal document that I'm aware of. The best (although somewhat outdated) resource for Keynote is likely https://iworkautomation.com/keynote/index.html


Like most apps, the source-of-truth is the application's dictionary (File -> Open Dictionary...) which will tell you what commands it supports, and its object model, parameters, etc., but in a kind of 'formal' way - i.e. "these are the things I understand, but it's up to you to work out how to put them together".


Case in point, you can look at the dictionary and it will tell you that a 'shape' has separate 'height' and 'width' properties, but it's up to you to determine how to apply them.


However, nothing there will tell you that make's at parameter doesn't help you create a shape on a slide... that's kind of an experience thing.

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.

Image replacement in Keynote document using applescript

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