forked from lemonmade/save-as-PDF
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Save as PDF.applescript
147 lines (126 loc) · 4.94 KB
/
Save as PDF.applescript
1
2
3
4
5
(*
SAVE AS PDF.SCPT
By Chris Sauve of [pxldot](http://pxldot.com).
See README for details.
*)property promptForUserOptions : true -- will change after first runproperty usePathFinderForSelection : false -- option on first runproperty deleteConverted : false -- option on first runif 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 falseend ifif 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 "Path Finder" set selectionList to selection if selectionList is not missing value then set documentList to {} repeat with i from 1 to (length of selectionList) set end of documentList to (path of (item i of selectionList)) end repeat end if end tellend ifif selectionList is missing value then display alert "No files were selected." returnend ifset 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 ifif ((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 ifend ifset WordpreviouslyOpen to (application "Microsoft Word" is running)set powerPreviouslyOpen to (application "Microsoft PowerPoint" is running)if ((length of wordDocumentList ≠ 0) and ((filesDesired = "Both") or (filesDesired = "Word"))) then tell application "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 tellend ifif ((length of powerDocumentList ≠ 0) and ((filesDesired = "Both") or (filesDesired = "PowerPoint"))) then tell application "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 tellend ifif deleteConverted then tell application "Finder" repeat with i from 1 to (length of documentList) delete item i of documentList end repeat end tellend ifto 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 hadExtensionsListend separateFilesByTypeon 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 textend sourceFolderon 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 saveNameend pdfSaveName