Welcome to my personal site. In addition to the posts you see below, feel free to explore other places where I stash content here. The now page is an occasionally-updated page about long-term plans and activities. I try to post several times a week and those updates appear on the journal page. Sometimes I post tidbits about things I learn in the til page. More info in: toolkit, readings, to learn, gallery, colophon, and changelog.


As a dual Canadian/U.S. citizen 🇨🇦 🇺🇸, I am opposed to the deeply corrupt and incompetent Administration of the U.S. government and urge Americans to vote all Republicans out of office.

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.

My LLM prompt for chat summaries

When actively coding, I’m typically interacting with LLMs via agents. But sometimes I will turn to a chat interface to explore an issue from a bigger perspective or to solve an issue where I want the LLM to explain and advise rather than have it take over. These chats can become lengthy and involve digressions and side branches to solve sub-issues that may not be strictly relevant to the overall goal. There may be facts, procedures and other information that I want to recall from this conversation; but not with all the side-branches that you would find if I just saved a link to the conversation. Even a straight export of the conversation would capture many irrelevant details.

So at the end of a long conversation, I ask the LLM to summarize the conversation so that I can save it in Obsidian. Here is my current prompt for this:

- Reorganize the material into a logical implementation
  sequence rather than preserving the conversation's chronology
  exactly.
- Document the final working solution, including commands,
  scripts, configuration files, explanations, and verification
  procedures.
- Include significant troubleshooting discoveries and explain why
  particular approaches failed. Exclude irrelevant diversions and
  superseded intermediate solutions.
- Distinguish between configurations that were actually tested and
  verified, recommendations that were not implemented, and
  unresolved issues.
- Preserve the exact content of final working scripts and
  configurations wherever available. Do not fabricate missing
  details.
- Include YAML frontmatter, descriptive headings, appropriately
  labelled code blocks, and relevant references.
- Do not use headings larger than ## (<h2>). In other words, don't
  create a title heading. Go straight into the content because I
  will supply a title later. Hierarchical headings are encouraged.
  Do not number headings though. Do not use bold in headings.
- If a code block consists of exactly one line, convert it to an
  inline code block. This saves space.
- Where possible convert ASCII art diagrams into D2 charts and make
  use of ```d2 ... ``` in the Markdown because we will often use the
  Markdown directly in Obsidian for use with its D2 plugin. If no
  diagrams are needed, do not fabricate one. It's OK not to have any
  diagrams at all.
- Try to avoid writing one-sentence paragraphs. Prefer clean
  narrative style over telegraphic writing.
- Produce a downloadable Markdown file.

If you have suggestions for improving this prompt, or have questions or comments, please get in touch through my contact page.