You can make a difference in the Apple Support Community!

When you sign up with your Apple Account, you can provide valuable feedback to other community members by upvoting helpful replies and User Tips.

Looks like no one’s replied in a while. To start the conversation again, simply ask a new question.

how do I use automator to batch convert doc to pdf?

Although I thought it would be an easy thing to do I'm finding it difficult to get much info or help on this. The few scripts and instructions I have discovered on the web don't seem to work. I tried to create one with a get file and then convert word docx. It creates the pdfs but they are corrupted and can't be opened.


Any ideas anyone?

Macpro, Mac OS X (10.6.4)

Posted on May 11, 2011 12:06 AM

Reply
90 replies

May 27, 2014 8:25 AM in response to Aaron Lee

Here's an applescript service which converts individual files to PDFs and then merges the sections. You can download the service via the below link and add it to library > services or you can use the script posted below:


http://goo.gl/ikQJ9k


One question though. I took the number of sections on a new page ("section new page", "section odd page", "section even page") as an indicator for the number of PDFs the document would produce. This isn't always correct however (thereby stalling the script). Apparently there are other factors influencing the number of PDFs produced by Word for a document. If anyone knows a better way of finding the number of output PDFs from Word, please let me know!


on run {input, parameters}

set the_counter to count of input

repeat with i from 1 to the_counter

set cur_file to (item i of input) as string

try

do shell script "cd " & quoted form of POSIX path of posix_file_path

display dialog "This script does not work with folders."

set the_counter to 0

exit repeat

end try

-- REMOVE EXTENSION

set theCharacters to characters of cur_file

set theReverse to reverse of theCharacters as string

set extension_length to (offset of "." in theReverse) + 1

set file_base to (text 1 thru -extension_length) of cur_file

set sectionCount to 0

set file_name to file_base & ".pdf"

tell application "Microsoft Word"

open cur_file

tell active document

activate

repeat with i from 1 to count of sections

if (section start of page setup of section i) as text is in {"section even page", "section odd page", "section new page"} then

set sectionCount to sectionCount + 1

end if

end repeat

end tell

tell application "Finder"

if exists file file_name then

repeat with i from 1 to 10000

set file_name to (file_base & "(" & i & ").pdf")

if not (exists file file_name) then exit repeat

end repeat

end if

end tell

save as active document file format format PDF file name file_name

end tell

set file_search to (text 1 thru -5) of file_name

if sectionCount > 2 then

set last_file to file_search & "." & sectionCount - 1 & ".pdf"

else

set last_file to file_name

end if

tell application "Finder"

set counter to 0

repeat until ((file last_file exists) or (counter = 600))

delay 0.5

set counter to counter + 1

end repeat

end tell

delay 0.5

set merge_files to every paragraph of (do shell script "ls -tr " & quoted form of POSIX path of file_search & "*" & ".pdf")

if (count of merge_files) > 1 then

set newFirst to quoted form of POSIX path of (file_search & ".1.pdf")

set item 1 of merge_files to newFirst

do shell script "mv " & " " & quoted form of POSIX path of file_name & " " & newFirst

set merge_file_paths to item 1 of merge_files

repeat with merge_file in (items 2 thru end of merge_files)

set merge_file_paths to merge_file_paths & " " & quoted form of merge_file

end repeat

do shell script "/System/Library/Automator/Combine\\ PDF\\ Pages.action/Contents/Resources/join.py -o " & quoted form of POSIX path of file_name & " " & merge_file_paths

tell application "Finder"

set counter to 0

repeat until ((file file_name exists) or (counter = 1800))

delay 0.5

set counter to counter + 1

end repeat

end tell

delay 2

do shell script "rm " & merge_file_paths

end if

end repeat

if the_counter > 0 then

tell application "System Events"

activate

display dialog ("Script Complete! " & return & return & the_counter & " files converted") buttons {"OK"} default button 1 with title "Convert to PDF"

end tell

end if

end run

Jun 30, 2014 10:34 PM in response to zalamander

I also have the same problem showing "Syntax Error: Expected end of line but found end of script" using the original script.

According to related posts, this problem mostly occurs in users with Maverick + Word + Parallel.

The reason seems to be that the "Microsoft word" in the script no longer directs to Microsoft word.app in MAC because Parallel has done some changes in /application

Therefore, the solution is simple.

Simply substitute "Microsoft Word" with the correct directory, e.g.. "/Applications/Microsoft Office 2011/Microsoft Word.app" and it will work again.

Hope this would be helpful 🙂


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


on run {input, parameters}
  set output to {}
  tell application "/Applications/Microsoft Office 2011/Microsoft Word.app" 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 ext to name extension of theDoc
  if ext is in theList then
  set x to (length of ext) + 2
  set theName to (text 1 thru -x) of (theDoc as string)
  set thePDF to theName & ".pdf"

  if (exists file thePDF) then
  set n to 1
  set thePDF to theName & "_" & n & ".pdf"
  repeat until (exists file thePDF) is false
  if (exists file thePDF) then
  set n to n + 1
  set thePDF to theName & "_" & n & ".pdf"
  end if
  end repeat
  end if



  tell application "/Applications/Microsoft Office 2011/Microsoft Word.app"
  open theDoc
  set theActiveDoc to the active document
  save as theActiveDoc file format format PDF file name thePDF
  copy (POSIX path of thePDF) to end of output
  close theActiveDoc
  end tell

  end if
  end tell


  end try
  end repeat

  tell application "/Applications/Microsoft Office 2011/Microsoft Word.app" to set default file path file path type documents path path theOldDefaultPath


  return output
end run

Aug 24, 2015 9:45 AM in response to spazek

This works except that the closing of a file is odd. Today I ran it on two groups of 20 Word 2011 files and all produced PDFs. Okay so far, and on my system at least I am used to the fact that when I create a PDF, as I close the file I am [most often] asked if I want to save changes, but here is the first oddity: In the first 20 I was prompted to save changes as each closed after each PDF. In the second 20, I was not.

But more strange is that once the PDFs were done, all forty re-opened and even then, all had a "save changes" prompt!

So I guess my question is can you change the script to "just say yes" to everything? And I suppose not open them again? I am not concerned about breaking anything, as I would never run such a script on a bunch of files where those files were my only copy.

Sep 28, 2015 12:32 PM in response to spazek

Spazek,


Thank you very much for taking the time to post this. The script works perfectly and helps when I have multiple reports (word docs) I need to PDF. Just a question though, it doesn't seem to work if I have images in my reports. Is there a way to tweak it so it can accommodate word documents with images as well?


Thank you in advance, you've saved me tons of time already!


-Josh


P.S. I'm using OSX 10.9.5 if that helps.

Nov 24, 2015 9:35 PM in response to spazek

Hi Spazek.


I hope you're well. Just to let you know, your amazing script to save word to PDF and add a number if file already exists (below) does not seem to works in the new Word (Office 2016). I've tried to fix but no luck.


tell application "Microsoft Word"

set theDoc to path of active document as alias


tell application "Finder"

set ext to name extension of theDoc

set x to (length of ext) + 2

set theName to (text 1 thru -x) of (theDoc as string)

set thePDF to theName & ".pdf"



--IF FILE ALREADY EXISTS ADD A NUMBER (INCREASING)

if (existsfilethePDF) then

set n to 1

set thePDF to theName & "_" & n & ".pdf"

repeat until (existsfilethePDF) is false

if (existsfilethePDF) then

set n to n + 1

set thePDF to theName & "_" & n & ".pdf"

end if

end repeat

end if


end tell


set theActiveDoc to the active document

try


save astheActiveDocfile formatformat PDFfile namethePDF

on error


display alert "Error"


end try

-- close theActiveDoc


end tell


end run

Dec 9, 2016 6:43 AM in response to zalamander

Hi,


I'm desperate to get this to work. My students find it virtually impossible to create a Pdf when they send me their work to be marked. I'd like to be able to get this script working properly. I've created a service in automator, and added run AppleScript. Set the service to receive files or folders from finder. Added the following script from this page...


on

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


onrun {input, parameters}

set output to {}

tell application "Microsoft Word" to set theOldDefaultPathtoget default file pathfile path typedocuments 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 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 pathfile path typedocuments pathpaththeFilePath

open theDoc

set theActiveDoc to the active document


save astheActiveDocfile formatformat PDFfile nametheFilename

copy (POSIX path of (theFilePath & theFilename as string)) to end of output


closetheActiveDoc

end tell

end if

end tell

end try

end repeat

tell application "Microsoft Word" to set default file pathfile path typedocuments pathpaththeOldDefaultPath



return output

end run

I've saved the service and can see in the right click menu. When I select a Document and convert, word pops up and closes again but not file is converted. If I put a file on the desktop it works. I'm presuming the file path is too long. Is there a fix for this?

Thanks in advance


Neil

how do I use automator to batch convert doc to pdf?

Welcome to Apple Support Community
A forum where Apple customers help each other with their products. Get started with your Apple Account.