could anyone help with a script to convert Pages-Documents to PDF as the following script does with doc/docx files… Thanks a lot!
property extensionList : {"doc", "docx"}
tell application id "DNtp"
set theInput to {}
set theSelection to the selection
repeat with theRecord in theSelection
set theFile to (path of theRecord) as POSIX file
set theDoc to theFile as alias
tell application "Finder"
set theFilePath to container of theDoc as text
set ext to name extension of theDoc
if ext is in extensionList then
set theName to name of theDoc
set theFilename to characters 1 through ((length of theName) - (length of ext) - 1) of theName as string
set theFilename to theFilename & ".pdf"
tell application "Microsoft Word"
set theOldDefaultPath to get default file path file path type documents path
set default file path file path type documents path path theFilePath
open theDoc
set theActiveDoc to the active document
set theFilePath to (path to trash) as string
set default file path file path type documents path path theFilePath
save as theActiveDoc file format format PDF file name theFilename
close theActiveDoc
set default file path file path type documents path path theOldDefaultPath
end tell
set theConvertedPath to (theFilePath & theFilename as string)
end if
end tell
set theConvertedRecord to import POSIX path of theConvertedPath to (parent 1 of theRecord)
#if exists theConvertedRecord then tell application "Finder" to delete theConvertedPath
end repeat
end tell
-- Convert Pages documents to PDF
property extensionList : {"pages"}
tell application id "DNtp"
set theSelection to the selection
set theFolder to path to trash as string
repeat with theRecord in theSelection
set thePath to (path of theRecord) as POSIX file
set theFile to thePath as alias
tell application "Finder"
set ext to name extension of theFile
if ext is in extensionList then
set theName to name of theFile
set theFilename to characters 1 through ((length of theName) - (length of ext) - 1) of theName as string
set theFilename to theFilename & ".pdf"
set theConvertedPath to (theFolder & theFilename as string)
tell application "Pages"
set theDoc to open theFile
export theDoc as PDF to file theConvertedPath
close theDoc
end tell
tell application id "DNtp" to import POSIX path of theConvertedPath to (parent 1 of theRecord)
end if
end tell
end repeat
end tell
Hi - This looks perfect - I have created a new script in DT, but when I select a Word Doc, and then select the script from the Scripts menu, nothing happens.
If I run the script in Script Editor (i.e. outside of DTp), it opens Word, but then returns an error around “Save As…”
That works for me! But you first have to start the script and MS Word once. After that it works!
property extensionList : {"doc", "docx"}
tell application id "DNtp"
set theInput to {}
set theSelection to the selection
repeat with theRecord in theSelection
set theFile to (path of theRecord) as POSIX file
set theDoc to theFile as alias
tell application "Finder"
set theFilePath to container of theDoc as text
set ext to name extension of theDoc
if ext is in extensionList then
set theName to name of theDoc
set theFilename to characters 1 through ((length of theName) - (length of ext) - 1) of theName as string
set theFilename to theFilename & ".pdf"
tell application "Microsoft Word"
set theOldDefaultPath to get default file path file path type documents path
set default file path file path type documents path path theFilePath
open theDoc
set theActiveDoc to the active document
set theFilePath to (path to trash) as string
set default file path file path type documents path path theFilePath
-- save as theActiveDoc file format format document default file name theFilename
save as theActiveDoc file format format PDF file name theFilename
close theActiveDoc
set default file path file path type documents path path theOldDefaultPath
end tell
set theConvertedPath to (theFilePath & theFilename as string)
end if
end tell
set theConvertedRecord to import POSIX path of theConvertedPath to (parent 1 of theRecord)
#if exists theConvertedRecord then tell application "Finder" to delete theConvertedPath
end repeat
end tell
Hello,
I allways used this script und was happy with it.
But now after changing to Catalina the script no longer works.
The pdf is in the trash but not in the group the pages document ist located.
Is somebody able to modify the script for Catalina?
Thanks
The script should be still compatible. Do you execute it via DEVONthink’s Scripts menu? Then please check that DEVONthink 3 is able to automate Pages (see System Preferences > Security > Automation > DEVONthink 3.app).
Here is my take on this, as a teaching edition script…
tell application id "DNtp"
set tempFolder to (path to temporary items from user domain) as string
-- Using the temp folder for temporary exports - a common thing to do.
repeat with thisRecord in (selection as list)
set recName to (name of thisRecord) -- Get the record's name
if recName ends with ".pages" then -- If the name ends with ".pages", process it
set baseName to (characters 1 thru -7 of recName) as string -- Get all the characters before the ".pages" and coerce it back to a string
set exportName to (baseName & ".pdf") as string -- Make a new string with the proper file extension
-- Calling Pages in a separate handler
set convertedDoc to my pagesConvertToPDF(path of thisRecord, tempFolder, exportName)
-- Passing the path, the location of the temp folder for exporting, and the name to be used for the export.
-- After the handler finishes, the script continues here…
import (tempFolder & exportName) to current group -- Import to the current group.
end if
end repeat
end tell
on pagesConvertToPDF(recPath, tempFolder, exportName)
tell application "Pages"
set theDoc to open recPath -- Open the document
with timeout of 1800 seconds -- Give it three minutes to export, as a safety margin
export theDoc to file (tempFolder & exportName) as PDF -- Exporting to the temp folder, with the proper name, and as a PDF
end timeout
close theDoc -- Close the document when finished
end tell
-- Return back to where the script left off
end pagesConvertToPDF
This shows an example of a simple handler with named parameters.
Also, this is not error-trapped outside of checking if a document is a Pages file or not.