Use Hazel to Add Tags to Files

From your noodle to other noodles. Talk about ways to get the most from Hazel. Even exchange recipes for the cool rules you've thought up. DO NOT POST YOUR QUESTIONS HERE.

Moderators: Mr_Noodle, Moderators

Use Hazel to Add Tags to Files Sat Aug 04, 2018 10:35 am • by biznachio
I love tagging files in MacOS. Tags allow us to add deep context to our files and make filtering files dead simple.

As much as I love tags, I loathe manually adding them. Fortunately Hazel's automation superpowers give us a way to apply tags programmatically.

I've pieced together a bash script that automatically adds tags to files. The script extracts words from a specific line in a text file to add as tags to a file. The script is customized to fit a larger paradigm, my knowledge and file workflow. I keep meticulous plain text notes to help me remember just about every aspect of my life. Each note includes the same front matter metadata.

Front Matter
Front matter contains extra information embedded directly within a file. You'll find a find several front matter formats: TOML, YAML, or JSON. I use YAML.

YAML uses a value:variable structure. YAML sits nice and tidy at the top of a text file. YAML front matter begins with three dashes (---), then any number of pre- or user-defined values and variables, and YAML ends with three more dashes (---).

It looks like this.

Code: Select all
Title: Cool Title
Date: 2018-08-04
Category: Writing
Tags: Hazel, Automation

There are tons of YAML variables and all sorts of services that use YAML (or any front matter format) in some way.

Most importantly, front matter is all text and resides in a plain text file. This singular fact lets us do all sorts of cool shit with the content inside the text file.

The Hazel rule and script I provide looks at a specific line in the front matter (the Tags: line/the 5th line), pulls the words out, and adds the words as tags to the file.

My Workflow, Briefly
I keep everything as text. I add front matter to every text file. Then I (used to) manually add the tags in MacOS.

Managing tags this way grew redundant. I wanted a way to extract the words in the Tags: line from the text file and automatically add them to that file. Hazel provided the way. Here's how to set it up for yourself.

How To

- Install Homebrew -
- Install tag command line tool -
- Hazel (obviously) -
- Some folder structure for your notes

The Hazel Rule
Plain and simple set up:

1. Give it a title. I named mine "Add YAML Tag(s)."
2. Scope the rule to run on modified files. You may have to add this after you've matched you files the first time if you have several notes.
- Date Last Modified is after Date Last Matched
3. Only match to files with the front matter value "Tags:"
- Contents contain Tags:
4. For matched files we want to Run a shell script
5. Click Edit script

The Script
Paste this bash script into the script popup.

Code: Select all
# look at the file's 5th line of text
# make an array of the words
# cut out the word Tags:
# translate , to new lines
arr=($(sed '5q;d' "$1" | cut -d " " -f2- | tr "," "\n"))

#echo ${arr[0]}

# loop through each array variable
# add a tag for each variable in the array
# adding tags require homebrew and tags command line tools

for i in ${arr[@]};
   tag -a $i "$1"

If you're curious how it works the script comments explain everything.

You can easily change what line the script looks at for tags. Updating the number (5) in the array assignment piece of code changes where the script looks. Keep in mind the very first line of text is one.

Code: Select all

I'm a novice script writer, not a professional. Unfortunately my lack of skills limits tags to only a single word. Multi-word tags will become two individual tags.

I'm sure my script is not as optimized as it could be. I pieced it together and it works so I let well-enough be.

Hopefully someone else will find use in what I've shared. Enjoy and happy file tagging!
Posts: 2
Joined: Sat Aug 04, 2018 6:42 am

Re: Use Hazel to Add Tags to Files Wed Aug 25, 2021 12:34 pm • by Dellu
Thank you.
This if very useful
Posts: 17
Joined: Thu Dec 12, 2013 6:26 am

Return to Tips & Tricks - DO NOT POST QUESTIONS