Want to highlight a helpful answer? Upvote!

Did someone help you, or did an answer or User Tip resolve your issue? Upvote by selecting the upvote arrow. Your feedback helps others! Learn more about when to upvote >

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

How to batch convert .pages to .pdf with automator & applescript

Hi all,


As the title states, I want to drag and drop one (or a whole stack) of .pages documents onto an automator app, and have it pump out .pdfs...


Here is my code, it crashes with an error, but I'm really new to this so I don't really know what's going wrong.



on run {input, parameters}

tell application "Finder"

repeat with aFile in input

if class of (itemaFile) is not folder then

set {tName, fileExt} to {name, name extension} of aFile

if fileExt is not missing value and fileExt is not "" then

set tName to "PDFs/" & (text 1 thru -((countfileExt) + 2) of tName) & ".pdf"

tell (get container of aFile)

if not (existsfolder "PDFs") then makenewfolderat it with properties {name:"PDFs"}

tell application "Pages"

set mydoc to openfileaFile-- open input file in Pages


exportmydoctofiletNameasPDF--do the exporting


closemydocsavingno-- close the original file without saving

end tell


end tell

end if

end if

end repeat

end tell

end run

Posted on Mar 31, 2015 1:33 AM

Reply
Question marked as Best reply

Posted on Jul 17, 2017 7:24 AM

Hi,


Apologies for resurrecting an old thread, but in case anyone else stumbles by here I thought I'd mention that you can do this with some third party Pages actions for Automator. Take a look here:


https://www.ghostotter.com/pages-automator-actions/

20 replies

Apr 2, 2015 2:09 PM in response to mirror318

You can create a droplet using the script in Batch convert Pages 5 files to PDF.


Save this in Script Editor (aka Applescript Editor) as an application, then you can drop the pages files to convert to PDF:


on open fs
  my proceed:fs
end open

on proceed:fs
  tell application "Finder"
  repeat with i from 1 to (get count of items in fs)
  if (name extension of item i of fs) is "pages" then
  set PDFfile to text 1 thru -7 of (item i of fs as text) & ".pdf"
  tell application "Pages"
  set mydoc to open (item i of fs) -- open input file in Pages
  export mydoc to file PDFfile as PDF --do the exporting
  close mydoc saving no -- close the original file without saving
  end tell
  end if
  end repeat
  end tell
end proceed:

Apr 2, 2015 1:18 PM in response to Tony T1

Thanks Tony, this script looks great,


However, when I try running it, the app gets up to this line:

set mydoc to open (item i of fs) -- open input file in Pages


Where it hangs for a while, then comes up with:

The action “Run AppleScript” encountered an error.

Check the actionʼs properties and try running the workflow again.


I checked the paths of the input and output file, they both look fine... What's going on?

Apr 2, 2015 1:26 PM in response to mirror318

mirror318 wrote:


Thanks Tony, this script looks great,


However, when I try running it, the app gets up to this line:

set mydoc to open (item i of fs) -- open input file in Pages


Where it hangs for a while, then comes up with:

The action “Run AppleScript” encountered an error.

Check the actionʼs properties and try running the workflow again.


I checked the paths of the input and output file, they both look fine... What's going on?


Not sure. It worked when I tested on 2 test documents created with Pages 5.5.2 on Yosemite 10.10.2

Apr 2, 2015 2:14 PM in response to mirror318

mirror318 wrote:


The action “Run AppleScript” encountered an error.

Check the actionʼs properties and try running the workflow again.


Those errors are from Automator.

No need to run the script though Automator

Open Applications/Utilities/Script Editor

Then copy the script I posted, and when you Save, choose File Format: Application (at the bottom of the Save dialog box)


The PDF is saved in the same Folder as the Pages Doc. To change to another Folder, just change the variable PDFfile

Apr 2, 2015 2:51 PM in response to mirror318

mirror318 wrote:


One more thing, is it easy to change that script so that the output is put in a subfolder called "PDFs" (the folder would go in wherever the .pages file was)?


This should save to a subfolder called "PDFs" in the Folder where the pages file is located:


on open fs
  my proceed:fs
end open

on proceed:fs
  tell application "Finder"
  repeat with i from 1 to (get count of items in fs)
  if (name extension of item i of fs) is "pages" then
  --set PDFfile to text 1 thru -7 of (item i of fs as text) & ".pdf"
  set PDFfile to name of (item i of fs)
  set PDFfile to text 1 thru -7 of PDFfile & ".pdf"
  if not (exists (parent of (item i of fs) as text) & "PDFs") then
  make new folder at (parent of (item i of fs)) with properties {name:"PDFs"}
  end if
  set PDFfile to (parent of (item i of fs) as text) & "PDFs:" & PDFfile as text
  tell application "Pages"
  set mydoc to open (item i of fs) -- open input file in Pages  
  export mydoc to file PDFfile as PDF --do the exporting  
  close mydoc saving no -- close the original file without saving  
  end tell
  end if
  end repeat
  end tell
end proceed:

Jul 1, 2015 6:32 AM in response to mirror318

In Pages 5.5.3 on 10.10.3 (Yosemite), this solution no longer works (on my machine, of course)


tell app 'Pages" to set mydoc to open oneFile


opens the file, but mydoc contains no reference to the file.


tell app "Pages" to set doclist to documents


is an empty list


tell app "Pages"

documents

end


is an empty list.


tell app "Pages" to set x to document 1


produces


Can’t get document 1. Invalid index." number -1719 from document 1


Neat, eh? document 1 is an invalid index for document 1


tell app "Pages" to set x to first document of documents


produces


error "Pages got an error: Can’t get document 1 of every document." number -1728 from document 1 of every document


etc.

Jul 1, 2015 2:04 PM in response to FurrriousG

Pages v5.5.3 on OS X 10.10.4.


This script works fine for dropped files (not folders) on its saved Application.


-- When saved as an Application from Script Editor on Yosemite, you can drop single or multiple

-- files on the application and they will be converted to PDF in the original Pages document

-- location. This version is not written to handle dropped folders.


-- VikingOSX, July 2015, Apple Support Communities


on openfileObjects


repeat with i from 1 to the (count of items of fileObjects)

set this_item to (item i of fileObjects) as alias

tell application "Finder" to set ftype to kind of this_item

if ftype contains "Pages Publication" then

set exportDocument to text 1 thru -7 of (this_item as text) & "pdf"

tell application "Pages"

set mydoc to openthis_item

with timeout of 1200 seconds

exportmydoctofileexportDocumentasPDF

end timeout

closemydocsavingno

end tell

end if

tell application "Finder"

set extension hidden of (file exportDocument as alias) to false

end tell

end repeat


end open

How to batch convert .pages to .pdf with automator & applescript

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