Thursday, November 13, 2014

#1 2007-08-13 03:41:45 pm

ruk
Member
Registered: 2007-08-13
Posts: 1

Batch Convert Multiple Pages (iWork) Documents

I have a pile of Pages (iWork) documents that I needed to convert to PDF.  As near as I can tell, Pages doesn't support any direct way to invoke its "Export to PDF" feature, and using the Print route to "Save as PDF" has its own well-documented challenges, so I created this script to automate the process using GUI scripting. 

The strangest line of the script is "set frontmost to true", which has a function I cannot describe to you because I don't know what it is (it was in the AppleScript forum here: http://lists.apple.com/archives/applesc … 00662.html); the script won't work reliably without it, though.

Upon execution the script will prompt you for a folder full of Pages documents; it will then, one by one, open each document, and export it as a PDF file to the default location for saving such a thing.

May not be the best way to do this (I welcome suggestions for alternatives!), but it worked for me.

Applescript:


set F to choose folder
tell application "Finder"
   set P to (files of entire contents of F)
   repeat with I from 1 to number of items in P
       set this_item to item I of P as alias
       if kind of this_item as string is "Pages Publication" then
           tell application "Pages"
               open this_item
               tell application "System Events"
                   tell process "Pages"
                       activate
                       set frontmost to true
                       click menu item "Export…" of menu 1 of menu bar item "File" of menu bar 1
                       click button "PDF" of radio group 1 of sheet 1 of window 1
                       click button "Next..." of sheet 1 of window 1
                       click button "Export" of sheet 1 of window 1
                   end tell
               end tell
               close window 1 without saving
           end tell
       end if
   end repeat
end tell


Filed under: Finder, System, pages

Offline

 

#2 2007-10-06 04:30:02 am

mlewan
Member
Registered: 2007-10-06
Posts: 1

Re: Batch Convert Multiple Pages (iWork) Documents

Did you try

Applescript:


on open theFiles
   tell application "Pages"
       repeat with aFile in theFiles
           open aFile
           set docName to name of front document
           -- Remove .pages extension.
           set prevTIDs to AppleScript's text item delimiters
           set AppleScript's text item delimiters to ".pages"
           -- Add .pdf extension.
           set docName to first text item of docName & ".pdf"
           set AppleScript's text item delimiters to prevTIDs
           -- Save file to Desktop.
           set docPathAndName to (path to desktop as string) & docName
           save front document as ".pdf" in docPathAndName
           close front document
       end repeat
   end tell
end open


Filed under: pages

Offline

 

#3 2009-01-25 02:35:34 pm

ct77
Member
Registered: 2008-11-08
Posts: 5

Re: Batch Convert Multiple Pages (iWork) Documents

A few modifications of the most recent version of the script, required (in my case at least) to make things work.  This was tested with Pages '09.

Open Script Editor, copy the script into it, and save as an application.  Drag and drop files you want to convert onto the application icon.

Saves converted files to Desktop.

Applescript:

on open theFiles
   tell application "Pages"
       repeat with aFile in theFiles
           open aFile
           set docName to name of front document
           -- Remove .pages extension.
           set prevTIDs to AppleScript's text item delimiters
           set AppleScript's text item delimiters to ".pages"
           -- Add .pdf extension.
           set docName to first text item of docName & ".pdf"
           set AppleScript's text item delimiters to prevTIDs
           -- Save file to Desktop.
           set docPathAndName to (path to desktop as string) & docName
           save front document as "SLDocumentTypePDF" in docPathAndName
           close front document
       end repeat
   end tell
end open


Filed under: pages

Offline

 

#4 2009-01-25 02:36:57 pm

ct77
Member
Registered: 2008-11-08
Posts: 5

Re: Batch Convert Multiple Pages (iWork) Documents

Slightly modified version of the script I just posted.  This version saves the new file -- the PDF -- in the same folder as the original document:

Applescript:

on open theFiles
   tell application "Pages"
       repeat with aFile in theFiles
           open aFile
           set docName to name of front document
           -- Remove .pages extension.
           set prevTIDs to AppleScript's text item delimiters
           set AppleScript's text item delimiters to ".pages"
           -- Add .pdf extension.
           set docName to first text item of docName & ".pdf"
           set AppleScript's text item delimiters to prevTIDs
           -- Get folder that dropped file exists in.
           tell application "Finder"
               set sourceFolder to (container of aFile) as Unicode text
           end tell -- Finder
           -- Save file to folder that dropped file exists in.
           set docPathAndName to sourceFolder & docName
           save front document as "SLDocumentTypePDF" in docPathAndName
           close front document
       end repeat
   end tell
end open


Filed under: Finder, pages

Offline

 

#5 2009-02-18 10:52:12 pm

dougransom
Member
Registered: 2009-02-18
Posts: 2

Re: Batch Convert Multiple Pages (iWork) Documents

The problem with all these scripts, except maybe the first one which I haven't tried, is that pages exports images using the lowest quality. Because I have images in my docs, including a scanned signature, the PDF looks horrible. 

If I manually export, the high-quality pdf export is the default works, but I cant see how to automate the export of pdf using pages high quality options.

Offline

 

#6 2009-02-21 12:44:28 pm

dougransom
Member
Registered: 2009-02-18
Posts: 2

Re: Batch Convert Multiple Pages (iWork) Documents

Installing cups-pdf http://www.codepoetry.net/projects/cups-pdf-for-mosx, creating a virtual printer named "Virtual Printer" and
peplacing the pdf export of the file with this will yield a high-quality pdf document.
    set printOpts to {target printer:"Virtual Printer"}
    tell application "Pages"
        repeat with aFile in theFiles
            open aFile
            print front document with properties printOpts
            close front document saving no
        end repeat
    end tell
    --end open


Filed under: PDF, Mac, cups, virtual, pages

Offline

 

#7 2009-09-09 10:45:18 am

happy_wooter
Member
Registered: 2008-07-20
Posts: 4

Re: Batch Convert Multiple Pages (iWork) Documents

Sorry for performing a bit of thread necromancy here, but it seemed relevant to  this discussion enough to post here.

   Any tips on getting this to work with Numbers and Keynote files? I already found out that if you change .pdf out for .doc and swap SLDocumentTypePDF for SLDocumentTypeMSWord it works fine to convert Pages documents to Word documents.

    The code listed by ct77 is working for Pages to .doc and .pdf. If I swap Pages for Keynote and change other things as appropriate it gets to the line which starts "save front document" and gives me an error stating it cannot save the document in that format, I've tried a number of options for the string there with no success.

    Trying the code to work on Numbers met with even less success than with Keynote, throwing an error message at the line beginning "set docName to" stating that it "couldn't find document 1".

   Any help you guys can provide is greatly appreciated.

Last edited by happy_wooter (2009-09-09 10:46:28 am)


Filed under: PDF, conversion, office, Iwork

Offline

 

#8 2010-01-07 07:40:01 am

Haze
Member
From: Denmark
Registered: 2009-12-16
Posts: 13

Re: Batch Convert Multiple Pages (iWork) Documents

Hey

I have made this for Exporting Numbers '09 documents to Excel format, using GUI and the Export function.

If you want PDF's instead of Excel, then change the two lines in the clickExcelButton handler, from button 2 to 1.

-- click radio button 2 of radio group 1 of sheet 1 of window theWindow
and
-- click checkbox 2 of radio group 1 of sheet 1 of window theWindow

This script should work in both Mac OS X 10.5 and Mac OS X 10.6.

Applescript:

tell application "Numbers"
   set theWindow to name of document 1
end tell

tell application "System Events"
   tell process "Numbers"
       activate
       set frontmost to true
       click menu item 11 of menu 1 of menu bar item 3 of menu bar 1
       my clickExcelButton(theWindow)
       click button 1 of sheet 1 of window theWindow
       click button 1 of sheet 1 of window theWindow
   end tell
end tell

on clickExcelButton(theWindow)
   tell (system info) to set currentOSVersion to get system version
   if currentOSVersion begins with "10.6" then
       tell application "System Events"
           tell process "Numbers"
               click radio button 2 of radio group 1 of sheet 1 of window theWindow
           end tell
       end tell
   else
       tell application "System Events"
           tell process "Numbers"
               click checkbox 2 of radio group 1 of sheet 1 of window theWindow
           end tell
       end tell
   end if
end clickExcelButton

I have also an "extension" of this script, that can close the document, when done.
Just add this to the buttom of the script, it will check the CPU usage of Numbers, and when usage drops below theLimit it can do something, in this close the document without saving.

Applescript:


set theLimit to 10
repeat
   tell application "System Events"
       set PID to unix id of process "Numbers"
       set theCPU to do shell script "ps aux | grep " & PID & " | grep -v grep | awk '{print $3}'"
   end tell
   if theCPU < theLimit then
       tell application "Numbers"
           close the document theWindow without saving
       end tell
       exit repeat
   end if
end repeat

Last edited by Haze (2010-01-07 07:41:52 am)


Filed under: PDF, Export, excel, numbers

Offline

 

Board footer

Powered by FluxBB

[ Generated in 0.043 seconds, 8 queries executed ]

RSS (new topics) RSS (active topics)