Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.

TwoBytes

macrumors 68040
Original poster
Jun 2, 2008
3,206
2,179
Is there a quick way to batch convert pages files into PDFs using Automator? I couldn't see anything obvious... thanks :)
 

TwoBytes

macrumors 68040
Original poster
Jun 2, 2008
3,206
2,179
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
 
Last edited:

superscape

macrumors 6502a
Feb 12, 2008
937
223
East Riding of Yorkshire, UK
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.
 
Last edited:
  • Like
Reactions: JuBe

TwoBytes

macrumors 68040
Original poster
Jun 2, 2008
3,206
2,179
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
 

TwoBytes

macrumors 68040
Original poster
Jun 2, 2008
3,206
2,179
Well, it's never more than 6mph in London :)

Yep, extension is ".pages"
 

superscape

macrumors 6502a
Feb 12, 2008
937
223
East Riding of Yorkshire, UK
Yep, extension is ".pages"

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.
 
Last edited:

TwoBytes

macrumors 68040
Original poster
Jun 2, 2008
3,206
2,179
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!! :)
 

TwoBytes

macrumors 68040
Original poster
Jun 2, 2008
3,206
2,179
:) 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!
 

Aouttier

macrumors member
Mar 3, 2016
42
0
Belgium
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

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?
 

superscape

macrumors 6502a
Feb 12, 2008
937
223
East Riding of Yorkshire, UK
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)

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?
 

Aouttier

macrumors member
Mar 3, 2016
42
0
Belgium
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.
 

Funkmaxtor

macrumors newbie
Jan 17, 2017
1
0
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.

I am still getting the same error. I am using Pages 6.0.5 on Sierra. Any help would be appreciated.
 

superscape

macrumors 6502a
Feb 12, 2008
937
223
East Riding of Yorkshire, UK
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!
 
Last edited:

superscape

macrumors 6502a
Feb 12, 2008
937
223
East Riding of Yorkshire, UK
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?
 

wave-forms

macrumors newbie
Aug 12, 2014
16
10
Los Angeles, CA
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!
 
Last edited:
  • Like
Reactions: superscape

superscape

macrumors 6502a
Feb 12, 2008
937
223
East Riding of Yorkshire, UK
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...
 

Attachments

  • Pages Conversion Droplet.app.zip
    56.3 KB · Views: 784

superscape

macrumors 6502a
Feb 12, 2008
937
223
East Riding of Yorkshire, UK
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/

I've removed the Sierra specific fix from the above URL. If you still have the permissions issue described above, update to the latest Pages version (and ideally macOS) and you'll be fine. Apple fixed it. "Woo!", and also, "Yay!"
 

edi

macrumors newbie
May 29, 2002
6
0
Nuremberg, Germany
Cheers everyone,
You could also use my app Docxtor. It lets you convert whole folders of documents and will even reproduce complete folder structures, something which is difficult to achieve in AppleScript. (Disclaimer: I am the developer of this app!)
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.