Prepend Text File with Automator & Shell Scripts

As a followup to my earlier post about how to create a macOS Service (or a Text Expander snippet) to quickly append a text file using Hazel, Automator and Shell Scripts, here’s a way to instead prepend text to a text file using the same tools.

prepend text automator screenshot

Why would you want to do this?

My core interest in setting this up is for plain-text journaling. In a way, I am cobbling together the functionality I find extraordinarily useful in the iOS app Drafts for use on macOS.

I like to capture ideas, thoughts, errands, links, apps, articles, tasks and todos quickly, and I use a plain-text document system I’ve put together over the years. These automation setups remove friction in doing so, which make it more likely I will maintain the behavior.

With this setup and the companion append text automation setup, I can hit a keyboard combo, have a small window pop up where I type what I want to capture, hit OK and the automation goes and appends it to the right .txt file, in the right location, behind the scenes.

I will be writing about my plain-text journaling system in a future post. For now, read on to learn how to set this journaling automation up for yourself.

 The shell script code, and what it does

The setup of this Automator Service is exactly the same as my append text Service, but the shell script code is different:

echo -e "- $FILE_CONTENT" | cat - /Users/George/Dropbox/Notes/todo.txt > /tmp/tempfile && mv /tmp/tempfile /Users/George/Dropbox/Notes/todo.txt

This shell script would take the text on your Clipboard, and prepend it to the todo.txt file residing in your /Dropbox/Notes/ folder.


If the todo.txt file initially looked like this:

- Shave cat
- Write essay on the history of forks
- Take out trash

After running that Automator Service, and entering the new task

- Lift heavy things

the todo.txt file would then look like this:

- Lift heavy things
- Shave cat
- Write essay on the history of forks
- Take out trash


Please be aware that I am not a coder or programmer, and I may have done something wrong by duct-taping this code together! This shell script works by saving the existing file, then overwriting it after inserting the new text to the existing content.

If something goes wrong, you could lose your file and/or content.

I used the code found on this page to put this together for my needs. I’d suggest reviewing it and/or asking someone who knows what they are doing if this is set up properly.

It seems to work fine on this end, but unlike the append text script, which only edits the text file, this prepend script overwrites your original file. Which seems kind of insane, and from what I’ve read it’s unavoidable. Which boggles my mind. But that’s how it is. Use this at your own risk!

 Add A Keyboard Shortcut

Once saved as a macOS Service, you can then go into the Shortcuts tab of the Keyboard section of the macOS System Preferences and add a keyboard shortcut of your choice to invoke this Service and prepend a text file.

macos keyboard shortcuts prefences

As mentioned in the earlier post about appending text, you could also tweak the setup to invoke this via Text Expander, and even further use a pop-up or fill-in snippet to prepend any other file of your choice on the fly.

Update 1: bring the text window to the front

To get the text box which pops up to be the front-most window so you can immediately start typing into it, add this AppleScript step to the start of the Automator Service workflow:

Screen Shot 2018-09-03 at 4.59.58 PM

Update 2: hit the OK button via keyboard

I also figured out how to hit the OK button via the macOS keyboard (since hitting the Return key in this text dialog box only enters a line break in your text): Function key + Return key.

Bonus: prepend with separator

If you want to customize this even further (which is how I have mine set up), you could add a separator between entries.  To do so you need to format your shell script like this:

FILE_CONTENT=$(pbpaste) echo -e "- $FILE_CONTENT\n___" | cat - /Users/George/Dropbox/Notes/todo.txt > /tmp/tempfile && mv /tmp/tempfile /Users/George/Dropbox/Notes/todo.txt

Note the \n after the $FILE_CONTENT variable. That is a “new line” instruction. I am not sure why, but the -e has to be included for the \n to work.

The \n is followed by three underscores:


which I currently use as my Markdown-friendly horizontal plain-text divider.

This would give you a list as such:

- Lift heavy things
- Shave cat
- Write essay on the history of forks
- Take out trash

That should give you a good reference as far as how to format to your liking. See the append text post for examples on including dynamic date & time to these entries.

I’ve even discovered you can use these shell script variables (including dynamic date and time) to refer to file names, which adds a whole new dimension to all of this.

In other words, you can append files named for example

journal 2017-12-30.txt

where the date portion is dynamically generated when you run the script.


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.

One thought on “Prepend Text File with Automator & Shell Scripts

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