I'm also using Numbers 14.2, in Sonoma 14.6.1.
This particular project is for an Electoral College Calculator.
On a sheet, I built a table that has all of the states, including the district-level breakouts for Maine and Nebraska, a (hidden) column with their electoral values, and columns of checkboxes. I have summary headers that, based upon the status of the checkboxes, calculate the sum of the values of the electoral votes, and an additional column that checks each row for exactly one checkbox each. If there isn't exactly one checkbox for a given row, an arrow is displayed indicating it needs attention.
Additionally, I have an election countdown table, and a Tally check table that has a summary header indicating if the tally is complete (all 538 votes accounted for), if there are too few, or too many, and provides either a Tally Complete, Too Many, or Too Few votes, with instructions to check for arrows to identify rows requiring attention. The check table also displays a larger, color-coded tally for each candidate, and it displays different messages, based upon the final results.
And, I have a 'Notes' table, explaining the Split States of Maine and Nebraska and how they handle their electoral votes, notes on Data Sources, Color coding, and other relevant information about how the calculator works.
That all works perfectly.
My next addition was adding an SVG image of the US, which includes embedded stylesheet information allowing me to color code each state. That's the image I'm trying to replace programmatically using Applescript.
Now I'm doing some scripting to update the color coding. I've written a Ruby script which calls a helper Applescript which populates an array indicating to which candidate a state has been assigned based upon the checkbox values, then processes the array to update the CSS data within the SVG file. This also works as intended.
The last element I now want to automate is to replace the SVG image after the CSS data has been updated, by calling another helper Applescript. I had a script very similar to the one suggested, but, just like now, I get as far as deleting the old image, which works, but I still appear to be running into two distinct problems.
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. I've tried at least half-a-dozen different methods for that, but those all seem to return the error:
./replace_svg.scpt:1178:1188: execution error: Can’t make «class NmSh» 1 of document id "A99293C6-42C7-4F9D-8A0C-7E167B902E40" of application "Numbers" into the expected type. (-1700)
After deciding to ignore that for the moment (one problem at a time), adding a copy of my SVG to the desktop to test the rest of the script suggested by Camelot, their script appears to find the SVG file, but then instead returns the error:
./replace_svg.scpt:414:498: execution error: Numbers got an error: Unsupported item. (-2763)
So, I'm left wondering if Applescript for some reason is balking at adding an SVG file, even though I can add the file to the sheet manually without any problem.
(Ultimately, I'd love to add a button to trigger my other scripts, but that may be beyond Numbers capabilities directly. That might be something I can automate using something like a 'Shortcut', but that's a different matter, and moot unless I can get this part to work.)