Batch convert pages to PDF with automator or script?

Discussion in 'Mac Programming' started by TwoBytes, May 11, 2016.

  1. TwoBytes macrumors 68020

    TwoBytes

    Joined:
    Jun 2, 2008
    #1
    Is there a quick way to batch convert pages files into PDFs using Automator? I couldn't see anything obvious... thanks :)
     
  2. superscape macrumors 6502a

    superscape

    Joined:
    Feb 12, 2008
    Location:
    East Riding of Yorkshire, UK
    #2
    I don't think there are any helpful Pages actions, but you could use an AppleScript action to do the export.
     
  3. TwoBytes, May 11, 2016
    Last edited: May 11, 2016

    TwoBytes thread starter macrumors 68020

    TwoBytes

    Joined:
    Jun 2, 2008
    #3
    Thanks for the reply Script is out of my comfort zone.

    I found this droplet

    https://iworkautomation.com/pages/document-export.html

    but i'm getting "incompatible items" errors with the latest pages files. Anyone savvy around to troubleshoot?

    on run

    set theseItems to (choose fileof type {"com.apple.iwork.pages.pages"} with prompt "Choose the Pages documents to export to PDF:" with multiple selections allowed)

    opentheseItems

    end run


    on opentheseItems

    -- TRIGGERED WHEN USER DRAGS ITEMS ONTO THE DROPLET

    set the filesToProcess to {}

    -- filter the dragged-on items for presentation files

    repeat with i from 1 to the count of theseItems

    set thisItem to itemi of theseItems

    if my checkForIdentifier(thisItem, {"com.apple.iwork.pages.pages"}) is true then

    set the end of the filesToProcess to thisItem

    end if

    end repeat

    if filesToProcess is {} then

    activate

    display alert "INCOMPATIBLE ITEMS" message "None of the items were Pages documents."

    else

    -- process the documents

    my exportToPDF(filesToProcess)

    end if

    end open


    on exportToPDF(theseFiles)

    repeat with i from 1 to the count of theseFiles

    set thisFile to itemi of theseFiles

    set thisFilePOSIXPath to the POSIX path of thisFile

    copy my deriveNewFilename(thisFilePOSIXPath, "pdf", "-", "") to {targetName, targetPOSIXpath}

    set targetFileReference to targetPOSIXpath as POSIX file

    tell application "Pages"

    try

    activate

    with timeout of 1200 seconds

    openthisFile

    delay 1

    -- EXPORT THE DOCUMENT

    with timeout of 1200 seconds

    export front documenttotargetFileReferenceasPDF

    end timeout

    close front documentsavingno

    end timeout

    on error errorMessagenumbererrorNumber

    if errorNumber is not -128 then

    display alerterrorNumbermessageerrorMessage

    end if

    error number -128

    end try

    end tell

    end repeat

    end exportToPDF


    on checkForIdentifier(thisItem, theseTypeIdentifiers)

    try

    -- uses Spotlight to check for specified item type

    set the queryResult to ¬

    (do shell script "mdls -raw -name kMDItemContentType " & ¬

    quoted form of the POSIX path of thisItem)

    if the queryResult is in theseTypeIdentifiers then

    return true

    else

    return false

    end if

    on error

    return false

    end try

    end checkForIdentifier


    on deriveNewFilename(sourceItemPOSIXPath, newNameExtension, incrementSeparator, targetFolderPOSIXPath)

    -- A sub-routine used for deriving the name and path of a new file using the name of an existing file

    -- Pass in file ref in POSIX format, the new name extension, an increment separator, and any target directory (in POSIX format)

    -- Name and POSIX path for new file are returned. The name is incremented if a file exists in the target location.

    -- Pass a null string for the target directory to use the item's parent directory

    -- Pass a null string for the new name extension to use the item's current name extension



    -- get item info

    copy my itemInfoFor(sourceItemPOSIXPath) to {parentDirectoryPath, sourceItemName, sourceItemBaseName, sourceItemNameExtension}

    if targetFolderPOSIXPath is "" then

    -- get the path to parent folder of the source item

    set targetFolderPOSIXPath to parentDirectoryPath

    else if targetFolderPOSIXPath contains "~" then

    set targetFolderPOSIXPath to (do shell script "echo " & targetFolderPOSIXPath)

    end if

    if targetFolderPOSIXPath does not end with "/" then set targetFolderPOSIXPath to targetFolderPOSIXPath & "/"



    -- check file extension

    if the sourceItemNameExtension is missing value then

    set the sourceItemNameExtension to ""

    if newNameExtension is "" then

    set extensionSeparator to ""

    else

    set extensionSeparator to "."

    end if

    else

    set extensionSeparator to "."

    end if



    -- generate the target file name

    if the newNameExtension is "" then

    set targetName to sourceItemName

    set targetExtension to sourceItemNameExtension

    else

    set targetExtension to newNameExtension

    set targetName to (the sourceItemBaseName & extensionSeparator & targetExtension) as Unicode text

    end if



    -- check to see if a file named the same as the source file exists in the target folder

    set targetItemPOSIXPath to targetFolderPOSIXPath & targetName

    set the fileExistenceStatus to ¬

    (do shell script "[ -a " & (quoted form of targetItemPOSIXPath) & " ] && echo 'true' || echo 'false'") as boolean

    if fileExistenceStatus is true then

    set the nameIncrement to 1

    repeat

    -- create a new target path with the target item name incremented

    set the newName to ¬

    (the sourceItemBaseName & incrementSeparator & (nameIncrement as Unicode text) & extensionSeparator & targetExtension) as Unicode text

    set targetItemPOSIXPath to targetFolderPOSIXPath & newName

    set the fileExistenceStatus to ¬

    (do shell script "[ -a " & (quoted form of targetItemPOSIXPath) & " ] && echo 'true' || echo 'false'") as boolean

    if fileExistenceStatus is true then

    set the nameIncrement to the nameIncrement + 1

    else

    set the targetPOSIXpath to (targetFolderPOSIXPath & newName)

    return {newName, targetPOSIXpath}

    end if

    end repeat

    else

    set the targetPOSIXpath to (targetFolderPOSIXPath & targetName)

    return {targetName, targetPOSIXpath}

    end if

    end deriveNewFilename


    on itemInfoFor(sourceItemPOSIXPath)

    -- get the parent directory of the item

    set the parentDirectoryPath to (do shell script "dirname " & (the quoted form of sourceItemPOSIXPath))

    --> "/Users/sal/Pictures"

    if parentDirectoryPath does not end with "/" then set parentDirectoryPath to parentDirectoryPath & "/"



    -- get the name of the item

    set the itemFileName to (do shell script "basename " & (quoted form of sourceItemPOSIXPath))

    --> "automator101.png"



    -- get the item name without extension

    set the itemNameWithoutExtension to (do shell script "file=" & (quoted form of itemFileName) & ";echo ${file%.*}")



    -- get the item extension

    if itemFileName contains "." then

    set the itemFileExtension to do shell script "file=" & (quoted form of itemFileName) & ";echo ${file##*.}"

    --> "png"

    else

    set the itemFileExtension to missing value

    end if



    return {parentDirectoryPath, itemFileName, itemNameWithoutExtension, itemFileExtension}

    end itemInfoFor
     
  4. superscape, May 12, 2016
    Last edited: Oct 25, 2016

    superscape macrumors 6502a

    superscape

    Joined:
    Feb 12, 2008
    Location:
    East Riding of Yorkshire, UK
    #4
    Holy overkill, Batman!

    Since I'm in a generous mood, here's some AppleScript to get you going:


    Code:
    set theFile to choose file with prompt "Please choose a Pages file"
    
    tell application "Finder"
        set theFilesFolder to (folder of theFile) as text
    end tell
    
    tell application "Pages"
        set theDoc to open theFile
     
        set theDocName to name of theDoc
     
        --I'm assuming that the file name always ends with ".pages"
        set theName to (characters 1 thru -7 of theDocName) as text
     
        export theDoc as PDF to file ((theFilesFolder & theName & ".pdf") as text)
     
        close theDoc
     
    end tell
    
    
    You should be able to run that from Script Editor. Note that I'm assuming you're just going to pass it Pages files, with a ".pages" file extension.

    If I have time, I'll write up how to add it to an Automator workflow to batch process later, although I've probably given you enough to work that out for yourself.

    Hope that's some help.
     
  5. TwoBytes thread starter macrumors 68020

    TwoBytes

    Joined:
    Jun 2, 2008
    #5
    Thanks! Could the sun in Yorkshire is sunny as London today? :) I also see your a dab hand at programming on your website :)

    I ran it on a pages file called test and I got this :(

    error "Can’t make characters 1 thru -7 of \"test\" into type text." number -1700 fromcharacters 1 thru -7 of "test" totext
     
  6. superscape macrumors 6502a

    superscape

    Joined:
    Feb 12, 2008
    Location:
    East Riding of Yorkshire, UK
    #6
    Yup. It's never dull in Hull.

    Does your file have a ".pages" file extension?
     
  7. TwoBytes thread starter macrumors 68020

    TwoBytes

    Joined:
    Jun 2, 2008
    #7
    Well, it's never more than 6mph in London :)

    Yep, extension is ".pages"
     
  8. superscape, May 12, 2016
    Last edited: May 12, 2016

    superscape macrumors 6502a

    superscape

    Joined:
    Feb 12, 2008
    Location:
    East Riding of Yorkshire, UK
    #8
    Hmmm. That's odd. It's working fine here.

    I've used a slightly shoddy way of getting the file name minus the extension - I'm just lopping off the last 7 characters. There are better ways of doing it - I was just knocking something together quickly as a proof of concept. Still, as long as you have a .pages file extension, I don't see why it wouldn't work.

    If it's not confidential, send me a copy of the file and I'll try it here.
     
  9. TwoBytes thread starter macrumors 68020

    TwoBytes

    Joined:
    Jun 2, 2008
    #9
    Thanks for troubleshooting. I tried brand new pages doc and it worked! Didn't work on my other docs which are CVs. It's strange this...maybe a formatting issue but I would have thought export to PDF would do it. Thanks though, the script works!! :)
     
  10. superscape macrumors 6502a

    superscape

    Joined:
    Feb 12, 2008
    Location:
    East Riding of Yorkshire, UK
    #10
  11. TwoBytes thread starter macrumors 68020

    TwoBytes

    Joined:
    Jun 2, 2008
    #11
    :) aw, I feel like I am the guilty but proud person for this new addition to the net! I'm hopeful it will give you some hits and work. If this is a testament to your work than everybody go go ghostotter.com!
     
  12. superscape, May 24, 2016
    Last edited: May 25, 2016

    superscape macrumors 6502a

    superscape

    Joined:
    Feb 12, 2008
    Location:
    East Riding of Yorkshire, UK
    #12
    Just for the record, I am indeed available for children's parties, bar mitzvahs and bespoke AppleScripting/custom development*. See https://www.ghostotter.com/custom-development/ for details.





    * I may have lied about the first two.
     
  13. MrDreez macrumors newbie

    MrDreez

    Joined:
    Oct 22, 2016
    #13
    I'm a teacher and need to batch convert .pages to PDF for numerous reasons (why can't the world run on OSX?!?!) and this worked brilliantly. Have been searching for this for a loooong time. Works like a charm. Two thumbs up. Cheers mate!
     
  14. superscape macrumors 6502a

    superscape

    Joined:
    Feb 12, 2008
    Location:
    East Riding of Yorkshire, UK
    #14

    No problem. Glad you found it useful.
     
  15. Aouttier macrumors newbie

    Joined:
    Mar 3, 2016
    Location:
    Belgium
    #15
    When I use the script above, I got always the error message number 6: "Pages got an error. The document 'xxx.pages' can not be exported to 'xxx'. You don not have permission. (My mac is a dutch one, so excuse me for the bad translation)

    I even got this error when I select a document from the folder 'Documents'.

    I have written a similar applescript of my own, and it worked. But I think it stopped working when I upgraded to sierra. When I found a solution for the above script, I think/believe that I have found the solution for my wn script.

    Does anyone have encountered the same problem and knop the solution for my problem?
     
  16. superscape macrumors 6502a

    superscape

    Joined:
    Feb 12, 2008
    Location:
    East Riding of Yorkshire, UK
    #16
    Hmm... I can't reproduce the problem here (I'm on El Capitan and Pages 5.6.2) so I can't shed much light on that, I'm afraid. The above script works fine for me. Can you do the export manually?
     
  17. Aouttier macrumors newbie

    Joined:
    Mar 3, 2016
    Location:
    Belgium
    #17
    Sorry for het late answer.

    When I do manually, I have no problem. The problem arises when I did an upgrade to OS Sierra, so I think it's linked with Sierra.

    But, I found an workaround. When I copy first an pdf to the folder with the same name, the script overwrites the pdf without error.
     
  18. Funkmaxtor macrumors newbie

    Funkmaxtor

    Joined:
    Jan 17, 2017
    #18
    I am still getting the same error. I am using Pages 6.0.5 on Sierra. Any help would be appreciated.
     
  19. superscape macrumors 6502a

    superscape

    Joined:
    Feb 12, 2008
    Location:
    East Riding of Yorkshire, UK
    #19
    I'm afraid I'm stuck on El Capitan for the foreseeable future. I'll take a look at this when I have Sierra.
     
  20. superscape, Feb 2, 2017
    Last edited: Feb 3, 2017

    superscape macrumors 6502a

    superscape

    Joined:
    Feb 12, 2008
    Location:
    East Riding of Yorkshire, UK
    #20
    OK. Veeeeery tentatively, I think I may have a solution. It seems to be a permissions related bug (or "undocumented feature"... ahem). I've documented a possible fix here:

    https://www.ghostotter.com/using-automator-convert-pages-pdf/

    Scroll down to the bit about "Update for macOS Sierra" towards the bottom for sample code.

    AppleScript only version here (no Automator):

    https://www.ghostotter.com/macos-sierra-export-pdf-pages-using-applescript/

    Interested to hear if that solves it for any of you!
     
  21. superscape macrumors 6502a

    superscape

    Joined:
    Feb 12, 2008
    Location:
    East Riding of Yorkshire, UK
    #21
    Hi all,

    Has anyone else found that this has mysteriously been fixed? (not sure whether a Page or a macOS update did it). Either way, the original method seems to be working for me on macOS 10.12.5 and Pages 6.1.1

    Anyone else?
     
  22. wave-forms, Jun 11, 2017
    Last edited: Jun 11, 2017

    wave-forms macrumors newbie

    Joined:
    Aug 12, 2014
    #22
    Yup! Just tried both of them on macOS 10.12.5 and Pages 6.1.1, and the old one works, while the new one returns an error.

    Although, it seems to be cutting off some of the name for each document (they are slightly longer than average here). Any way to adjust that?

    EDIT: I see what's happening. My documents were .pages but the file names didn't have the extension.
    Changed the line
    Code:
    set theName to (characters 1 thru -7 of theDocName) as text
    to
    Code:
    set theName to (characters 1 thru -1 of theDocName) as text

    Thanks for the script!
     
  23. superscape macrumors 6502a

    superscape

    Joined:
    Feb 12, 2008
    Location:
    East Riding of Yorkshire, UK
    #23
    Hi,

    It seems the annoying bug we had in Pages re the permissions thing has been solved for a little while now.

    However, I notice that files created by previous versions of Pages need opening and resaving in the latest version before they'll work properly. That can be a bit annoying if you have a lot of files, so here's a handy AppleScript droplet for anyone who finds it useful.

    Usual disclaimer: I'm pretty sure it's safe to use, but use at your own risk...
     

    Attached Files:

Share This Page