Mark Sealey wrote:
Is it possible to have something like this without Numbers actually open, please: I want to put it into Automator and have the whole thing run unattended?
Hi Mark,
Yes, it is quite easy to have a script have Numbers open a document, insert tabbed values from the clipboard, and close the document. Just insert your document's path and name, the target sheet name, and the target table name in the property statements in the script below. (The script won't complain if the document happens to be open already when you run it.)
If you are uncertain of the whole path of your document you can locate it in Finder and drag it onto an open window in Terminal, then insert that result into the script.
This is one of those rare situations where so-called "gui scripting" is noticably more efficient than having a script assign values to cells one-by-one, especially if you have quite a few new rows to insert.
This script mimics the manually pasting of tabbed clipboard contents into a Numbers cell. You'll notice when you click a Numbers cell once and command-v to paste values the table expands automatically as much as needed. It does its thing much faster than a script that inserts values cell by cell.
As written the script assumes you do not have any blank rows at the bottom of the table. If you typically do have blank rows a small change in the script can handle that.
If you've specially formatted the table and want to preserve the format you can use
tell application "System Events" to keystroke "v" using {option down, shift down, command down}
Substitute your user name for sg3, or get the whole path via the terminal as described above.
Tested in Script Editor and in a Run AppleScript action in Automator.
SG
property doc : "/Users/sg3/Desktop/Test.numbers"
property sht : "Sheet 1"
property tbl : "Table 1"
tell application "Numbers"
opendoc
tell front document to tell sheet sht to tell table tbl
add row below last row
set selection range to last row's first cell
end tell
activate
repeat until frontmost
end repeat
tell application "System Events" to keystroke "v" using command down
close front document
end tell