Scripting for Pages 5.2

Pages 5.2 restores some more support for Applescript. But this script, which one of the users here helped me write, works for Pages 4.3 but not 5.2. Can anyone help get it to work for 5.2?

–Script to create letter with Pages from selected Contact; works w/ Pages 4.3
– 1/17/2014
tell (current date)
set currentDate to its date string
set fileNameDateString to (its day as integer) & space & (its month as text) & space & year
end tell
– set desktopFolder to path to desktop as text
set mobileFolder to (path to library folder from user domain as text) & “Mobile Documents:com~apple~Pages:Documents:”
– ~/Library/Mobile Documents/com~apple~Pages/Documents
tell application “Contacts”
activate
set theSelection to selection
if theSelection is {} or class of item 1 of theSelection is not person then return
set thisPerson to item 1 of theSelection
set companyName to organization of thisPerson – will be “missing value” if none
set personName to name of thisPerson
set lastName to (last name) of thisPerson
set firstName to (first name) of thisPerson
set nName to (nickname) of thisPerson
set salutName to firstName
set nameVar to firstName & space & lastName
set theAddresses to addresses of thisPerson
if theAddresses is {} then return

if (count theAddresses) is 1 then
	set currentAddress to item 1 of theAddresses
else
	set addressList to {}
	repeat with i from 1 to count theAddresses
		set end of addressList to (i as text) & space & label of item i of theAddresses
	end repeat
	
	if nName is not missing value then
		set salutName to nName
	end if
	set chosenItem to choose from list addressList with title "Write a nice letter to " & nameVar with prompt "Pick an address"
	if chosenItem is false then return
	set addressIndex to (word 1 of item 1 of chosenItem) as integer
	set currentAddress to item addressIndex of theAddresses
end if
set addressStreet to street of currentAddress
set addressCity to city of currentAddress & space & state of currentAddress & space & zip of currentAddress
set addressCountry to country of currentAddress

end tell

set fileName to lastName & ", " & firstName & " " & fileNameDateString & “.pages”

tell application “Pages”
launch
activate
set currentDocument to make new document with properties {template name:“TNC small”}
tell currentDocument
set dateParagraph to make new paragraph
tell (current date) to get day & space & (its month as string) & space & year
set letterDate to result as text
set contents of dateParagraph to letterDate & return
–set fileNameDateString to (its day as text) & space & (its month as text) & space & year
–set contents of dateParagraph to fileNameDateString & return
set alignment of dateParagraph to right
set salutParagraph to make new paragraph at after last paragraph
set alignment of salutParagraph to left
set contents of salutParagraph to "Dear " & salutName & “:” & return & return & return & return & return
set nameParagraph to make new paragraph at after last paragraph
set alignment of nameParagraph to left
–set contents of nameParagraph to personName & return & addressStreet & return & addressCity
if companyName is not missing value then
set addressString to personName & return & companyName & return & addressStreet & return & addressCity
else
set addressString to personName & return & addressStreet & return & addressCity
end if
if addressCountry is not missing value and addressCountry is not “United States of America” then
set addressString to addressString & return & addressCountry
end if
set contents of nameParagraph to addressString
end tell
select insertion point before paragraph 3 of body text of document 1
tell application “System Events” to tell process “Pages”
key code 125 --down arrow
end tell
–save currentDocument in file (desktopFolder & fileName) – save to desktop
save currentDocument in file (mobileFolder & fileName) – save to mobile folder

end tell

I tried to compile your script.
It stopped upon the instruction supposed to create a document from a template.
You are using the old syntax when this one is changed and the dictionary is fair enough to give an example of the required syntax :

make new document with properties {document template:template “Essay”}

Yvan KOENIG (VALLAURIS, France) mercredi 2 avril 2014 17:17:52

Hmmm. When I try that, I get the syntax error:

Expected “,” or “}” but found “:”

Thanks. The template is working (perhaps there was a typo). Pages opens with the correct template, but then I immediately get the error:

Pages got an error: Don’t know how to create TMAScriptTextStorage

on the line

set dateParagraph to make new paragraph

the “make new paragraph” is highlighted.

I am eager to try this. Thanks so much for taking the time to parse this! Odd that there would still be “paragraph” but no way to “make new”!

Hmm: This line:

set character 1 of paragraph dateParagraph to letterDate

gives me this error:

error “Pages got an error: AppleEvent handler failed.” number -10000

I did write to Apple feedback about the issues you identified.

Can you send me this template you made?

I built a template containing height paragraphs of a single space.
I defined the first one as right aligned.

Maybe that was the problem

It’s exactly what I did.
You may get my template and the script at :

http://www.adrive.com/public/aNVRzF/4Pages5.2.zip

Yvan KOENIG (VALLAURIS, France) jeudi 3 avril 2014 21:54:17

Thanks. Will try it out.

Brilliant! You are a master at this stuff. IS there any way to position the cursor flush left one the line after the date? This worked in Pages 4.3 but Pages 5.2 doesn’t like it.

select insertion point before paragraph 3 of body text of document 1
tell application “System Events” to tell process “Pages”
key code 125 --down arrow
end tell

Thanks again for your help. It is nice to see the hand of a master at work!

Also, should this work to save the file?

save document 1 in file (mobileFolder & fileName) – save to mobile folder

it is not working

Here is more error info on trying to save the file from the “Replies” window: I don;t understand why it is not working

save “Doe, John 3 April 2014.pages” in file “Macintosh HD:Users:MyName:Library:Mobile Documents:com~apple~Pages:Documents:Doe, John 3 April 2014.pages”
Result:
error “Pages got an error: Can’t make "Doe, John 3 April 2014.pages" into type specifier.” number -1700 from “Doe, John 3 April 2014.pages” to specifier

May you check the name of the folder in which you try to save ?

On my machine, there is no folder named Mobile Documents.
There is one named Mobile Documents.123456789

To be honest, here I give a fave numerical value.
If I am serious enough to use the true folder name, what a surprise, the task is executed flawlessly.

Now I will try to write the instructions required to move the cursor.

Yvan KOENIG (VALLAURIS, France) vendredi 4 avril 2014 11:39:11

Hmm, I do have a folder called Mobile Documents, as well as one with a number. All the Pages (4.3) docs are under Mobile Documents. I will look into this further.

Thanks for the cursor and the folder stuff. Will try them today.

All works brilliantly except that I continue to get an error that Pages cannot make the filename “into type specifier.” There is something that has changed from 4.3 to 5.2 in the way the save command works. The Dictionary is not too clear about what “specifier” means.

Curiously,

save document 1 in file (mobileFolder & fileName) – save to mobile folder

successfully writes the document to icloud (i.e., if you go to file|open in Pages, the doc shows up there), but the edited document does not report being saved.

If your mobile Pages folder is in Mobile Documents (without any number extension) you can use just


set mobilePagesDocumentsFolder to (path to library folder from user domain as text) & "Mobile Documents:com~apple~Pages:Documents:"

Right, that is actually how I have it defined (you showed me how to do that months ago). But using Pages 5.2 when I issue this directive:

save document 1 in file (mobileFolder & fileName) # where fileName has been defined befroehand

a document is created in the Mobile Folder (i.e. iCloud) but the file being edited does not get any name: it still says “Untitled”

I ran your script as is and got an error: I can send you a screen shot if you tell me the best way to do that.