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 23, 2024 11:30 AM

> First, my scripts and SVG file are stored in the same directory as my Numbers spreadsheet. Rather than hardcode the path I want to build it programmatically based upon some manipulations of '(path to me)' for the helper script.


path to me returns the path to the running application, not the current document.


To get the current document's path, ask it for its file:


tell application "Numbers"
	tell document 1
		set f to its file		
	end tell
end tell


Now you have the path to the .numbers file. There are a couple of ways of finding the directory from there - the easiest is to ask the Finder.


Here's a revised script that looks for a file in the same folder as the .numbers file:


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

tell application "Numbers"
	set docPath to file of document 1
	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
		
	end tell
end tell

-- have to drop out of Numbers here due to property name conflicts
tell application "Finder"
	set d to container of (file docPath) -- find the folder containing the .numbers file
	-- build a path to the image by combining the folder path and the image name
	set newImagePath to (file fn of d) as text
end tell

-- and back to Numbers
tell application "Numbers"
	tell sheet 1 of document 1
		
		set newImage to make new image with properties {file:newImagePath}
		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


With a little work you could add a safeguard to verify the file exists before deleting the current one

18 replies

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.

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.