Hmmm. When I try that, I get the syntax error:
Expected “,” or “}” but found “:”
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.
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