I'm very new to AppleScript so bear with me. What I am trying to achieve is for every time I run the script, the current value of a cell is copied into a cell, one column to the right of when I last ran the script, so the first time it puts the value of A1 into B1, then A1 into C1, then A1 into D1 ect. How would I do this?
1 Answer
This is written as more of a proof of concept to show some logic in accomplishing the task. There may be a better way to code it, but I do not really do much AppleScript with Numbers, maybe someone else (CJK) can present some different code.
The following example AppleScript code was tested under macOS High Sierra and Numbers, version 5.1 (5683), with a default document using the basic blank template and as coded works per what was mentioned in the OP. The example AppleScript code assumes that Numbers is already running with the default document opened.
As coded, with set n to 1
, if the value of, e.g., A1 is not missing value, then the value of the next cell in that row whose value is missing value, then gets the value of the first cell of that row. In this case the value of, e.g., A1.
Setting set n to 2
, and so on, acts on that row and the value of that rows first cell, if not missing value, then get set to the value of the next cell of that row whose value is missing value.
Note: The setting or changing the value of a cell is not set until focus is removed from that cell. This normally occurs when pressing a key, e.g., tab, enter, arrow or when clicking elsewhere in the document with the mouse. I'm mentioning this so when you go to test the code and you typed something in e.g. A1, its value is still missing value until focus is moved elsewhere.
tell application "Numbers"
set n to 1
tell row n of table 1 of sheet 1 of document 1
if the value of its first cell is not missing value then
set v to value of its first cell
set c to its cell count
set i to 2
set flag to true
repeat while flag is true and i is less than c + 1
if the value of cell n of column i is missing value then
set its value of cell n of column i to v
set flag to false
else
set i to i + 1
end if
end repeat
end if
end tell
end tell
Note: The example AppleScript code is just that and does not contain any additional error handling as may be appropriate. The onus is upon the user to add any error handling as may be appropriate, needed or wanted. Have a look at the try statement and error statement in the AppleScript Language Guide. See also, Working with Errors. Additionally, the use of the delay command may be necessary between events where appropriate, e.g. delay 0.5
, with the value of the delay set appropriately.