Scripting for Pages 5.2

I did test, and got an error. I can send you a screen shot if you tell me a good way to get it to you.

Hello

I removed every old versions of my script.

It was difficult to get a correct version because rkimball’s machine doesn’t behave like mine.

Here, when an application create a new document, this one is named “Sans titre” (would be “Untitled” in English with no extension name.
If I ask the app to return the document’s properties, it retrun : file: missing value.

On rkimball machine the document is created with the name extension and its properties contain :
file: file:file Macintosh HD:Users:¢¢¢¢¢¢¢:Library:Mobile Documents:com~apple~Pages:Documents:Untitled.pages

I was unable to find what may be causing this special behavior so, I decided to use a power-hammer to solve the problem.

The final script is :

Open this Scriplet in your Editor:

# = = = = = = = = = = = = = = = = = = = = = =
# Build the path to the destination folder
# If there is a folder "~/Library/Mobile Documents", use it
# If there is no such folder, use "~/Library/Mobile Documents.123456789"

set mobileFolder to path to library folder from user domain
tell application "System Events"
	if exists folder ((mobileFolder as text) & "Mobile Documents") then
		"Mobile Documents"
	else
		name of folders of result whose name starts with "Mobile Documents."
	end if
end tell
set mobileFolder to (mobileFolder as text) & result & ":com~apple~Pages:Documents:"

# Define the file name

set fileName to "Doe, John 3 April 2014.pages"

# = = = = = = = = = = = = = = = = = = = = = =
# Replace what you did with Contacts

set salutName to "Lucky Luke"
set companyName to "Fake and Co"
set personName to "Poor Lonesome Cowboy"
set addressStreet to "Infinite loophole"
set addressCity to "Cupertilow"
set AppleScript's text item delimiters to {""}
set addressCountry to (reverse of characters of "United States of America") as text

# = = = = = = = = = = = = = = = = = = = = = =
# the prefix « its » is required to grab a property (here, month) displayed like a class
tell (current date) to day & space & (its month as string) & space & year
set letterDate to result as text

# = = = = = = = = = = = = = = = = = = = = = =

if (do shell script "defaults read 'Apple Global Domain' AppleLocale") starts with "fr_" then
	set buttonCustom to "Personnalisé"
	set buttonApp to "Standard"
	set buttonAll to "Tous"
	set thePrompt to "Créer un document Pages 5.2 depuis un modèle."
else
	set buttonCustom to "Custom"
	set buttonApp to "Standard"
	set buttonAll to "All"
	set thePrompt to "Make a Pages 5.2 document from a template."
end if

# We aren't in a tell application block so there is no need to prefix with " tell me to "

display dialog thePrompt buttons {buttonCustom, buttonApp, buttonAll} default button buttonAll
set doWhat to button returned of result

tell application "/Applications/Pages.app/" # "/Volumes/Samsung 32/Applications/Pages.app/"
	if doWhat is buttonCustom then
		set appTemplates to name of every template whose id starts with "User/"
	else if doWhat is buttonApp then
		set appTemplates to name of every template whose id starts with "Application/"
	else
		set appTemplates to name of every template
	end if
	
	# Here we are in a tell application block so we must prefix with " tell me to "
	
	tell me to set theTemplate to choose from list appTemplates
	if theTemplate is false then error number -128
	set currentDocument to make new document with properties {document template:template (item 1 of theTemplate)}
	tell currentDocument
		# log (get its properties) 
		tell body text
			set parCount to 1
			set dateParagraph to parCount
			set paragraph dateParagraph to letterDate
			set salutParagraph to dateParagraph + 1
			set paragraph salutParagraph to "Dear " & salutName & ":"
			set nameParagraph to salutParagraph + 5
			
			if companyName is not missing value then
				set addressString to personName & linefeed & companyName & linefeed & addressStreet & linefeed & addressCity
			else
				set addressString to personName & linefeed & addressStreet & linefeed & addressCity
			end if
			if addressCountry is not in {missing value, "United States of America"} then
				set addressString to addressString & linefeed & addressCountry
			end if
			set paragraph nameParagraph to addressString
			set nbParagraphs to count paragraphs # used below
		end tell # body text
	end tell # Document
end tell # Pages

# It's awful but it works.
delay 0.1
activate application "/Applications/Pages.app/"
tell application "System Events" to tell (first process whose frontmost is true)
	# awful trick to select something so that other command do their job
	keystroke "a" using {command down}
	delay 0.1
	# move the cursor to the very end of the document
	key code 125 using {command down}
	delay 0.1
	# = = = = = = = = = = = = = = = = = = = = ==============================
	# move up to the beg of the wanted paragraph. CHECK THAT I REACH THE CORRECT PARAGRAPH
	# = = = = = = = = = = = = = = = = = = = = ==============================
	repeat (nbParagraphs - 3) times
		key code 126
	end repeat
end tell

# Urge the application to save the document

set thePath to mobileFolder & fileName
tell application "Pages"
	# log "here, point 1"
	# log (get document 1's properties) 
	save currentDocument in file thePath
	# log "here, point 2" 
	# The power-hammer at work
	# get rid of the created/filled document
	close document 1 without saving
	# log "here, point 3" 
	# open the newly saved one which wear the defined name
	open file thePath
	# log "here, point 4" 
end tell

# = = = = = = = = = = = = = = = = = = = = =

I commented out several log instructions which were useful to trace the surprising behavior.

Of course, if somebody has an explanation for this behavior, please, post it here.

I borrowed some instructions from :
¨http://macosxautomation.com/applescript/iwork/pages/¨For info,
there is also a web site dedicated to Numbers :
¨http://macosxautomation.com/applescript/iwork/numbers/
and an other one dedicated to Keynote :¨
http://macosxautomation.com/applescript/iwork/keynote/¨¨

Yvan KOENIG (VALLAURIS, France) samedi 5 avril 2014 19:24:07