I have found a wonderful script online to convert Word to PDF. It works great as a Service for selected files. However, I would like to automate a Hazel workflow so that Hazel watches a folder for Word files, converts them to PDF using the AppleScript, tags them and files them away in a sub-folder. When I run the AppleScript that way, it has an error. This is probably because no files are selected in the Finder. Does any know how to ammend this script so it will do what I want?
Here is the script:
(*
SAVE AS PDF.SCPT
By Chris Sauve of pxldot.
See README for details.
*)
property promptForUserOptions : false – will change after first run
property usePathFinderForSelection : false – option on first run
property deleteConverted : false – option on first run
if promptForUserOptions then
display dialog ¬
“Do you use Path Finder for all your file browsing needs?” buttons {“Yes, I Do!”, “No, I Use Finder”} default button 2
set usePathFinderForSelection to ((button returned of result) is “Yes, I Do!”)
display dialog ¬
“Do you want to delete the converted files?” buttons {“Yes, Delete”, “No, Keep Them”} default button 2
set deleteConverted to ((button returned of result) is “Yes, Delete”)
set promptForUserOptions to false
end if
if not usePathFinderForSelection then
tell application “Finder”
set selectionList to selection
set documentList to {}
if length of selectionList ≠0 then
repeat with i from 1 to (length of selectionList)
set end of documentList to ((item i of selectionList) as text)
end repeat
end if
end tell
else
tell application “Finder”
set selectionList to «class slcT»
if selectionList is not missing value then
set documentList to {}
repeat with i from 1 to (length of selectionList)
set end of documentList to («class pthH» of (item i of selectionList))
end repeat
end if
end tell
end if
if selectionList is missing value then
display alert “No files were selected.”
return
end if
set wordDocumentList to separateFilesByType(documentList, {“doc”, “docx”})
set powerDocumentList to separateFilesByType(documentList, {“ppt”, “pptx”, “pptm”})
set filesDesired to “Both”
if ((length of wordDocumentList) = 0) and ((length of powerDocumentList) = 0) then
display alert “No Word (.doc, .docx) or PowerPoint (.ppt, .pptx, .pptm) files were selected.”
end if
if ((length of wordDocumentList) > 0) and ((length of powerDocumentList) > 0) then
display dialog ¬
“Would you like to convert Word documents only, PowerPoint documents only, or both?” buttons {“Word”, “PowerPoint”, “Both”} default button “Both” giving up after 15
if gave up of result then
return
else
set filesDesired to (button returned of result)
end if
end if
set WordpreviouslyOpen to (application id “com.microsoft.word” is running)
set powerPreviouslyOpen to (application id “com.microsoft.powerpoint” is running)
if ((length of wordDocumentList ≠0) and ((filesDesired = “Both”) or (filesDesired = “Word”))) then
tell application id “com.microsoft.word”
activate
set tempDefaultDocumentPath to (get default file path file path type documents path)
repeat with i from 1 to (length of wordDocumentList)
-- Fixes documents from saving in the default folder instead of the source folder
set default file path file path type documents path path (my sourceFolder(item i of wordDocumentList))
set printDoc to open file name (item i of wordDocumentList)
set nameSave to my pdfSaveName(name of printDoc)
try
save as printDoc file format format PDF file name nameSave
end try
close printDoc saving no
end repeat
set default file path file path type documents path path tempDefaultDocumentPath
if not WordpreviouslyOpen then quit
end tell
end if
if ((length of powerDocumentList ≠0) and ((filesDesired = “Both”) or (filesDesired = “PowerPoint”))) then
tell application id “com.microsoft.powerpoint”
activate
repeat with i from 1 to (length of powerDocumentList)
open item i of powerDocumentList
set printDoc to (first presentation whose full name = ((item i of powerDocumentList) as string))
set theSource to (my sourceFolder(item i of powerDocumentList) & ":" & my pdfSaveName(name of printDoc))
save printDoc in theSource as save as PDF
close printDoc saving no
end repeat
if not powerPreviouslyOpen then quit
end tell
end if
if deleteConverted then
tell application “Finder”
repeat with i from 1 to (length of documentList)
delete item i of documentList
end repeat
end tell
end if
to separateFilesByType(listOfFiles, listOfExtensions)
set hadExtensionsList to {}
set text item delimiters to {“.”}
repeat with i from 1 to (length of listOfFiles)
set extensionType to (last item of (text items of (item i of listOfFiles)))
repeat with j from 1 to (length of listOfExtensions)
if extensionType = (item j of listOfExtensions) then
set end of hadExtensionsList to (item i of listOfFiles)
exit repeat
end if
end repeat
end repeat
set text item delimiters to {“”}
return hadExtensionsList
end separateFilesByType
on sourceFolder(filePath)
set my text item delimiters to {“:”}
set sourceFolderPath to ((items 1 thru -2 of (text items of filePath)) as text)
set my text item delimiters to {“”}
return sourceFolderPath as text
end sourceFolder
on pdfSaveName(fileName)
set text item delimiters to {“.”}
set namePieces to text items of fileName
set last item of namePieces to “pdf”
set saveName to namePieces as text
set text item delimiters to {“”}
return saveName
end pdfSaveName