Submit Hint Search The Forums LinksStatsPollsHeadlinesRSS
14,000 hints and counting!

Automate print to PDF using AppleScript and Pages System
There are times that it would be nice to automate the creation of PDF files. For example, one might want a workflow to open a file, change some paragraphs, and print to PDF. This would seem a simple thing, but I haven't found a way to script the Print to PDF functionality. However, the Pages app in the iWork suite will do it for you.

As an example, the following AppleScript will use {ages to print to PDF: Some interesting things to note about the above code. First, the file test3.pdf will be created as a PDF. It seems Pages figures out what you want, and you get a PDF. test2.rtf and test4.txt will be created as RTF and pure text files, which is also useful. However, the file test1.rtf never gets created -- instead, you get a Pages document named test1.rtf.pages. It seems Pages takes the first save as saving the document, then runs the subsequent saves in the proper format. Even if you use the as "rtf" construct in the AppleScript, Pages still saves the file as a Pages document. The workaround here is to have a template document which you open, then do the save into another file:
tell application "Pages"
    open file "test6.pages"
    set product to front document
    set first paragraph of product to "Fee, fii, foo, fum!"
    save product in "test6.pdf"
end tell
Of course, all of the above examples save the files at the root directory level -- full AppleScript paths would be needed to put the files where you want. This method is a valuable way to automate the creation of PDF files with the combination of Pages and AppleScript.
    •    
  • Currently 2.20 / 5
  You rated: 4 / 5 (10 votes cast)
 
[23,645 views]  

Automate print to PDF using AppleScript and Pages | 8 comments | Create New Account
Click here to return to the 'Automate print to PDF using AppleScript and Pages' hint
The following comments are owned by whoever posted them. This site is not responsible for what they say.
Automate print to PDF using AppleScript and Pages
Authored by: asmeurer on Oct 16, '08 08:09:50AM

Can't Automator do this?



[ Reply to This | # ]
Automate print to PDF using AppleScript and Pages
Authored by: zadig on Oct 16, '08 08:13:27AM

I'm fairly sure that OpenOffice can do this as well. At least on other platforms and the X11 version, OpenOffice.org can script PDF production from the command line.



[ Reply to This | # ]
Automate print to PDF using AppleScript and Pages
Authored by: rammjet on Oct 16, '08 09:47:12AM
Automate print to PDF using AppleScript and Pages
Authored by: Dr. T on Oct 16, '08 09:55:55AM

kd4ttc, are you a Doors fan?



[ Reply to This | # ]
Automate print to PDF using AppleScript and Pages
Authored by: kd4ttc on Oct 20, '08 08:44:30PM

;-)
Yeah, along came Mr. Goodtrips
Looking for a new a ship


---
Steve Holland



[ Reply to This | # ]
Now it acts a little differently.
Authored by: kd4ttc on Oct 20, '08 09:09:48PM

Applescript is weird. I tested the hint I submitted several times. Now when I run it I find that test1.rtf is created as an rtf file, contrary to what happended before. Like I said, sometimes it is just weird.

I really appreciate all the comments about this hint. I was looking at the links about the CUPS printer. I couldn't figure out how to set that up. I appreciate that this hint got the other hint linked in. Unfortunately, it looks like the cups printer solution leads to big files. Are those pdfs image files? In my application I want files that can be text searched.




---
Steve Holland



[ Reply to This | # ]
Automate print to PDF using AppleScript and Pages
Authored by: musselrock on Oct 16, '08 06:36:16PM

I'm completely new to scripting Pages, but the following AppleScript will create a .pdf or .rtf file with text in the body on the first try without needing an existing template.

tell application "Pages"
set filname to (path to desktop folder as text) & "test.pdf"
set x to make new document with properties {path:filname}
set first paragraph of x to "test some text to test"
save x in file filname
end tell



[ Reply to This | # ]
CUPS-PDF
Authored by: victory on Oct 17, '08 12:19:31PM
Nice hint.

[This is just a pointer to a related tip for anyone looking to accomplish something similar -- I'm not suggesting it as a replacement for the original hint.]

If you have to create a lot of PDFs this way and don't want to manually have to select 'Print to PDF' from the dropdown-button-thing in the Print dialog each time, consider installing a a virtual printer driver.

After this driver installed, you should see a new printer called 'CUPS-PDF' available in the print dialog. Once you select the 'CUPS-PDF' driver, any subsequent print jobs will be saved as PDF files in a predefined folder in your home directory. Now just press Cmd-P then Enter to quickly save to PDF from just about any app.

[ Reply to This | # ]