Word Document to PDF

Get help. Get answers. Let others lend you a hand.

Moderator: Mr_Noodle

Word Document to PDF Mon Jul 16, 2012 10:11 pm • by Scrubbs
I have a selection of word documents that I'm downloading and I want to convert to PDF's and put on a cloud server to access via my iPad. Each report contains the same title which I'd like to use as a contents filter to only work convert the document in an action folder where I collect my work initially. Most of what I find in google is old answers using a "word convert file format to" command that is no longer used.

I've tried an automator workflow that opens the PDFs and uses the word save as PDF but the PDFs are corrupted.

I was able to use the applescript from this link https://discussions.apple.com/thread/3050596?start=0&tstart=0 to create a service to converts all word documents to PDF's. This would require me to do this portion manually. I can have Hazel automove the PDF's to the cloud folder. I would have to clean up the folder manually as well.

This seems to be a widespread need, any help on how to approach would be appreciated.
Scrubbs
 
Posts: 10
Joined: Mon Jul 16, 2012 9:46 pm

Re: Word Document to PDF Tue Jul 17, 2012 8:14 pm • by Scrubbs
I think I have a workable solution.

I created a folder and had hazel move files based on the Title that always appears in the document.
I created a folder action in automator using the script from here https://discussions.apple.com/thread/3050596?start=0&tstart=0 which converts all the docs by opening in Word 2011 and saving as a PDF in the same folder.
Simple move to Dropbox of PDF based on extension with Hazel and delete Documents that are 3 mins. old or longer in the folder.

This seems to work so far. The folder action bit seems to have solved my problem.
Scrubbs
 
Posts: 10
Joined: Mon Jul 16, 2012 9:46 pm

Re: Word Document to PDF Tue Feb 11, 2014 12:06 pm • by theFile
Sorry to open an old thread, but has anybody else made any progress, here? I want to automatically convert any .doc/.docx file in my Downloads folder to PDF if I append [toPDF] to the filename before saving to Downloads. I don't have any problem having Hazel identify the files correctly, but I do not know what to do with the script noted above (https://discussions.apple.com/thread/30 ... 0&tstart=0).

That is, I can get the Automator service working just fine; when I right-click a file and select Word2PDF from the Services menu, it works like a charm. But I want to have Hazel execute the service automagically, and I can't figure out how to have this happen. Are folder actions the only solution? Or is there an easy method to take the Automator Service script and convert it into something Hazel can work with (i.e. shell script, Applescript, etc.)?

Any help is appreciated -- thanks!
theFile
 
Posts: 7
Joined: Tue Feb 11, 2014 12:01 pm

Re: Word Document to PDF Tue Feb 11, 2014 4:03 pm • by Mr_Noodle
I'm not sure why Automator is needed at all (or a folder action). If it's a script, then use Hazel's "Run AppleScript" action. You will need to convert the script to work with Hazel but outside of that, it avoids the rigamarole of running the script via Automator.
Mr_Noodle
Site Admin
 
Posts: 11235
Joined: Sun Sep 03, 2006 1:30 am
Location: New York City

Re: Word Document to PDF Tue Feb 11, 2014 4:26 pm • by theFile
Here's the AppleScript used in Automator, ran as a service, taking "files/folders from Finder" as input:

Code: Select all
property theList : {"doc", "docx"}
 
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 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


I'm okay, but by no means good, at working through AppleScript, so I am a bit unsure how to adopt this script to accommodate Hazel's use of "theFile" as an input. Would I substitute "theFile" for "x" in "repeat with x in input" on line 6? (i.e. "repeat with theFile in input")
theFile
 
Posts: 7
Joined: Tue Feb 11, 2014 12:01 pm

Re: Word Document to PDF Tue Feb 11, 2014 5:10 pm • by Mr_Noodle
Read the help on AppleScript. If you are going to paste the script into Hazel, then you need to get rid of the handler ("on run"). You'll also have to figure out the deal with converting the use of "parameters" to using "theFile" instead.
Mr_Noodle
Site Admin
 
Posts: 11235
Joined: Sun Sep 03, 2006 1:30 am
Location: New York City

Re: Word Document to PDF Tue Feb 11, 2014 5:32 pm • by theFile
I'll see what I can figure out. Thanks!
theFile
 
Posts: 7
Joined: Tue Feb 11, 2014 12:01 pm

Re: Word Document to PDF Thu Feb 13, 2014 2:44 pm • by Scrubbs
Mr_Noodle wrote:Read the help on AppleScript. If you are going to paste the script into Hazel, then you need to get rid of the handler ("on run"). You'll also have to figure out the deal with converting the use of "parameters" to using "theFile" instead.


This was when I first got Hazel and hadn't yet figured out how to adapt the scripts. I'll put it on the list of things to update now that someone else is interested. I was able to make it work with the folder action so I've not updated since that time.
Scrubbs
 
Posts: 10
Joined: Mon Jul 16, 2012 9:46 pm

Re: Word Document to PDF Sat Mar 01, 2014 1:35 pm • by Scrubbs
Had some time to sort this out. Explained here: http://www.scrubbs.me/my-notes-blog/2014/3/1/automatically-convert-word-documents-to-pdf

Here is the script, enjoy!

Code: Select all
-- this script adapted from post at https://discussions.apple.com/thread/3050596?start=0&tstart=0 solution by spazek
-- details on the use of this script for Hazel found a http://www.scrubbs.me/my-notes-blog/2014/3/1/automatically-convert-word-documents-to-pdf

tell application "Microsoft Word" to set theOldDefaultPath to get default file path file path type documents path -- looks like we change the default path to where the document is and then set it back when we're done
try
    tell application "Finder"
        set theFilePath to container of theFile as text
       
        set ext to name extension of theFile
       
        set theName to name of theFile
        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 theFile
            set theActiveDoc to the active document
            save as theActiveDoc file format format PDF file name theFilename
            close theActiveDoc
        end tell
       
    end tell
   
end try
try
   
    tell application "Microsoft Word" to set default file path file path type documents path path theOldDefaultPath
   
end try
Scrubbs
 
Posts: 10
Joined: Mon Jul 16, 2012 9:46 pm

Re: Word Document to PDF Mon Jan 04, 2016 5:55 pm • by mobileliving
I have used the script below to watch a Google Drive folder, and anytime a Word file is added or modified, it creates a new PDF. It works great except.... When Word is first opened, if I have another Word file open when I save and close a Word file to Drive, it takes the content of the open Word file and saves it with the name of the file I saved to Drive. This got me into trouble by posting online private content! Once Word has been open, and a file saved and processed by the script, following saves and edits will work correctly (though it's processing the PDF 2x for some reason). This script is very dangerous with a bug like that. I wonder if anyone else has had the same problem, or if it is something I did wrong??

Scrubbs wrote:Had some time to sort this out. Explained here: http://www.scrubbs.me/my-notes-blog/2014/3/1/automatically-convert-word-documents-to-pdf

Here is the script, enjoy!

Code: Select all
-- this script adapted from post at https://discussions.apple.com/thread/3050596?start=0&tstart=0 solution by spazek
-- details on the use of this script for Hazel found a http://www.scrubbs.me/my-notes-blog/2014/3/1/automatically-convert-word-documents-to-pdf

tell application "Microsoft Word" to set theOldDefaultPath to get default file path file path type documents path -- looks like we change the default path to where the document is and then set it back when we're done
try
    tell application "Finder"
        set theFilePath to container of theFile as text
       
        set ext to name extension of theFile
       
        set theName to name of theFile
        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 theFile
            set theActiveDoc to the active document
            save as theActiveDoc file format format PDF file name theFilename
            close theActiveDoc
        end tell
       
    end tell
   
end try
try
   
    tell application "Microsoft Word" to set default file path file path type documents path path theOldDefaultPath
   
end try
mobileliving
 
Posts: 1
Joined: Mon Jan 04, 2016 5:41 pm

Re: Word Document to PDF Sun Jan 10, 2016 12:44 pm • by Scrubbs
Ugh - that is not the intended behavior! I must say I've never used in any other way than on a private folder. I don't have time at the moment to try to trouble shoot but thanks for sharing this. I'll post as a bug to the script on my site.
Scrubbs
 
Posts: 10
Joined: Mon Jul 16, 2012 9:46 pm

Re: Word Document to PDF Fri Aug 05, 2016 12:51 pm • by theFile
Scrubbs-

Just saw this - thanks for the update! It works great. I'll keep in mind the bug, though - mainly I use it to convert non-private writing to PDF, so it shouldn't affect me much.
theFile
 
Posts: 7
Joined: Tue Feb 11, 2014 12:01 pm

Re: Word Document to PDF Mon Jan 09, 2023 3:32 pm • by gedeyenite
Hey all. After weeks of on-and-off searching for a script that would convert Word docs to PDFs as part of a Hazel workflow, I found this! Everything works except for the hiccup where Word pops up a window which asks to "Grant File Access" one or multiple times. Is there something that can be added to the script to avoid that pop-up, or grant the access before it asks?
gedeyenite
 
Posts: 4
Joined: Fri Jun 21, 2019 2:23 pm


Return to Support