I want to have a folder on my desktop. When I drop Word documents (.docx) into that folder, I want those files to be automatically/instantly converted into PDF files with the same filename (except for the extension, of course).
Macros are always disabled when imported into the Keyboard Maestro Editor.
The user must ensure the macro is enabled.
The user must also ensure the macro's parent macro-group is enabled.
System information
macOS 14.7
Keyboard Maestro v11.0.3
It does, however, require two Unix utilities (pandoc and tectonic) to be installed to do the conversion. It assumes these were installed via Homebrew, but if not, you can modify a variable to point to the path where they reside.
In theory, you can do this by simply watching a folder for a changed file, but the problem is that the macro will see the newly-added file before Word has fully written it. Hence, most of the macro is just a loop to make sure the file has been fully copied across before trying to convert it.
You need to set the trigger to point at the folder you want to watch; it's filled with a dummy path as is.
As written, the macro displays some progress bars and a final "all done" message; if you want it to run with less/no output, just disable some/all of the magenta-colored actions.
If you don't want to install the Unix utilities, you could replace that one shell script action with something else. For example, TextEdit can open .docx files, and has a Save as PDF option. So you could have the files open in TextEdit, then save them as PDF, then close them. I like the Unix approach because it can be done in the background.
The macro works as advertised . . . with one major problem. And that is that the PDF is not formatted correctly. That is, the PDF loses much of the formatting from the Word document. So the PDF that is created is not a 1:1 duplicate of the Word document--and really not close to it.
Any suggestions on how to fix this? Because otherwise this macro is
Word's internal PDF export is still borked (and has been for some time). The "Best for electronic distribution..." option does work, although it uses an MS online service to do the conversion.
If, @maclaw21, you're happy about uploading your super-secret stuff to an MS service, the trick to exporting to PDF using AppleScript is to manually select "Best for electronic..." once, after which it should remain the default -- even when using an AS command in the form:
save as activeDoc file name theOutputPath file format format PDF
But using AS does launch/activate Word, open, and then close the document -- at best, rather distracting. That might be a show-stopper for you.
IIRC, @griffman had decent results turning Excel files into PDFs in the background using LibreOffice via the command line. Could be worth a try...
Not ideal, but if we can do it in the background, that would be better. More importnatly, I'm not sure what I am supposed to do with the AS as it relates to the macro.
I found that as well but did not play around with it. Maybe I will give it a try.
I'm not saying MS are doing anything nefarious with your data. But it is an online service, and personal preference or business compliance may stop you from using it.
If that's the case then you can't use AppleScript. In fact, you can't use Word directly to export to PDF. That leaves you using a third-party converter like @griffman did above, most of which have some conversion issues (for example, LibreOffice doesn't use the Aptos font -- which is the current MS default document font!), or opening the document in Word and then driving the macOS "Print" dialog to save as PDF.
If time isn't a constraint, consider dropping all your documents into that folder but not instantly processing them -- you can then batch-convert them all at a time when Word being active and the Print dialog flashing on and off your screen won't be a distraction.
The new attached version lets you pick from four separate conversion methods: tectonic, xelatex, Word's built-in converter (which requires online tools), or macOS' own built-in print to PDF engine. Specify the desired engine to use in the first green box.
Note that using xelatex requires installing MacTeX from Homebrew, and that's a very long install. The results weren't any better than tectonic to my eye, so I'm not sure it's worth that effort.
I added Word while @Nige_S was posting about it, otherwise I probably would have skipped it. The best output—by far—comes from using print to PDF. Yes, this will briefly open and close some windows onscreen, but if you want fidelity, it was perfect with the three (somewhat complex) test documents I used.
Beyond that, you'll have to investigate what other PDF conversion tools are available for .docx files, as I'm out of time and known alternatives (beyond the mentioned libreoffice, but it definitely has conversion issues).
That's interesting. On my Mac, the page breaks didn't line up with where they were in the original when I converted using Word's converter, but did so perfectly when using Print to PDF.
One of the weird things about all of this is that Word has a "Send PDF" feature that--boom--opens an Outlook compose window with the PDF of the document attached. So there is clearly some fast converting happening within Word. Why that can't be called on outside the app is beyond me.