Help me! I want to easily export a document, with hierarchical outline, from Drafts to Microsoft Word (MSWord) - and have the outline be properly indented in Word’s stupid non-standard way. It’s easy to copy the Markdown-formatted draft as Rich Text (I think I have more than one action that will do that). But what comes next? Happy to automate in Drafts, Shortcuts, Keyboard Maestro or BetterTouchTool, StreamDeck or a combination of the above. I don’t know AppleScript, but I know how to execute an AppleScript from any one of those.
Thinking laterally, and perhaps to no avail: Would it be feasible to transform the text on the Drafts side so that when translated to RTF it does the right thing in Word?
I’m just thinking manipulation is probably easier in Drafts than writing some eg VBA to do it in Word.
MS Word’s hierarchical outline probably most closely resembles HTML lists. Therefore. in Drafts, I would create a Markdown list. Of course various levels of indentation can be used to represent different levels of nesting.
- Item 1
- Item 1.1
- Item 1.1.1
- Item 1.2
- item 2
Copy that as rich text and Word should understand it correctly when it is pasted in. At a minimum, Word should maintain the correct nesting. The styles may be all weird. Although Word provides various options when pasting. You can paste and merge styles, paste and keep source files, or paste and use destination styles. Just use the best one for your situation. I have found that if you paste into an existing outline in a Word document (even if the outline is empty) using the existing styles (or merging styles) will often give the best results. Word will apply the style of the existing (empty outline) to the pasted items.
Best of luck with this!
I semi-frequently have to move outline content from Drafts to Word. I usually just paste it as plain text and rebuild everything in Word, using the Drafts version as a guide. In my experience, Word tries to help you with autoformatting, and it all falls apart.
However, I would love to see an answer!
Here’s a walkthrough.
First of all, you want to install a command line utility (if you haven’t already) called Pandoc. This is a utility to help you to convert between textual file formats - such as from Markdown to Word Document.
I would recommend using the Homebrew package manager to manage installs and updates of your command line tools. If you use Homebrew, installing Pandoc is as easy as entering this command in your Terminal app:
brew install pandoc
The next step is to build a Drafts action to allow us to use Pandoc from within Drafts to convert Markdown to a DOCX format.
To do this I’m going to use some JavaScript in a Drafts script action step. Drafts’ JavaScript model offers us a class to interact with shell scripting, which is exactly what we need for interacting with a command line utility like Pandoc.
Here’s my script:
let strScript = `#!/bin/zsh
DOCPATH="$HOME/Desktop/pandoc_drafts.docx"
PANDOCPATH="/usr/local/bin/pandoc"
echo "$1" | "$PANDOCPATH" -f markdown -t docx -o "$DOCPATH"
open "$DOCPATH"
`;
let objShellScript = ShellScript.create(strScript);
if (!objShellScript.execute([draft.content])) console.log("STDERR: " + objShellScript.standardError);
The two let lines and the last line run the shell script, but let’s pull that first script definition out and take a closer look.
#!/bin/zsh
DOCPATH="$HOME/Desktop/pandoc_drafts.docx"
PANDOCPATH="/usr/local/bin/pandoc"
echo "$1" | "$PANDOCPATH" -f markdown -t docx -o "$DOCPATH"
open "$DOCPATH"
The first line is our shebang to let the OS know we want to run our script in Z-shell (the default macOS shell).
The next line defines a variable called DOCPATH
that I’ve set to be a file called pandoc_drafts.docx
on my Desktop. That is going to be where Pandoc is instructed to save the output Word document to.
The third line is where to find Pandoc. Often, when running scripts through third party apps like Drafts, you either need to pull in a profile, or be specific where to find stuff - here I am being specific. If Pandoc is installed elsewhere, you’ll need to put in your own path.
If you want to find out where Pandoc is installed, open your Terminal and enter which pandoc
, and that should tell you where to find it, and you can just pop those details into the script.
The third line takes the first argument to the script and tells pandoc to convert it from Markdown to DOCX, and save it out to the file specified by the DOCPATH
variable. The first argument is specified in the last line of the JavaScript above. It is the content of the current draft opened in the Drafts editor when you run nthe action (specified as draft.content
).
The fourth line tells the OS to open the newly created file. It should open in Word.
To save typing, here’s the action for folks to use, set their own paths, and to modify to take advantage of all those lovely options.
Now let’s have an example…
Here’s some Markdown text I placed in a draft.
# Heading 1
Let's have a nursery rhyme.
## Heading 1.1
Sing along if you know it
### Heading 1.1.1
Head, shoulders, knees and toes,
Knees and toes.
### Heading 1.1.2
Head, shoulders, knees and toes,
Knees and toes.
## Heading 1.2
And eyes, and ears, and mouth, and nose.
# Heading 2
Head, shoulders, knees and toes,
## Heading 2.1
Knees and toes.
I’m using headings here because that’s what Microsoft Word’s outlining is based on … so I would have to heartily diagree with this being a “stupid non-standard way” for indentation. Its the same approach used for collapsing sections in many text-based editors.
After triggering the action, it generates, and after a few moments opens, the Word document. The amount of time will vary based on machine speed, length and complexity of text, etc.
Here’s how the output looked for me when the document opened in Word’s Print Layout view.
That’s just used whatever are set as Word’s default headings are. I don’t use Word a lot on my Mac, so I haven’t bothered changing my defaults. But it is worth noting here that you can specify a template document for Pandoc to use when generating DOCX output, and that is well worth looking into if you start making more use of Pandoc
How does this look in Outline view? Well, I’m glad you asked…
… it looks exactly as I would hope (/expect).
Now, as an exercise for anyone who is particularly interested in nudging the Drafts action a little further, Word supports AppleScript, and Drafts can run AppleScript scripts.
This standalone script opens a particular file on my desktop and then instructs Word to display the active window (which should be the one displaying this file in outline view).
tell application "Finder"
open POSIX file "/Users/stephen/Desktop/pandoc_drafts.docx"
end tell
tell application "Microsoft Word"
activate
set view type of view of active window to outline view
end tell
The action can therefore be modified to take advantage of this if viewing in Word’s Outline view each time is preferred.
Hopefully, that all makes sense, and gives anyone who wants to work with outlines in Word, or even just use Drafts to start off or generate Word documents on their Mac a good place to start.
Should anyone come across a way to run Pandoc on i*OS, do let me know. I have to round trip content to my Mac mini and have Hazel run Pandoc commands for me on content I push to it. I really don’t want to trust my content to third party online services for this sort of thing, and I feel a local option would be much more convenient.
Incredible as always!
Thank you for the script, and, just as much, for the explanation. Now I know how to make my hierarchical headers turn into Word heading styles, which is a different problem I had wanted to solve. And I agree with you that this isn’t something Word handles in a non-standard way.
I have installed Pandoc and just run this action, and it does most of what I need it to do. Not only does it properly translate my headings, but it also properly translates hierarchical lists (which is what I should have said I needed in the first place). By hierarchical lists, I mean text, as denoted in Drafts with bullet points and indented bullet points. This is where Microsoft is non-standard: the way it understands indented text.
My only remaining glitch is that text I typed with hard returns at the end of each line and no list notation (via * or -) just runs on, with no line breaks. Something for me to explore.