I'm still very much at the bottom of the learning curve for Automotor and AppleScript so I apologise for lack of basic understanding that has inevitably led to this question.
I'm running MacOSX 10.15.6 (Catalina) on a MacBook Air. My ultimate aim is to take a folder of .pages files (or other compatible file types) and batch convert to .pdf (or other) by opening with Pages and then exporting to the same folder as new file type. I have set up an Automator script that contains: i. Get Specified Finder Items - defines folder containing the files ii. Get Folder Contents - lists all documents in folder iii. AppleScript to open each document and export as PDF
Even before getting to the 'export' bit (I've commented out the export command in the bit below), the AppleScript is throwing an error when I try to get the path to the directory containing the file. The AppleScript looks like:
on run {input, parameters}
repeat with theFile in input
tell application "Pages"
set theDoc to open theFile
set theDocName to name of theDoc
set theName to (characters 1 thru -7 of theDocName) as text
set thePDFPath to ((path to theFile as text) & theName & ".pdf") as text
-- export theDoc to thePDFPath as PDF
close theDoc
end tell
end repeat
end run
The error I get is:
The action “Run AppleScript” encountered an error: “Pages got an error: Can’t make alias "path:to:directory:test.pages" into type constant.”
I've been struggling with this for a while and none of the suggestions I've found online so far have helped to resolve the issue. Any help would be very much appreciated.
path to
only returns the path to the application or script, or to certain locations in the file system such as home folder
or documents folder
. You don't need to use anything to get the path though, as theFile
is already a reference to an item in the input - you can just coerce it to text. Also, once you've built the file path, Pages is expecting a file specifier for the export.
Note that the file path includes the extension, so a little manipulation is needed to separate it from the rest of the name - here I've added a handler to split the file path into the containing folder, the name, and extension, so that they can be mangled as desired:
on run {input, parameters}
repeat with theFile in input
set {folderPath, fileName, extension} to getNamePieces from theFile
tell application "Pages"
set theDoc to open theFile
set theDocName to name of theDoc
set theName to (characters 1 thru -7 of theDocName)
set thePDFPath to (folderPath & fileName & theName & ".pdf")
export theDoc to file thePDFPath as PDF
close theDoc
end tell
end repeat
end run
to getNamePieces from someItem
tell application "System Events" to tell disk item (someItem as text)
set theContainer to the path of container
set {theName, theExtension} to {name, name extension}
end tell
if theExtension is not "" then
set theName to text 1 thru -((count theExtension) + 2) of theName
set theExtension to "." & theExtension
end if
return {theContainer, theName, theExtension}
end getNamePieces