Help with using a shell script to find all files with certain tags

Hi,

I have the following shell script in Keyboard Maestro:

read -r -d '' shCMD <<EOT
	mdfind -onlyin "$KMVAR_rcPath" "kMDItemUserTags == '*$KMVAR_Tag*'" | sort -d
EOT

eval "$shCMD";

I know hardly anything about using shell scripts, this is something I found from somewhere.

It references a folder to search in (rcPath) and the tag to look for (Tag).

With most tags it works fine, returning a list of files. However, certain tags it has trouble with, which seem to be tags with quotes and other unusual characters in them.

Does anyone know how to modify this script so that it would work for ALL tags, including those that have funny characters?

Many thanks!

Andy

Hey Andy,

You need to learn a bit about escaping quotes in the shell. (It can be finicky and complicated depending upon what you're doing.)

Google Search Link

Get rid of the asterisks in your mdfind line:

mdfind -onlyin "$KMVAR_rcPath" "kMDItemUserTags == '$KMVAR_Tag'" | sort -d

Unless you really want to use wildcards.

Escape both single and double quotes inside your tag name:

Escaping Quotes in Tag for mdfind.kmmacros (3.0 KB)

-Chris

2 Likes

Thanks Chris, that is brilliant! Works perfectly.

Best wishes,

Andy

1 Like