chauncey-garrett
11/5/2013 - 8:19 PM

listtags.bash

# Updated 2013-10-31 to handle:
# listing all files with tags
# list only files without tags
# and deal with spaces in filenames (that part got ugly).

# Also, set the query variable to local

# List files with specified Finder tags in current directory and subdirectories
# Works with partial words starting from the beginning of the word
lst() {
	local query
	# if the first argument is "all" (case insensitive),
	# a boolean AND search will be used. Defaults to OR.
	bool="OR"
	[[ $1 =~ "all" ]] && bool="AND" && shift

	# if there's no argument or the argument is "+"
	# list all files with any tags
	if [[ -z $1 || $1 == "+" ]]; then
		query="kMDItemUserTags == '*'"
	# if the first argument is "-"
	# list only files without tags
	elif [[ $1 == "-" ]]; then
		query="kMDItemUserTags != '*'"
	# Otherwise, build a Spotlight syntax query string
	else
		query="tag:$1"
		shift
		for tag in $@; do
			query="$query $bool tag:$tag"
		done
	fi

	while IFS= read -r -d $'\0' line; do
		echo ${line#`pwd`/}
	done < <(mdfind -onlyin . -0 "$query")
}