Replacing images in pages.

I’ve successfully been able to replace text by the tag property although I seem to be encountering some kind of bug, or naivety when it comes to images.

I’ve tinkered around with the following code and it seems that on occasion all three images will be replaced, but very rarely. I thought I was accessing the images directly by getting the name, although I’m wondering if with the layer tree, the images get incremented while replacing.



tell application "Pages"
	tell document "SampleImages.pages"
		set newImageOne to POSIX file "/Users/username/Desktop/one.png"
		set newImageTwo to POSIX file "/Users/username/Desktop/two.png"
		set newImageThree to POSIX file "/Users/username/Desktop/three.png"
		delay 0.5
		
		set imageOne to get file name of image 1
		set imageThree to get file name of image 2
		set imageTwo to get file name of image 3
		delay 0.5
		set itemOnePlaceholder to get image {file name:imageOne}
		set itemTwoPlaceholder to get image {file name:imageTwo}
		set itemThreePlaceholder to get image {file name:imageThree}
		delay 0.5
		
		set file name of itemOnePlaceholder to newImageOne
		set file name of itemThreePlaceholder to newImageTwo
		set file name of itemTwoPlaceholder to newImageThree
		
	end tell
end tell


It seems that it will replace one of the images twice…

Putting aside that I can’t get through the ‘itemOnePlaceholder’ lines, which maybe relates to my version of Pages (7.1), you do have the numbers crossed up.

e.g.
set imageThree to get file name of image 2
set imageTwo to get file name of image 3

and

    set file name of itemThreePlaceholder to newImageTwo
   set file name of itemTwoPlaceholder to newImageThree

Dunno if that’s causing your problem but it might warrant looking into.

Thanks for the pointer, although it doesn’t seem that it matters.

I updated it to be more legible to your point, and it still is producing inconsistent results:



tell application "Pages"
	tell document "SampleImages.pages"
		set newImageOne to POSIX file "/Users/username/Desktop/one.png"
		set newImageTwo to POSIX file "/Users/username/Desktop/two.png"
		set newImageThree to POSIX file "/Users/username/Desktop/three.png"
		
		set imageOne to get file name of image 0
		set imageTwo to get file name of image 1
		set imageThree to get file name of image 2
		
		
		set itemOnePlaceholder to get image {file name:imageOne}
		set itemTwoPlaceholder to get image {file name:imageTwo}
		set itemThreePlaceholder to get image {file name:imageThree}
		
		
		set file name of itemOnePlaceholder to newImageOne
		set file name of itemTwoPlaceholder to newImageTwo
		set file name of itemThreePlaceholder to newImageThree
	end tell
end tell


At least on the Catalina, Posix path property of file name property of image is read only. So, you can’t set file name properly. To replace images properly you should use other approach: 1) delete old image, 2) add new image using command make new image with properties {file:thisImageFile}.

See first example here: https://iworkautomation.com/pages/image.html
Example opens template document, deletes its images, then adds new images.

To replace some image in place, need additional steps. 1) You should get old image’s base properties: position , size , width , height and so on. 2) Scale new image to size of old image 3) add image to document 4) set its position to position of old image. As you can see now, it isn’t so simple, but is possible. And, correct

Thanks for the clarification of the process… I ventured down this route but kept arriving at this error: “ TMAScriptImageInfoProxy”. NOTHING on the internet about it. Then I realized that it requires a tell to the page.

Also, I tried doing it via the description from the website you provided as mentioned in the tip, but it produces the same result doing it by name.

Think this will do the trick! Finally able to move forward to the rest of the script.

Really appreciate the pointer!!