A script to add macOS tags and file comments in Yazi
I have a love/hate relationship with the macOS Finder, so I often work in yazi homepage | GitHub. But one of the things that the Finder does do acceptably is to set tags and add Finder comments. I make extensive use of both of these in order to find files and directories more easily. However, yazi doesn’t have exactly the metadata editor that I was looking for. So I wrote one.
File comment metadata on macOS
First, a brief digression on file metadata in macOS - specifically file comment metadata. There are at least two types of file comment metadata available for filesystem entities in macOS - the so-called “Finder comments” also known as “Spotlight comments” which are available in Finder’s “Get Info…” dialog. They are quick, easy, and familiar to most macOS users. They are also very fragile because they are stored in two different places. The primary storage is in a .DS_Store file in the enclosing directory. A second copy is stored in an xattr (extended attribute) com.apple.metadata:kMDItemFinderComment for the file. In computer science, it’s rarely a good idea to have two sources of truth, and sure enough this is where Finder comments go off the rails. If you enter a comment in Finder’s Get Info, then edit the comment with xattr, Finder will never see the change. The Get Info version just shows whatever was initially entered. Worse, since it is stored separately from the file, and .DS_Store files are often excluded from cross-platform sync, the comment disappears.
There is a second, more portable form of comment com.apple.metadata:kMDItemComment which doesn’t appear in Finder’s Get Info dialog but which is only stored with the file, not in the .DS_Store file. And this metadata is also available to Spotlight. Howard Oakley has a good writeup on Finder comments and their fragility.
Yazi metadata script
With that digression out of the way, I’ll explain what the script is about. The idea is to highlight a file in yazi, press “T,” and it will run a script that allows you to add and remove tags and add a kMDItemComment. There is also an option to copy the comment to kMDItemFinderComment. The script uses AppleScript to set the latter so that it is visible in the Finder, if that’s your cup of tea.
Prerequisites
The script requires fzf to offer existing tags, the tag command to manage tags, and Python 3 to write comments. Install them with Homebrew:
brew install fzf tag python
The script
The script should be saved as ~/.config/yazi/scripts/tagcomment.sh:
#!/usr/bin/env bash
exec </dev/tty >/dev/tty 2>&1
[ $# -eq 0 ] && exit 0
XA_TAGS="com.apple.metadata:_kMDItemUserTags"
XA_COMMENT="com.apple.metadata:kMDItemComment"
XA_FINDER="com.apple.metadata:kMDItemFinderComment"
set_comment_xattr() { # $1=path $2=text ("" clears)
if [ -z "$2" ]; then
xattr -d "$XA_COMMENT" "$1" 2>/dev/null
else
local hex
hex=$(python3 -c 'import plistlib,sys; print(plistlib.dumps(sys.argv[1], fmt=plistlib.FMT_BINARY).hex())' "$2")
xattr -w -x "$XA_COMMENT" "$hex" "$1"
fi
}
set_finder_comment() { # $1=path $2=text ("" clears)
osascript - "$(realpath "$1")" "$2" <<'EOF'
on run {p, c}
tell application "Finder" to set comment of (POSIX file p as alias) to c
end run
EOF
}
echo "Targets:"; printf ' %s\n' "$@"
echo
current=$(tag -l -N "$@" | tr ',' '\n' | sed '/^[[:space:]]*$/d' | sort -u)
all_tags=$(tag --usage | sed -E 's/^[[:space:]]*[0-9]+[[:space:]]+//' | sort -u)
echo "Current tags: $(paste -sd, - <<<"$current")"
echo "Current kMDItemComment: $(xattr -px "$XA_COMMENT" "$1" 2>/dev/null | xxd -r -p | plutil -p - 2>/dev/null)"
echo "Current Finder comment: $(mdls -raw -name kMDItemFinderComment "$1" | sed 's/(null)//')"
echo
# --- tags: add ---
picked=$(printf '%s\n' "$all_tags" | fzf --multi --height=40% --reverse \
--bind 'tab:toggle+clear-query' \
--prompt='add tag > ' --header='Tab: select several | Enter: confirm | Esc: skip') || picked=""
read -r -p "New tags (comma-separated, blank to skip): " new
add=$(printf '%s\n%s\n' "$picked" "${new//,/$'\n'}" \
| sed 's/^[[:space:]]*//;s/[[:space:]]*$//;/^$/d' | paste -sd, -)
# --- tags: remove ---
rem=""
if [ -n "$current" ]; then
rem=$(printf '%s\n' "$current" | fzf --multi --height=40% --reverse \
--bind 'tab:toggle+clear-query' \
--prompt='remove tag > ' --header='Tab: select several | Enter: confirm | Esc: skip') || rem=""
rem=$(paste -sd, - <<<"$rem")
fi
# --- comment: kMDItemComment first, then optionally Finder ---
change_comment=0; write_finder=0
read -r -p "Comment (blank = keep, '-' = clear): " comment
if [ -n "$comment" ]; then
change_comment=1
[ "$comment" = "-" ] && comment=""
read -r -n1 -p "Also write it to the Finder comment (shown in Get Info)? [Y/n] " yn; echo
[[ ! $yn =~ ^[Nn]$ ]] && write_finder=1
fi
# --- summary ---
if [ -z "$add" ] && [ -z "$rem" ] && [ $change_comment -eq 0 ]; then
echo "Nothing to change."; read -r -p "Press enter"; exit 0
fi
[ -z "$comment" ] && shown="(clear)" || shown="\"$comment\""
echo
echo "About to write to $# item(s):"
if [ -n "$add" ] || [ -n "$rem" ]; then
echo " Finder tags $XA_TAGS"
[ -n "$add" ] && echo " add: $add"
[ -n "$rem" ] && echo " remove: $rem"
fi
if [ $change_comment -eq 1 ]; then
echo " Comment $XA_COMMENT"
echo " $shown"
if [ $write_finder -eq 1 ]; then
echo " Finder comment $XA_FINDER"
echo " $shown"
fi
fi
echo
read -r -n1 -p "Apply? [Y/n] " ok; echo
[[ $ok =~ ^[Nn]$ ]] && { echo "Cancelled."; exit 0; }
# --- apply ---
[ -n "$add" ] && tag -a "$add" "$@"
[ -n "$rem" ] && tag -r "$rem" "$@"
if [ $change_comment -eq 1 ]; then
for f in "$@"; do
set_comment_xattr "$f" "$comment"
[ $write_finder -eq 1 ] && set_finder_comment "$f" "$comment"
done
fi
echo "Done."
read -r -p "Press enter"
exit 0
Add keybinding
Add a yazi keybinding at ~/.config/yazi/keymap.toml:
[[mgr.prepend_keymap]]
on = "T"
run = 'shell "~/.config/yazi/scripts/tagcomment.sh \"$@\"" --block'
desc = "Edit Finder tags and comment"
Restart yazi and you should be good to go. Just highlight a file and press “T” to edit metadata.