I’ve been using Hazel to run plenty of macOS automations.
My need
I often find macOS search faster than Obsidian search, and consequently I wanted a way search my .md files by obsidian tags.
Solution
I wrote a bash script that scans my markdown files and automatically applies the obsidian tags to macOS Finder tags This helps me keep things organised using native tags without manually assigning them.
Limitations
- This script will only add tags to the file, it will not remove tags as you change them within Obsidian
- Sync is unidirectional Obsidian to macOS. Adding tags in macOS finder will not apply those tags to the markdown file.
Requirements
- Hazel app
- homebrew with yq and tags packages installed
Set up
Have hazel watch your obsidian vault folder. Can either execute on file modified, or in my case on files that have no tags.
Then have hazel run the bash script:
#!/bin/bash
# Input: file path
FILE="$1"
# Check that it's a Markdown file (.md or .markdown), case-insensitive
shopt -s nocasematch
if [[ ! -f "$FILE" || ! "$FILE" =~ \.(md|markdown)$ ]]; then
exit 0
fi
shopt -u nocasematch
# Extract YAML front matter into a temporary file
YAML_TEMP=$(mktemp)
trap 'rm -f "$YAML_TEMP"' EXIT # ensure temp file is deleted on exit
awk '/^---$/ {count++} count==1 {print} count==2 {print; exit}' "$FILE" > "$YAML_TEMP"
# Extract tags as a comma-separated list
TAGS=$(yq eval '.tags | join(",")' "$YAML_TEMP" 2>/dev/null)
# Exit if no tags found
if [ -z "$TAGS" ]; then
exit 0
fi
# Apply tags using the `tag` utility
tag --add "$TAGS" "$FILE"
exit 0
Have fun!