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.

Looks like no one’s replied in a while. To start the conversation again, simply ask a new question.

scripting image replacement within numbers

I need to script an update within my Numbers file.


No NoCode 'shortcut' nonsense. I need an actual coding solution. Applescript worked great for the first part of my requirements, but can't seem to interact with the image I have inserted into one of my tabs (i need to replace my svg file to pick up changes made to it.)


Applescript lacks current (up to date) documentation or examples for manipulating numbers images (and, appears to be deprecated, because Apple seems to assume no one who can actually program wants to interact with applications any more).


Shortcuts suck, and are super limited (again, because Apple is trying to dumb things down, instead of actually allow folks who can program to do so).


I'm able to delete an image, but i can't replace it, nor can i insert a new image (which would also be a less elegant, but sufficient solution).

MacBook Pro (M1, 2020)

Posted on Sep 20, 2024 12:24 PM

Reply
Question marked as Top-ranking reply

Posted on Sep 20, 2024 2:31 PM

A subtle problem that, for sure, Apple could fix, but I think I understand why.


Ultimately, when you embed an image, the image is stored within the .numbers file and is, essentially, disassociated with the source file (especially bearing in mind that the image could be a file from disk or a photo from your Photos library. That's essentially why it doesn't update with changes to the source file.


The easiest workaround I could come up with is this:


ell application "Numbers"
	tell sheet 1 of document 1
		-- get the image
		set myImage to image 1
		-- grab its properties
		tell myImage
			set {fn, h, l, o, pos, refs, refv, rot, w} to its {file name, height, locked, opacity, position, reflection showing, reflection value, rotation, width}
		end tell
		delete myImage
		
		-- this assumes the image is on your desktop. Amend to your image path
		set newImage to make new image with properties {file:(POSIX path of (path to desktop) as text) & fn}
		tell newImage
			set {height, locked, opacity, position, reflection showing, reflection value, rotation, width} to {h, l, o, pos, refs, refv, rot, w}
			
		end tell
	end tell
end tell


It works by identifying the image (in this case I'm just assuming 'image 1'), and grabs all its properties.

It then deletes the image and immediately recreates it with the same filename (assuming it was a file on disk). The script then (re)sets all the image properties to match the original image.


Not quite the same as updating in-place, but pretty close.


Since the image is embedded, there is no record of where the original source image came from, so you'll have to set that part yourself.


This is essentially the same thing as happens if you click the 'Replace' button in the Image sidebar when the image is selected, but you wanted a scripted version.

18 replies

scripting image replacement within numbers

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