Hello,
I’m looking for a solution to copy values from one numbers-document into another numbers-document using applescript.
Therfore I want to generate a script which procedes the following steps:
Numbers document A sheet 1 table 1 cell “A3” → value of cell “A3” should be copied to clipboard → value in clipboard should be pastetd in document B sheet 1 table 1 in cell “B4”
As I am really beginner in Applescript I’m hoping to get here an example or some kind of template to realize this
thank you in advance
Rainer
Honestly, I’m not sure that using the clipboard is the good solution.
The normal scheme would be :
tell application "Numbers"
tell document "Doc 1.numbers" to tell sheet 1 to tell table 1
set aValue to value of cell "A3"
end tell
tell document "Doc 2.numbers" to tell sheet 1 to tell table 1
set value of cell "B4" to aValue
end tell
end tell
Of course you may use the clipboard with :
tell application "Numbers"
tell document "Doc 1.numbers" to tell sheet 1 to tell table 1
set the clipboard to value of cell "A3"
end tell
tell document "Doc 2.numbers" to tell sheet 1 to tell table 1
set value of cell "B4" to the clipboard
end tell
end tell
But I don’t see the interest of this scheme.
I just know two cases where using the clipboard is useful.
(1) when we want to copy paste several cells in a single operation
(2) when the selection contains at least a date-time value.
In such case, we are supposed to use:
tell application "Numbers"
tell document "Doc 1.numbers"
-- set active sheet to sheet 1
tell sheet 1 to tell table 1
set selection range to range "A3"
my CopyThat()
end tell
end tell
the clipboard as text -- justb to check that CopyThat did its duty
delay 0.2
tell document "Doc 2.numbers"
-- set active sheet to sheet 1
tell sheet 1 to tell table 1
set selection range to range "B4"
my PasteHere() -- I don't understand why, this evening the contents of the clipboard isn't pasted
end tell
end tell
end tell
on CopyThat()
tell application "System Events" to tell process "Numbers"
set frontmost to true
keystroke "c" using {command down}
end tell
end CopyThat
on PasteHere()
tell application "System Events" to tell process "Numbers"
set frontmost to true
keystroke "v" using {command down}
end tell
end PasteHere
Alas, as I wrote in a comment, I don’t understand why, this evening the contents of the clipboard isn’t pasted.
Maybe I have corrupted something during tests of a facetious script.
I will re-test tomorrow.
Yvan KOENIG running High Sierra 10.13.6 in French (VALLAURIS, France) mardi 21 avril 2020 22:34:22
Hello Yvan,
thanks a lot for your suggestions !!!
I tried the “normal scheme” …I changed the filenames (“Test1.numbers” instead of “Doc 1.numbers”)…however I got an error -1728 “document "Test1.numbers"“ cannot be read.”
Both numbers-files (Test1 and Test2) are open on the desktop. Is it nescessairy to put in a path (instead of the filename?) ?
Rainer
I guess that when you write : Both numbers-files (Test1 and Test2) are open on the desktop I must understand that the icons of the documents are available on the desktop.
It’s clear that the documents aren’t open in Numbers.
My scripts assume that the documents are open in the application.
As they are clearly not (alas I’m not a sooth sayer and can’t guess what you didn’t say) some extraneous code is logically required.
The first script would be :
set p2d to (path to desktop as text)
set file1 to (p2d & "doc 1.numbers") as «class furl»
set file2 to (p2d & "doc 2.numbers") as «class furl»
tell application "Numbers"
set doc1 to open file1
set doc2 to open file2
tell doc1 to tell sheet 1 to tell table 1
set aValue to value of cell "A3"
end tell
tell doc2 to tell sheet 1 to tell table 1
set value of cell "B4" to aValue
end tell
end tell
I let you modify the 2nd one accordingly.
At last, I found why the 3rd one failed.
We must explicitly bring the used document at front.
set p2d to (path to desktop as text)
set docName1 to "doc 1.numbers"
set file1 to (p2d & docName1) as «class furl»
set docName2 to "doc 2.numbers"
set file2 to (p2d & docName2) as «class furl»
tell application "Numbers"
set doc1 to open file1
set doc2 to open file2
my bringToFront(docName1) -- pass the name of the do
tell doc1 to tell sheet 1 to tell table 1
set selection range to range "A3"
my CopyThat()
end tell
my bringToFront(docName2)
tell doc2 to tell sheet 1 to tell table 1
set selection range to range "B4"
my PasteHere() -- I don't understand why, this evening the contents of the clipboard isn't pasted
end tell
end tell
on CopyThat()
tell application "System Events" to tell process "Numbers"
set frontmost to true
keystroke "c" using {command down}
end tell
end CopyThat
on PasteHere()
tell application "System Events" to tell process "Numbers"
set frontmost to true
keystroke "v" using {command down}
end tell
end PasteHere
on bringToFront(aName)
tell application "System Events" to tell process "Numbers"
set frontmost to true
tell menu bar 1 to tell menu bar item 12 to tell menu 1 to click menu item aName
end tell
end bringToFront
Yvan KOENIG running High Sierra 10.13.6 in French (VALLAURIS, France) mercredi 22 avril 2020 12:27:32
What’s not clear?
The file reference and the document property of the Numbers.app are 2 different things. The 1st is the address of the 1st byte of the file on the disk, the 2nd is the address of the 1st byte of the already opened file in the RAM memory occupied by the Numbers.app in the active state.
To make it easier to understand, I wrote the script of Yvan Koenig like this:
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 -- that is, copy file's contents from disk to RAM of Numbers.app
set document_2 to open file_2
set (value of cell "B4" of table 1 of sheet 1 of document_2) to ¬
(value of cell "A2" of table 1 of sheet 1 of document_1)
return {name of document_1, name of document_2} -- I added this only to see the names
end tell
Using clipboard:
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 -- that is, copy file's contents from disk to RAM of Numbers.app
set document_2 to open file_2
set the clipboard to (value of cell "A2" of table 1 of sheet 1 of document_1)
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} -- I added this only to see the names
end tell
Are file name extensions hidden on fishrain’s machine? If so, the names of the Numbers documents will be just “Test1” and “Test2”.
Great support here !! Thank you all !
Indeed it was quite simple: as Nigel already suspected…both documents were open in the application but it didn’t work because the file-extension “.numbers” were hidden…
I changed the file-names to Test1 and Test2 (without .numbers) and everything works fine !!!
Rainer