You can make a difference in the Apple Support Community!

When you sign up with your Apple Account, you can provide valuable feedback to other community members by upvoting helpful replies and User Tips.

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

Applescript Copying a table between documents

Hi All,


SG was so kind to help out with a script that opened 2 documents and copied/pasted a table from one to the other document.

I wanted to modify the script to work with both documents already opened.


I have modified (possibly broken) SGs script to this:


tell application "System Events"

tell process "Numbers"

set frontmost to true

-- Selects Document1

click (first menu item whose name contains "Document1") of menu "Window" of menu bar 1

end tell

end tell


tell application "Numbers"

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}

end tell


tell application "System Events"

tell process "Numbers"

set frontmost to true

-- Selects Document2

click (first menu item whose name contains "Document2") of menu "Window" of menu bar 1

end tell

end tell


tell application "Numbers"

tell application "Numbers" to activate

delay 0.4

tell application "System Events" to keystroke "v" using {command down}

end tell


Its seems to work, but it does not paste the copied table, but this:




What am I doing wrong here?



Best...Jan


Posted on Jan 5, 2022 11:12 PM

Reply

Similar questions

4 replies

Jan 6, 2022 11:22 PM in response to SGIII

Thank you so much for you efforts!!!!!!



I think the delay might have been contributing factor, so "2" is a good setting.


I could not get the paste part working, and since I work with .csv I changed the numbers document part of your script, and now this works for me.


tell application "System Events"

tell process "Numbers"

set frontmost to true

click (first menu item whose name contains "Document1") of menu "Window" of menu bar 1

end tell

end tell


tell application "Numbers"

-- put focus on source document and copy table to clipboard

set t to table 1 of sheet 1 of front document

-- select all the cells in the table for copying

tell t to set its selection range to range (name of its cell range)

activate -- return the focus to Numbers before copying

delay 2 -- vary as needed to give UI time to catch up

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

end tell


tell application "System Events"

tell process "Numbers"

set frontmost to true

click (first menu item whose name contains "Document2") of menu "Window" of menu bar 1

end tell

end tell


tell application "Numbers"

activate -- return the focus to Numbers before copying

delay 2 -- vary as needed to give UI time to catch up

tell application "System Events" to keystroke "v" using {command down}

end tell



Thank you so very much!!!!!



Best...Jan



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

This seems to be working reliably on my machine. You may need to vary the "delays" to get it to work on yours. Both Doc 1 and Doc 2 are assumed to be already open. If you rename to match your setup be sure to include the .numbers file extension.


set sourceDocName to "Doc 1.numbers"
set targetDocName to "Doc 2.numbers"

tell application "Numbers"
	-- put focus on source document and copy table to clipboard
	set t to table 1 of sheet 1 of document sourceDocName
	-- select all the cells in the table for copying
	tell t to set its selection range to range (name of its cell range)
	activate -- return the focus to Numbers before copying
	delay 2 -- vary as needed to give UI time to catch up
	tell application "System Events" to keystroke "c" using {command down}
	
	-- "opening" an already open document puts the focus on it for pasting
	open (POSIX path of (file of document targetDocName as alias) as text)
	delay 1.5 -- vary as needed to give UI time to catch up
	tell application "System Events" to keystroke "v" using {command down}
end tell



SG

Applescript Copying a table between documents

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