Append Text File With Hazel, Automator & Shell Scripts

I love the Drafts app for the iPhone, and I love automating stuff. The downside to using Drafts is that the workflows I’ve set up aren’t possible with any software on the Mac. I cobbled together some methods to achieve at least the one most common task I have, appending info to a master plain-text log file on Dropbox. Here’s how I did it.

[UPDATE 2017-12-30: here’s the companion article on prepending text files]

TextExpander & shell script

The first method uses TextExpander to append my Journal.txt file with the clipboard contents, while also prepending it with a date/time stamp.

Create a new TextExpander snippet with “shell script” as the format in the drop down menu, and assign a shortcut (I chose j// for mine):

DATE_PREPEND=$(date +"%Y-%m-%d %r")
echo -e "\n## $DATE_PREPEND: $FILE_CONTENT" >> /Users/George/Dropbox/Notes/Journal.txt

If your clipboard was the text “Clipboard contents”, you’d see

## 2013-03-21 1:01 AM: Clipboard contents

added to the end of your Journal.txt file. All I need to do is copy some text, invoke the snippet shortcut, and the text on the clipboard gets magically added to my Journal.txt file.

The parts that I struggled with were the new lines (\n). I had to add the -e to get them to register. I have no idea why, just found it out online as one option. The rest of the code was graciously provided by a fellow Hazel user over on the Noodlesoft forums. The “pbpaste” command grabs the content of the OS X clipboard. The ## is just a visual separator, and the date format can be tweaked to your preference.

Automator Service for new dialog prompt

Of course, you need to do this for new text as well so in this case I created an Automator Service that does the same, but prompts you for text. Use the same shell script, but omit the first line (#!/bin/bash):

Obviously you’ll need to change the path to the .txt file you’re using.

There’s probably a better shell script that will grab the clipboard contents instead of doing it through Automator, but I’m lucky I made it this far.

Next, you can assign a keyboard shortcut to this service for system-wide access.

Update: bring the text box to front-most focus

I have been frustrated with this setup due to the fact that I had to constantly use the mouse to click the text box dialog window that was brought up by this Service. I’ve since figured out how to bring that window to the front.

Just add this AppleScript at the start of your Automator service:


like so:

Screen Shot 2018-09-03 at 4.59.58 PM

Now that pop-up window will be ready for typing without further effort.

And I’ve also figured out how to select the OK button via the macOS keyboard: Function key + Return key.

Those two enhancements should further reduce friction.

Other thoughts

If you have more than one log file you’d like to append, I guess you’d just create a new version of each of these and change the file path. And if you were a real coder (unlike me), you might be able to get a shell script going that would also do some Evernote stuff, tweet, or even add new Reminders and such.

For me, I mostly needed this to log random info while at the Mac instead of doing this via the iPhone. I’ll probably set up another one to use with my plain-text task list as well.

I have a bookmarklet set up on the iPhone to pull the title and URL from a webpage and send it to Drafts, for appending to the Journal.txt file, and this bookmarklet will help do something similar on the Mac so you can use it with these hacks.

For those curious, I’ve been working with a plain-text log file setup using Drafts, Dropbox, Hazel, Day One, and the actions over at IFTTT with RSS feeds, Foursquare checkins, and more. Very similar to this article over at Cult of Mac, which I discovered after setting up my system.

I’ll post soon on my setup to save a single journal entry text file to Dropbox via Drafts or Notational Velocity/nvALT, and have the contents automatically added to Day One on the Mac, then prepend the entry with a date/time stamp and append to the plain-text log file.

Why did I spend all my time with these redundant setups? I like Day One, but I also like the idea of not being locked into one system. My plain-text log file setup is in a way similar to the Momento app (which does export the data as plain text), but with my plain-text setup there’s almost zero friction. With Day One and Momento, while I like the apps, I prefer to use them for viewing content — not spending time adding it.

With this setup for the Mac and my Drafts setup, I can quickly grab info or jot it down, fire it off once and have it added to all the places I’d like to store it, in the format I prefer.


2018-09-03: added section on bringing the Automator text dialog box to the front, and info on the selecting the OK button via keyboard shortcut.

14 thoughts on “Append Text File With Hazel, Automator & Shell Scripts

  1. From the way you play with this stuff you may want to take a look at emacs and using the org mode. A lot of the stuff you are doing with these different tools can be all accomplished in emacs and since it is text based, free and runs on almost everything may be a way for you to avoid operating system changes. Aquamacs is a good version for the mac. Since it also ties into lisp it is also completely extensible.

    Anyway cool stuff, amazing amount of energy to keeping up the note taking for so long. thanks

    1. Thanks for the tips Tom. I seem to keep seeing emacs as I dig around creating these little hacks. I’ll have to look into it soon.

      Part of creating these setups is just to have fun making them work, but now that my notes all go where I want them to, it makes it much more enticing to keep the journal going.

  2. George, found this when searching for ways to do similar things, and very grateful for the way you’ve laid this out here. I was able to add a fill-in field in place of “Journal” in “Journal.txt”, so that I can choose which file to send this off to.

    I do have a couple of other thoughts, and just in case you have any wisdom, thought I’d ask: (1) is it possible to prepend rather than append the text? (2) is it possible to do a find and replace, rather than either appending or prepending?

    If you have any hints on either of those, I’d be extremely grateful! They’d be useful variations on this theme.

    1. Glad you found this post helpful Mark. Nice idea on the fill-in snippet for the file name.

      As far as pretending text vs. appending it, I am sure that’s possible. Unfortunately, as explained in the post I duct tape these scripts together, so I’m not able to help. But I think there were pretend variants of those scripts. I’m not so sure about the find and replace stuff. If you discover info on either it would be great if you could stop back and share.

  3. Thanks George. I fear I am, like you, am too much of a ‘duct taper’ to succeed – but I’ll let you know if I find something that works.

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s