Show your last 3 visited code projects as a Finder sidebar folder, updated automatically by zsh.
There's no reliable public API for programmatically adding items to the macOS Finder sidebar Favorites. The old LSSharedFileList API was deprecated in macOS 10.11 and removed in 10.13. Tools like mysides no longer work on modern macOS (Sequoia).
I keep all my code projects in ~/code. I frequently want to drag files to a recently used project (I work mostly in the terminal). This shows a work-around to keep the Finder Favorites sidebar updated with the last 3 code projects I've worked on.
Instead of fighting Finder's internals, this uses a simple workaround:
- A hidden folder (
~/.Recent Projects/) contains numbered symlinks pointing to your most recently visited project directories - A zsh
chpwdhook automatically updates the symlinks whenever youcdinto a subdirectory of~/code - You drag the
~/.Recent Projectsfolder to your Finder sidebar once — after that, it stays current automatically
The result looks like this in Finder:
.Recent Projects/
1 applescripts → ~/code/applescripts
2 osxphotos → ~/code/osxphotos
3 aoc2024 → ~/code/aoc2024
mkdir -p "$HOME/.Recent Projects"# Recent Projects — keep Finder sidebar folder with last 3 ~/code subdirs
RECENT_PROJECTS_DIR="$HOME/.Recent Projects"
RECENT_PROJECTS_HISTORY="$RECENT_PROJECTS_DIR/.history"
_update_recent_projects() {
local current="$PWD"
local code_dir="$HOME/code"
# Only track direct children of ~/code
[[ "$current" == "$code_dir"/* ]] || return
local project="${current#$code_dir/}"
project="${project%%/*}"
[[ -n "$project" && -d "$code_dir/$project" ]] || return
# Update history: remove existing entry, prepend new one, keep 3
touch "$RECENT_PROJECTS_HISTORY"
local tmp
tmp=$(grep -vx "$project" "$RECENT_PROJECTS_HISTORY" 2>/dev/null | head -2)
{ echo "$project"; [[ -n "$tmp" ]] && echo "$tmp"; } > "$RECENT_PROJECTS_HISTORY"
# Remove old symlinks, recreate numbered ones
for lnk in "$RECENT_PROJECTS_DIR"/[1-3]\ *; do
[[ -L "$lnk" ]] && command rm -f "$lnk"
done
local i=1
while IFS= read -r proj; do
[[ -n "$proj" ]] || continue
ln -sf "$code_dir/$proj" "$RECENT_PROJECTS_DIR/$i $proj"
((i++))
[[ $i -gt 3 ]] && break
done < "$RECENT_PROJECTS_HISTORY"
}
autoload -Uz add-zsh-hook
add-zsh-hook chpwd _update_recent_projects- Open Finder
- Press
Cmd+Shift+Gand type~/.Recent Projects - Drag the
.Recent Projectsfolder proxy icon to the Favorites section of your sidebar
chpwdhook: zsh calls_update_recent_projectsevery time the working directory changes — whether viacd,pushd,popd, or any other mechanism.- Project detection: The function checks if the current directory is under
~/code/. If so, it extracts just the top-level project name (e.g.,cd ~/code/osxphotos/teststracksosxphotos). - History file:
~/.Recent Projects/.historystores project names in most-recent-first order. On each visit, the current project is moved to the top and the list is trimmed to 3. - Numbered symlinks: After updating history, old symlinks (
1 ...,2 ...,3 ...) are removed and recreated. The numbering keeps the most recent project visually sorted first in Finder.
- Change the tracked directory: Replace
$HOME/codein thecode_dirvariable with any parent directory you want to track. - Change the number of projects: Replace
head -2withhead -N-1and the[1-3]glob with[1-N]where N is the desired count. - Change the folder location: Update
RECENT_PROJECTS_DIRto wherever you'd like the symlinks to live.
- macOS (tested on Sequoia 15)
- zsh (default shell on macOS)