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.
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:
FILE_CONTENT=$(pbpaste) 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
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
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.
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.
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
\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.
\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
where the date portion is dynamically generated when you run the script.