Applescript to copy and paste a selection in Numbers app

Good day to all,

I am trying to write (ever so unsuccessfully) an applescript to copy and paste, within Numbers app, any given selection.

I found a solution using keystroke command "c" and command "v", please see below, but I did not like the elegance of it, or the troubles to compile correctly, for it took the some time to get the script to paste the contents in numbers, instead of pasting it in the script editor... oh boy...

The script below worked fine though... but I have the feeling it could be better

tell application "Numbers"
tell first table of the active sheet of the front document

	delete (columns -5 thru -1)
	set selection range to cell "A1"
	repeat 5 times
		add column before column "A"
	end repeat
	set the width of column "A" to 39
	set the width of column "B" to 307
	set the width of column "C" to 39
	set the width of column "D" to 60
	set the width of column "E" to 60
	set thisRangeName to ((name of the first cell of column "K") & ":" & ¬
		(name of the last cell of column "O"))
	set the selection range to range thisRangeName
	
	
end tell

end tell

tell application "Numbers"
activate
tell application "System Events" to keystroke "c" using {command down}
end tell

tell application "Numbers"
activate
tell first table of the active sheet of the front document

	set targetRangeName to ((name of the first cell of column "A") & ":" & ¬
		(name of the last cell of column "E"))
	set the selection range to range targetRangeName
	tell application "System Events" to keystroke "v" using {command down}
	
	
end tell

end tell

Hi @Ricabude, you can set the contents of a cell in Numbers using their value property. Off the top of my head, I think the syntax is something like

set the value of cell "A2" of table 1 of sheet 1 of the front document to ... 

and, of course, you can retrieve the clipboard contents using the clipboard. Combining these two would look like this:

set the value of cell "A2" of table 1 of sheet 1 of the front document to the clipboard

Thanks @CJK, but I am looking for a script to copy and paste any given block of cells, say:

  • copy cells "A1:D5", and paste it on "F1:I5", or any given range

OK, can you see how you might use what I've given to achieve that very goal ?

So, the value property of a cell can be read as well as set. Therefore, I think you can do something like this (inside a tell table 1 of sheet 1 of document 1 block):

set the value of cell "A1" to the value of the cell "F1"

That would copy the data from one individual cell into another. Doing it for a block of 15 cells is no different, as long as you can get the selection range that enclose the cells, you can then use a repeat loop (or two of them) to automate this.

@Ricabude, I had a play around with Numbers, having just downloaded it, and it seems there's no need for any of this. Copying and pasting a block of cells from one area to another is a built in feature of the Numbers app.

Have I misunderstood your objective here ? What do you wish an AppleScript to do that Numbers itself is currently not doing ?

Hi @CJK,

I saw that the combination of Applescripts with KM macros is very powerful , for one covers what the other lacks, and Numbers is an app I use often, and in my oppinion, has some cumbersome UI features, commands one can only acess through an enormity of clicks.

Because of that, I wrote already a bunch of KM macros for numbers, and many times, in the body of the macro/applescript, copying and pasting blocks of cells is necessary.

Following your advice, I've written something in this direction, please see below:

set srceRng to "F1:J16"
set destRng to "A1:E16"

tell application "Numbers"
   tell first table of the active sheet of the front document
      
      set destCol to range destRng's column 1's address
      repeat with c in range srceRng's cells
         set v to c's value
         set r to c's row's address
         set col to c's column's address
         set row r's cell (col - 5)'s value to v
      end repeat
      
   end tell
end tell

Thanks again

1 Like

Hey Ricardo,

You can also do something like this:

tell application "Numbers"
   tell first table of the active sheet of the front document
      
      set srcRangeValues to value of cells of range "F1:J16"
      set destCellList to cells of range "A1:E16"
      
      repeat with i from 1 to length of destCellList
         set value of item i of destCellList to item i of srcRangeValues
      end repeat
      
   end tell
end tell

It should be possible to set the value of a range of cells en mass, but I'm not seeing a way to do it with Numbers.

Every time I fool with Numbers I end up gratefully going back to Excel...

-Chris

2 Likes

Brilliant Chris !!! :wave::wave:

What an elegant solution!!!

You're right about Numbers, it is such a badly written app - so unlike everything else written for Mac, I simply don't understand... Sometimes I wonder if it was made by a begginer intern... :blush:

I like the graphic interface though, sharing possibilities and not having to deal with Microsoft omnipresent bugs, even on Mac environment...

Luckily, KM and Applescript are helping tremendously to make up for its flaws.

Thank you very much!!

1 Like

I’m still not seeing how this is any different to what Numbers does when you copy n paste the usual way, or how it’s going to take fewer keystrokes than ⌘-C, ⌘-V.

Hey Ricardo
You can also do something like this:

tell application "Numbers"
   tell first table of the active sheet of the front document
      
      set srcRangeValues to value of cells of range "F1:J16"
      set destCellList to cells of range "A1:E16"
      
      repeat with i from 1 to length of destCellList
         set value of item i of destCellList to item i of srcRangeValues
      end repeat
      
   end tell
end tell

This was really helpful to me today as I am just starting to learn Applescript for Numbers for some simple functions. Thanks!

1 Like