I have a simple spreadsheet of textual information in a Numbers document. I want to copy the string data from individual cells so it can be pasted and formatted into a Pages document and saved for later editing.
Each row has 5 columns. Each cell has a piece of text I need to copy, paste over into a Pages document, and then repeat, until one row has been completed. Then the Pages document will need to save and close, and then open up a new blank document for the repeat.
On repeat, the applescript should step down to the next row and repeat for all 5 columns.
Right now, I’m having trouble even getting a script to properly select a single cell at a time. I’m using the following code, but it looks like it’s selecting the entire row.
tell application "Numbers"
activate
tell table 1 of sheet 1 of document "KK-6July-1"
repeat with i from 2 to row count
set selection range to row i in column 1
Can anyone point me in the proper direction to select a single cell at a time, copy the string data, and paste it into a Pages document? Thank you.
I went through your code and even tried to run it, but nothing happened and even though I know the AppleScript is all essentially the same, I’m having a tough time getting around the French involved. Sorry about that.
Row 1 is the header row. Beginning on row 2, is my first complete series of information. Each row represents an article that needs to be written. I want my script to:
Copy cell B2 and make it the document name in a new Pages document. Then it will copy that same string to very first line of the document and make it bold.
Skip two lines and then copy cell A2 in italics to the Pages document.
Skip two lines and then copy cell C2 in bold to the Pages document.
Skip two lines and then copy cell D2 in bold to the Pages document.
Skip two lines and then copy cell E2 in bold to the Pages document.
Add some other automatic text that it the same every time.
Save the Pages document in the proper location and close it.
I know how to use System Events to execute the copy and paste, and other automatic keystrokes, but I’m having trouble getting Numbers to select single cells so I can just get to step 1.
I feel like the best strategy might be executing a copy / paste for all the data, then moving over to Pages and spitting it out, one instruction at a time. Not sure, though.
Right now the biggest issue I am having is setting text from my Numbers spreadsheet as strings to my various variables. It doesn’t seem like I’m doing it right, according to AppleScript.
tell application "Numbers" to tell document 1 to tell sheet 1 to tell table 1
--repeat with i from 2 to 26
set title to (text of cell 2 of row 2)
It keeps saying “error “Numbers got an error: Can’t get every text of cell 2 of row 2 of table 1 of sheet 1 of document 1.” number -1728 from every text of cell 2 of row 2 of table 1 of sheet 1 of document 1”
Not sure how to tell it to parse out the text from a cell, in order to make that my string for my variable.
The error message tell you exactly what you are doing wrongly.
As we may see in the dictionary, a cell as no “text” property.
cell‚n [inh. range > item] : A cell in a table
elements
contained by columns, ranges, rows.
properties
column (column, r/o) : The cell’s column.
name (text, r/o) : The cell’s coordinates (e.g., C3).
row (row, r/o) : The cell’s row.
value (any) : The cell’s value.
use :
set title to value of cell 2 of row 2
KOENIG Yvan (VALLAURIS, France) dimanche 7 juillet 2013 17:35:18
Yes, of course. Apologies for not realizing that sooner.
This worked. And I can see that my AppleScript is correctly grabbing the information I need. Now I’m having trouble assigning that data into Pages as necessary.
With my variable title, I attempt this:
tell application "Numbers" to tell document 1 to tell sheet 1 to tell table 1
repeat with i from 2 to 2
set title to value of cell 2 of row i as text
set keyword to value of cell 1 of row i as text
set H1 to value of cell 3 of row i as text
set H2 to value of cell 4 of row i as text
set H2 to value of cell 5 of row i as text
tell application "Pages"
make new document
set document name to title
end tell
And I receive this error: "error “Pages got an error: Can’t set document "Pages" to "FX Trading Rules For Survival".” number -10006 from document “Pages”
I tried setting it as a property also, and that just leaves the Pages document sitting as “untitled”.
I am attempting to edit some of the necessary items and am running into a roadblock with the file path. I wish to save the documents to /kris/Desktop/Freelancing/eLance/Forex/July/First 25 but when I input that into the script, I get a “file could not be saved” error.
I see the default is saving them to my documents folder and that won’t suit this situation.
Yes, okay. HFS+, got it. The saving is working properly, now.
I started toying with the code to cater it more precisely, and have run into trouble. There are five different segments of info, and I’ve renamed the variables so they make more sense to me when I lay them out.
Before, the keyword was getting placed first, followed by the three headers (H1, H2, h3). The end formatting should look like this (note that anything with a * never changes):
Title
Supporting paragraph 1.*
keyword
Supporting paragraph 2.*
H1
Supporting paragraph.*
H2
Supporting paragraph.*
h3
Supporting paragraph.*
But when I try to play with the way Pages is being told to “set body text”, I get the following error: "error "Pages got an error: Can’t set document "FX Markets and Currency Pairs" to "fx
"." number -10006 from document “FX Markets and Currency Pairs”"
on run
tell application "Numbers" to tell document 1 to tell sheet 1 to tell table 1
tell row 2
set keyword to value of cell 1
set title to value of cell 2
set H1 to value of cell 3
set H2 to value of cell 4 # I apologizes, this son of a b_tch change D2 to d2
set h3 to value of cell 5
end tell # row
end tell # Numbers
set docName to title
set targetFolder to (path to desktop as text) & "Freelancing:eLance:Forex:July:First 25" #edit to change folder location
set myNewDoc to my makeNewIworkDoc("Pages", docName, targetFolder)
set skip2lines to linefeed & linefeed & linefeed & linefeed
tell application "Pages" to tell document docName
set body text to keyword & skip2lines
set italic of first paragraph to true
set bold of last character to true
set last character to H1 & skip2lines
set bold of characters -3 thru -2 to false
set last character to H2 & skip2lines
set bold of characters -3 thru -2 to false
set last character to h3 & skip2lines
set bold of characters -3 thru -2 to false
set bold of last character to false
end tell # Pages
end run
I guess that the culprit is the instruction
set docName to title
It’s supposed to get the title of the Numbers document but
(a) you are not speaking to Numbers but at the script editor or to the script itself
(b) a Numbers document don’t have a title, it has a name.
Replace the culprit by :
tell application "Numbers" to tell document 1
set docName to name
end tell
if docName ends with "numbers" then
set docName to (text 1 thru -8 of docName) & "pages"
else
set docName to docName & ".pages"
end if
and I guess that you will get a better behaviour.
KOENIG Yvan (VALLAURIS, France) lundi 8 juillet 2013 17:12:04