Saving Doc as PDF and Moving to a New Folder

Hello all,

I’ve had my first mac for about 4 months, and am just getting into automaton and apple script.

I’m currently working on a script that converts a word document into a PDF and moves it from one folder to another. In each of my clients’ folders, I have a “Word Processing” folder and a “Correspondence” folder. My goal is to save the Word document in the “Word Processing” folder and save it as a PDF in the “Correspondence” folder. I’d like to be able to do this from either the context menu or while the word doc is open (so I don’t have to find it in Finder).

I’ve found an apple script that does the conversion, but I’m having problems moving the output from the Word Processing Folder to the Correspondence folder. My initial thought was to do a find and replace on the file path, but that doesn’t seem to be working. It works fine if I remove the reference to replaceText within the “on run,” but then I just get a PDF in the same folder.

property theList : {"doc", "docx"}

on replaceText(find, replace, subject)
	set prevTIDs to text item delimiters of AppleScript
	set text item delimiters of AppleScript to find
	set subject to text items of subject
	
	set text item delimiters of AppleScript to replace
	set subject to "" & subject
	set text item delimiters of AppleScript to prevTIDs
	
	return subject
end replaceText

on run {input, parameters}
	set output to {}
	tell application "Microsoft Word" to set theOldDefaultPath to get default file path file path type documents path
	repeat with x in input
		try
			set theDoc to contents of x
			
			tell application "Finder"
				set theFilePath to container of theDoc as text
				set theFilePath to replaceText("4 Word Processing", "2 Correspondence", theFilePath)
				set ext to name extension of theDoc
				if ext is in theList then
					set theName to name of theDoc
					copy length of theName to l
					copy length of ext to exl
					
					set n to l - exl - 1
					copy characters 1 through n of theName as string to theFilename
					
					set theFilename to theFilename & ".pdf"
					
					tell application "Microsoft Word"
						set default file path file path type documents path path theFilePath
						open theDoc
						set theActiveDoc to the active document
						save as theActiveDoc file format format PDF file name theFilename
						copy (POSIX path of (theFilePath & theFilename as string)) to end of output
						close theActiveDoc
					end tell
				end if
			end tell
		end try
	end repeat
	tell application "Microsoft Word" to set default file path file path type documents path path theOldDefaultPath
	return output
end run

Hi,

try this


property theList : {"doc", "docx"}
property destinationFolderName : "2 Correspondence"

on run {input, parameters}
	set output to {}
	set {TID, text item delimiters} to {text item delimiters, ":"}
	
	repeat with x in input
		try
			set theDoc to contents of x
			set pathComponents to text items of (theDoc as text)
			tell application "System Events" to set {fileName, fileExtension} to {name, name extension} of theDoc
			if fileExtension is in theList then
				
				set baseName to text 1 thru ((get offset of "." & fileExtension in fileName) - 1) of fileName
				set pdfName to baseName & ".pdf"
				set destinationPath to (items 1 thru -3 of pathComponents & {destinationFolderName, pdfName}) as text
				copy POSIX path of destinationPath to end of output
				tell application "Microsoft Word"
					open theDoc
					set theActiveDoc to the active document
					save as theActiveDoc file format format PDF file name destinationPath
					close theActiveDoc saving no
				end tell
			end if
		end try
	end repeat
	set text item delimiters to TID
	return output
end run


Stefan,

Thank you very much. It works like a charm in the context menu.

Nothing appears in the correspondence folder if I run the service while the word document is the active window, however. I know saving as a PDF is easy (Save As, go up a folder, select correspondence, save as PDF). I was hoping to use a keyboard shortcut to do all 4 of those from within Word. I know how to assign a keyboard shortcut to a service, but is running the service on the active document possible?

So, I believe my question is: How do I set the input to be either the Word doc selected in Finder (context menu) or the active word document that’s currently on top?

this version saves the active document if there is one otherwise it takes the input list


property theList : {"doc", "docx"}
property destinationFolderName : "2 Correspondence"

on run {input, parameters}
	tell application "Microsoft Word" to set theActiveDoc to active document
	set output to {}
	set {TID, text item delimiters} to {text item delimiters, ":"}
	if theActiveDoc is not missing value then
		
		tell application "Microsoft Word" to set {fullPath, fileName} to {full name, name} of theActiveDoc
		if fullPath is not fileName then -- fullPath and fileName are the same if the document has never been saved
			set fileExtension to text (get offset of ".doc" in fileName) thru -1 of fileName
			set baseName to text 1 thru ((get offset of fileExtension in fileName) - 1) of fileName
			set pdfName to baseName & ".pdf"
			set pathComponents to text items of fullPath
			set destinationPath to (items 1 thru -3 of pathComponents & {destinationFolderName, pdfName}) as text
			copy POSIX path of destinationPath to end of output
			
			tell application "Microsoft Word"
				save as theActiveDoc file format format PDF file name destinationPath
				-- close theActiveDoc saving no
			end tell
		end if
	else
		repeat with x in input
			try
				set theDoc to contents of x
				set pathComponents to text items of (theDoc as text)
				tell application "System Events" to set {fileName, fileExtension} to {name, name extension} of theDoc
				if fileExtension is in theList then
					
					set baseName to text 1 thru ((get offset of "." & fileExtension in fileName) - 1) of fileName
					set pdfName to baseName & ".pdf"
					set destinationPath to (items 1 thru -3 of pathComponents & {destinationFolderName, pdfName}) as text
					copy POSIX path of destinationPath to end of output
					tell application "Microsoft Word"
						open theDoc
						set theActiveDoc to the active document
						save as theActiveDoc file format format PDF file name destinationPath
						close theActiveDoc saving no
					end tell
				end if
			end try
		end repeat
	end if
	set text item delimiters to TID
	return output
end run


That worked! Thank you very much, and happy New Year.