Want to highlight a helpful answer? Upvote!

Did someone help you, or did an answer or User Tip resolve your issue? Upvote by selecting the upvote arrow. Your feedback helps others! Learn more about when to upvote >

Looks like no one’s replied in a while. To start the conversation again, simply ask a new question.

Copying a range of cells from one sheet of one document to another sheet of another document using AppleScript

I have the following script to copy the value of cell A2 from one document to cell B4 of another document. What do I change in this script to have it copy a range of cells (be it numeric or text) of range A2:B20 from file_1 to the range of B2:C20 on file_2 instead ? Appreciate the help and expertise here. Thanks.


set file_1 to choose file of type "numbers" with prompt "Choose 1st file"

set file_2 to choose file of type "numbers" with prompt "Choose 2nd file"

tell application "Numbers"

set document_1 to open file_1

set document_2 to open file_2

set the clipboard to (value of cell "A2" of table 1 of sheet 1 of document_1) as text

delay 1

set (value of cell "B4" of table 1 of sheet 1 of document_2) to the clipboard

return {name of document_1, name of document_2} -- added this to see the names

end tell

Posted on May 27, 2022 4:05 PM

Reply
Question marked as Best reply

Posted on May 28, 2022 9:36 AM

As I am sure you have discovered gui-scripting can be fussy. It is too bad Numbers AppleScript support doesn't include a way to write a block of data. So either you're forced to loop through cells (slow) or figure out how to copy-paste.


I got this to work here (be sure so scroll to capture the couple of letters beyond the right margin):


set file_1 to choose file of type "numbers" with prompt "Choose Source file"
set file_2 to choose file of type "numbers" with prompt "Choose Dest file"

tell application "Numbers"
	set document_1 to open file_1 -- foremost
	tell document_1 to tell table 1 of sheet 1 to set its selection range to range "A2:B20"
	activate -- to put focus on Numbers app (not on the script itself)
	delay 0.5
	tell application "System Events" to keystroke "c" using command down
	
	set document_2 to open file_2 -- now this one is foremost
	tell document_2 to tell table 1 of sheet 1 to set its selection range to range "B4"
	-- activate -- doesn't seem to be needed here
	delay 0.5
	tell application "System Events" to keystroke "v" using command down
end tell





Depending on your machine you may be able to reduce the 0.5 delay.


SG


Similar questions

2 replies
Question marked as Best reply

May 28, 2022 9:36 AM in response to CJHENG27

As I am sure you have discovered gui-scripting can be fussy. It is too bad Numbers AppleScript support doesn't include a way to write a block of data. So either you're forced to loop through cells (slow) or figure out how to copy-paste.


I got this to work here (be sure so scroll to capture the couple of letters beyond the right margin):


set file_1 to choose file of type "numbers" with prompt "Choose Source file"
set file_2 to choose file of type "numbers" with prompt "Choose Dest file"

tell application "Numbers"
	set document_1 to open file_1 -- foremost
	tell document_1 to tell table 1 of sheet 1 to set its selection range to range "A2:B20"
	activate -- to put focus on Numbers app (not on the script itself)
	delay 0.5
	tell application "System Events" to keystroke "c" using command down
	
	set document_2 to open file_2 -- now this one is foremost
	tell document_2 to tell table 1 of sheet 1 to set its selection range to range "B4"
	-- activate -- doesn't seem to be needed here
	delay 0.5
	tell application "System Events" to keystroke "v" using command down
end tell





Depending on your machine you may be able to reduce the 0.5 delay.


SG


Copying a range of cells from one sheet of one document to another sheet of another document using AppleScript

Welcome to Apple Support Community
A forum where Apple customers help each other with their products. Get started with your Apple ID.