Convert Word to PDF with Contextmenu/Script

Hi

Does anybody know is there a way to convert a .doc or docx in a single click to a .pdf who is stored at the same place with the same tags in the database?

Very offen we editing a word and the convert it to a pdf before we send it. Does anybody has a idea to deal with that a quick and easy way?

Thanks you thinking about :slight_smile:

How do you currently convert the document to PDF? The best solution is probably to open the document with Word or Pages and to print the document to a PDF but that’s hardly scriptable.

It should be doable fairly easy. Word’s applescript support is actually very good.

I use an automator script for batch conversion of Word files similar to this one:

https://discussions.apple.com/message/15180576#15180576

It should be fairly easy to adapt the script to open the selected files in Word and import the resultant pdfs back into DT.

Sorry just don’t have time right now to tackle this myself although I would find it very useful too.

Frederiko

Google is your friend. There are several solutions at the top of a search for “applescript word pdf” – e.g., just two:

Standalone ApplesScript (which Frederiko pointed out): discussions.apple.com/message/15180576#15180576
Automator version: onabai.wordpress.com/2012/07/10 … df-in-osx/

You’ll not be able to put this into the contextual menu, but you could take one of those starting points and wrap a DEVONthink script around it that would do the bit about “store in the same place [i.e., the source group] with the same tags”. Your final script could be installed into DEVONthink’s Scripts menu or added to the toolbar. Or you could modify the Automator version and make it into a service executable from DEVONthink’s Services menu.

You’re right, it’s doable as there’s already a script on my disk dating back to January :slight_smile:


-- Convert Word documents to PDF

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

tell application id "DNtp"
	set theInput to {}
	set theSelection to the selection
	repeat with theRecord in theSelection
		set theFile to (path of theRecord) as POSIX file
		set theDoc to theFile as alias
		
		tell application "Finder"
			set theFilePath to container of theDoc as text
			set ext to name extension of theDoc
			if ext is in extensionList then
				set theName to name of theDoc
				set theFilename to characters 1 through ((length of theName) - (length of ext) - 1) of theName as string
				set theFilename to theFilename & ".pdf"
				
				tell application "Microsoft Word"
					set theOldDefaultPath to get default file path file path type documents path
					set default file path file path type documents path path theFilePath
					open theDoc
					set theActiveDoc to the active document
					set theFilePath to (path to desktop folder) as string
					set default file path file path type documents path path theFilePath
					save as theActiveDoc file format format PDF file name theFilename
					close theActiveDoc
					set default file path file path type documents path path theOldDefaultPath
				end tell
				
				set theConvertedPath to (theFilePath & theFilename as string)
			end if
		end tell
		
		set theConvertedRecord to import POSIX path of theConvertedPath to (parent 1 of theRecord)
		if exists theConvertedRecord then tell application "Finder" to delete theConvertedPath
	end repeat
end tell

1 Like

Thanks Christian. That will be most helpful.

There is one edge case when the script will fail. Word saves documents divided by section breaks into separate pdfs. In that case only the pdf with the first section will be imported.

Frederiko

I say stop using Word and start using Markdown! :mrgreen:

(Haha! Did you EVER imagine me saying that, korm?? :smiley: :laughing: )

Someone needs to tell DEVONtech that Jim’s account has been hacked. :laughing:

Hahaha! You started the ball rolling and I’ve slowly fallen in love with it. So… thanks. Haha! :smiley:

Wow, so much response during one way.

To the first answers: It’s not about just convert word to pdf. That was maybe a trick a few years ago, it was more to put the tags on.

But thank you very much for the script. I will test it asap. It looks what i’m was looking for.

Woow! The Scripts works… not at all, but close :slight_smile:

I’m using it on a Mac and it creats the pdf, but it put it in the computer trash bin :smiley:

How can i change the script to don’t to that :mrgreen: :mrgreen:

In Christian’s script after the line


set theConvertedRecord to import POSIX path of theConvertedPath to (parent 1 of theRecord)

add the line


set tags of theConvertedRecord to tags of theRecord

and all the tags from the word document will be copied to the pdf.

Otherwise the script worked well for me. There will be a copy of the pdf in the trash. This is nothing to worry about. Word has to save the file before it can be imported into DT. The pdf is imported into the same group as the original pdf which is where you should look for it.

In Word 2016 you have to go through a serious of pop up boxes authorising access to the Desktop before the script will work for the first time.

Frederiko

You guys are Amazing!

It works like a charm :slight_smile:

Thank you very, very much for your great support!

Can we use you as a character witness in some other threads, where the prevailing opinion is that “the devs are lazy” and the “forum members hostile”?

Since the last update it doesn’t work anymore. But i have no idea where to check first, why it does’t work anymore. But of course you can use my case. The devs are definitely not lazy!

You didn’t mention what the “last update” was – DEVONthink, OS X, Word, something else? But I ran Criss’s script through Script Debugger and this statement fails:


set theOldDefaultPath to get default file path file path type documents path

with an AppleScript compiler error “Expected end of line but found class name.” Typically vague Apple error message. I’m running Word 15.16 (151105), and the Word dictionary appears to support this statement, but it actually doesn’t.

Anyway, looks like the script is broken over here because something broke Word’s AppleScript support.

The failed statement is merely the first broken statement in the script – there might be others.

Word 15.16 (151105)
DT 2.8.8
El Capitan 10.11.2

and it still works for me

Frederiko

I wonder if I have have a potential conflict between Word 15.16 running on the OS X side and Word 2013 running under a VM – Parallels is sharing OS X/Windows 10 apps both directions. Script Debugger and Apple Script Editor each kept trying to launch the Windows instance of Word.

Mhh i have the same status like Frederick, and there it works. Strange!

Word: 15.16
Mac: 10.11.2
DT: 2.8.8

Has the Problem have to come from another 3rd Party on my Computer… Puh… hard to find out. Any idea where to start to check. The problem is, i can’t really say what change between the point of working and not working.

Found and tested this script just now, and it works perfectly! Thanks so much - this will be a major time saver for me.