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)