Skip to content

Instantly share code, notes, and snippets.

@ttscoff
Created June 12, 2014 14:22
Show Gist options
  • Star 6 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ttscoff/c96b6c454be6937e5c7e to your computer and use it in GitHub Desktop.
Save ttscoff/c96b6c454be6937e5c7e to your computer and use it in GitHub Desktop.
cdt function to change directory based on a tag filing system
# In my tag-filing system where top-level "context" folders are tagged with
# "=Context" tags and subfolders are tagged with "@project" tags, this function
# lets me quickly cd to any tagged folder in my filing system. The first
# argument is a context folder, the rest are a chain of subfolders. The
# subfolders don't need to be contiguous, and the matching is fuzzy.
cdt() {
local fuzzy
local root
local sub
local list
if [[ $# == 0 || $1 =~ ^\-?\-h(elp)?$ ]]; then
cat <<-EOS
$FUNCNAME changes directory based on tagged folders
usage: $FUNCNAME context [subfolder, [subfolder...]]
EOS
return
fi
fuzzy=`echo ${1##[#=]} | sed 's/\([[:alpha:]]\)/\1*/g'`
root=`mdfind "kMDItemUserTags = '=*$fuzzy'c && kMDItemContentType = 'public.folder'"|head -n 1`
if [[ $2 ]]; then
shift
while [[ $1 ]]; do
fuzzy=`echo $1 | tr -d "@: " | sed 's/\([[:alpha:]]\)/\1*/g'`
sub=`mdfind -onlyin "$root" "kMDItemUserTags = '@$fuzzy'c && kMDItemContentType = 'public.folder'"|head -n 1`
list=`ls -1F | grep -i "${fuzzy//\*/.*}.*\/$" | head -n 1`
if [[ -n $sub ]]; then
root=$sub
elif [[ -d $root/$list ]]; then
root=$list
fi
shift
done
fi
builtin cd "$root"
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment