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

Applescripting - open a document, and copy table to another document

Hi All,



I am trying to use Applescript to :

  1. Open a numbers document
  2. Copy the only table in there
  3. Pasting it into another numbers document


Can anybody help with how to copy/paste a table?


Many thanks.



Best...Jan

Posted on Jan 4, 2022 10:44 PM

Reply
Question marked as Top-ranking reply

Posted on Jan 5, 2022 7:37 AM

There is no direct AppleScript command to copy or paste tables. But you can use "gui-scripting" to accomplish the task, with something like this:


tell application "Numbers"
	-- open first document and copy table to clipboard
	set f to choose file with prompt "Choose a file" of type {"Numbers"}
	open f
	set t to table 1 of sheet 1 of front document -- selects the table
	tell application "Numbers" to activate
	delay 0.4
	tell application "System Events" to keystroke "c" using {command down}
	
	-- open second document and paste table from clipboard
	set f to choose file with prompt "Choose a file" of type {"Numbers"}
	open f
	tell application "Numbers" to activate
	delay 0.4
	tell application "System Events" to keystroke "v" using {command down}
end tell



SG

Similar questions

6 replies
Question marked as Top-ranking reply

Jan 5, 2022 7:37 AM in response to jklarsen

There is no direct AppleScript command to copy or paste tables. But you can use "gui-scripting" to accomplish the task, with something like this:


tell application "Numbers"
	-- open first document and copy table to clipboard
	set f to choose file with prompt "Choose a file" of type {"Numbers"}
	open f
	set t to table 1 of sheet 1 of front document -- selects the table
	tell application "Numbers" to activate
	delay 0.4
	tell application "System Events" to keystroke "c" using {command down}
	
	-- open second document and paste table from clipboard
	set f to choose file with prompt "Choose a file" of type {"Numbers"}
	open f
	tell application "Numbers" to activate
	delay 0.4
	tell application "System Events" to keystroke "v" using {command down}
end tell



SG

Jan 6, 2022 7:32 AM in response to jklarsen

This seems to be more reliable when opening the two documents with the script.


tell application "Numbers"
	-- open first document and copy table to clipboard
	set f to choose file with prompt "Choose a file" of type {"Numbers"}
	open f
	set t to table 1 of sheet 1 of front document
	-- select all the cells in the table
	tell t to set its selection range to range (name of its cell range)
	activate -- return focus to Numbers before copying
	delay 2
	tell application "System Events" to keystroke "c" using {command down}
	
	-- open second document and paste table from clipboard
	set f to choose file with prompt "Choose a file" of type {"Numbers"}
	open f
	activate -- return focus to Numbers before pasting
	delay 2
	tell application "System Events" to keystroke "v" using {command down}
end tell



SG

Applescripting - open a document, and copy table to another document

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