applescript to bulk convert Pages files to PDF

 Not my script, but it works:

paste this into Script Editor, and choose save as app:

use scripting additions


property valid_kind : {"Pages Document"}

property imageBest : false

property fileCnt : (0 as integer)

-- property dx_icon : "/System/Library/CoreServices/CoreTypes.bundle/Contents/Resources/MultipleItemsIcon.icns"


on open dropped_items

tell application "Pages" to set itsversion to its version

considering numeric strings

if itsversion > "5.6" then set imageBest to true

end considering

repeat with anItem in dropped_items

try

tell application "Finder"

if kind of anItem is equal to "Folder" then

set docList to (every item in entire contents of folder anItem ¬

whose kind is in valid_kind) as alias list

repeat with afile in docList

my export_file(afile)

end repeat

else if kind of anItem is in valid_kind then

my export_file(anItem)

else

log "ignored" -- think of this as a continue statement

end if

end tell

on error errmsg number errnbr

my error_handler(errnbr, errmsg)

quit

end try

end repeat

if fileCnt > 0 then

-- display dialog "Export Complete." & return & tab & " Files exported to PDF " & " :  " & (fileCnt as text) ¬

-- buttons {"Done"} default button "Done" with title "Pages Export Facility" -- with icon POSIX file dx_icon as alias

else

display dialog "Export completed without any files processed." & return & "Only Pages (.pages) documents are exported to PDF." buttons {"Done"} default button "Done" with title "Pages Export Facility" -- with icon POSIX file dx_icon

end if

quit

end open


on export_file(afile)

-- switch pages extension to pdf

set thisFile to afile as alias as text

set exportPDF to (text 1 thru ((offset of ".pages" in thisFile) - 1) of thisFile as text) & ".pdf"

close access (open for access exportPDF)

tell application "Pages"

try

set myDoc to open afile

with timeout of 1800 seconds

if imageBest then

export myDoc to file exportPDF as PDF with properties {image quality:Best}

else

export myDoc to file exportPDF as PDF

end if

end timeout

set fileCnt to (fileCnt + 1) as integer

close myDoc saving no

on error errmsg number errnbr

my error_handler(errnbr, errmsg)

quit

end try

end tell

-- an interactive Export To PDF in Pages makes the PDF extension visible

-- but this script needs to be coerced to do that.

tell application "Finder"

if exists (item exportPDF as alias) then

set extension hidden of (item exportPDF as alias) to false

end if

end tell

return

end export_file


on error_handler(nbr, msg)

return display alert "[ " & nbr & " ] " & msg as critical giving up after 10

end error_handler


on quit {}

-- perform these cleanup items, and then really quit

tell application "Pages" to if it is running then quit

set fileCnt to (0 as integer)

continue quit

end quit

Popular posts from this blog

The most useful defaults-write options

Erasing firmware password

Migrating from Windows to Mac