Looks like no one’s replied in a while. To start the conversation again, simply ask a new question.

applescript

Hi all,


I'm new to this game and if my question is not at the right place I hope you'll excuse me.


The question is about "Pages" and "Applescript"


I have about 500 "Pages" documents in a folder and I need to pass them to .pdf format in order for them to be accessible to everybody.

So I wrote a script that does just that: Finder opens the document and Pages saves it in .pdf. The problem is that Pages doesn't manage to close the original document and give an error message. I've tried "close" alone and "close document", both followed by the name of the document or not, with the same result.


Here is a simplified version of the script.


set chemin_in to "Actes"

tell application "Finder"

count folder chemin_in

set nombre_actes to the result

set boucle to 0

repeat nombre_actes times

set boucle to boucle + 1

set nom_acte to the name of file (boucle) of folder chemin_in

open (document file nom_acte in folder chemin_in)

set chemin_out to "Actes pdf:"

--display alert chemin_out message boucle giving up after 1

tell application "Pages"

save front document in chemin_out & nom_acte as "SLDocumentTypePDF"

activate document nom_acte

close document nom_acte (red color my choice to pinpoint what doesn't work)

end tell

--close Finder window 1

end repeat

end tell

Pages word processor-OTHER, Mac OS X (10.5.8)

Posted on Nov 30, 2012 2:04 AM

Reply
8 replies

Nov 30, 2012 4:26 AM in response to albar64

I am not sure why that line doesn't work; possibly it concerns the fact that two lines previously you created a second (pdf) document with the same name. While AppleScript error messages are often pretty cryptic, it probably would have been a good idea to include the wording of that message.


I think I would change your Pages tell block to something like:


tell application "Pages"

set targetDocument to front document

save targetDocument in chemin_out & nom_acte as "SLDocumentTypePDF"

close targetDocument

end tell



A couple years ago, MacOSXHints had a hint for doing something similar to what you are trying, with two nice scripts that might be useful to you.

Nov 30, 2012 5:41 AM in response to Jeff Shenk

Hi Jeff


Thanks for your answer.

To be clear on the subject I enclose 3 screens of what it looks like. (For the exemple in my first message I had reduce the path to end folders "Actes" and "Actes pdf" however the path is more complex)


If I disable the "close" statement, the script works perfectly, saves the documents in .pdf format in the proper folder but leaves all the .pages ones open, which clogs my desk. (in screen "actes 2" you can see what it looks like when the "close" command is disabled)


If I set the loop to 1 (instead of the 500 as needed), the script saves and closes that only document.


If I set the loop to 2 or any higher number, the script will work as above for the 1st document but stops at the second one (after having saved it in .pdf) with the enclosed message. Actually, it's always as from the second loop that it starts failing, irrespective of what number I start the loop at.


screen "actes 1" shows the folder with the ".pages" documents

screen "actes 2" shows the folder with the ".pdf" documents

screen "applescript error" shows the error message displayed.


If I display the name of the document to be closed it appears that it's the right one, that is: the one that is open and mentioned in the error message.


Regarding the scripts which are part of OS10 script folder, I could'nt find any suitable to do the above.


Strange, isn't it ?

I'll try your code to see if works.


The error message translates as:

error in Pages: It's impossible to obtain document " name_of_doc"



Again thanks for your help.


Alain


User uploaded file



User uploaded fileUser uploaded file

Nov 30, 2012 7:44 AM in response to albar64

Alain,


I just made a test folder with five Pages documents in it, and ran a script with what was basically your original code, and it worked correctly. Strange, indeed.

Have you tried "close front document" instead of referring to it by name?


This is what I used, if you can see any differences that might be relevent:


set theFolder to "OWC Mercury Elite AL Pro:Users:jeffshenk:Desktop:new_test:"

set sourceFolder to "OWC Mercury Elite AL Pro:Users:jeffshenk:Desktop:new_test:Pages_docs"

set destFolder to "OWC Mercury Elite AL Pro:Users:jeffshenk:Desktop:new_test:pdf_docs:"


tell application "Finder"

set theCount to (countfoldersourceFolder)

repeat with i from 1 to theCount

set nom_acte to the name of file i of folder sourceFolder


open (document filenom_acte in foldersourceFolder)


tell application "Pages"

save front document in destFolder & nom_acte as "SLDocumentTypePDF"


activatedocumentnom_acte


closedocumentnom_acte

end tell

end repeat

end tell

Dec 1, 2012 1:03 AM in response to Jeff Shenk

Hi Jeff,


Got it to work. I had to do 2 things:


1st: move the destination folder path at the top of the script like you did in your exemple (although this should not have made any difference)


2nd: use

closefront document


instead of


closedocumentnom_acte (which always result in the error message)


Why ? I suppose it will remain a mistery, maybe a matter of Applescript version mine is 2.2.1 (100.1)


Here is the script, trimmed down to the bare essentials.




set chemin_in to "Travail:Alain Françoise:Alain:Généalogie:Actes:Décès:"

set chemin_out to "Travail:Alain Françoise:Alain:Généalogie:Actes:Décès pdf:"


tell application "Finder"

set nombre_actes to count folder chemin_in

set boucle to 0

repeat nombre_actes times

set boucle to boucle + 1

set nom_acte to the name of file boucle of folder chemin_in

open document file nom_acte in folder chemin_in

tell application "Pages"

save front document in chemin_out & nom_acte as "SLDocumentTypePDF"

close front document saving no

end tell

end repeat

end tell



Thanks again for help, and this script can also be useful to someone else.


Alain

Dec 1, 2012 5:37 AM in response to albar64

Alain,


I'm glad you got it to work.


I'm using AppleScript Editor version 2.3 (118) with AppleScript 2.1.2, on OS X 10.6.8, so there might be a difference in something there.


From the error message you have been getting I wonder if the Finder's name for a file (which you use to set nom_acte) could differ in some detail from the Pages name for the document opened from that file. My tests with a different system and different names have not shown any difference, though.


Jeff

Dec 2, 2014 8:43 PM in response to Jeff Shenk

HI
I am looking for a help regarding issue. I want a script that can read rtf
file which just contains one number(like 22) and then update to +1(so here
23) but not able to write as it throws an error.
*error "Can’t make \"22 into type number."*
here is the code
*set* theFile *to* (*open for access* *file*
"Mac:Users:ansi:Desktop:dkdm.rtf" *with* write permission) *as* «*class*
utf8»
*set* fileContents *to* (*read* theFile) *as* *integer*
*set* b *to* fileContents + "1"
write b to thFile
*display dialog* b
*close access* theFile

please help me. I just want script to read file which have one number to
read it increment by 1 and save it.



Dec 3, 2014 6:54 AM in response to Jeff Shenk

For Pages v5, Apple changed the AppleScript Dictionary (again), and one can no longer save to SLDocumentTypePDF. The syntax is now:


with timeout of 1200 seconds

export front documenttofiletargetFileHFSPathasPDF

end timeout

where targetFileHFSPath is just ~/Desktop/foo.pdf after it was derived from ~/Desktop/foo.pages.

There is no ability to control the quality of the PDF export, though I do have a GUI scripted solution that can do that in Pages v5.

applescript

Welcome to Apple Support Community
A forum where Apple customers help each other with their products. Get started with your Apple ID.