MacOS – Using Tag how to cut and parse file paths into tags

command linefinderfinder-tagmacosscript

I'm using Tag and Hazel and I'd like to recursively go through files on our server, obtain their file paths and filenames and parse the whole thing into tags to apply to the file. Is there a shell script that can do this? I can use Leap by Ironic Software to remove the trash tags system wide, but this gives me a real start into an automatic tagging system for all my files.

Basically I'm going to take a filename like

/Volumes/media/Clientele/Stock/Purchased Sets/Vintage
Bundle/vintage-bundle-7/offset/30_CIRCULAR_VECTOR_TEXTURES/C03.eps

Truncate the /Volumes/media/Clientele/Stock/ off the top, and the file extension .eps from the end, then cut the rest into separate tags:

  • Purchased Sets
  • Vintage Bundle
  • vintage
  • bundle
  • 7
  • offset
  • 30
  • CIRCULAR
  • VECTOR
  • TEXTURES
  • C03

…remove leading and trailing spaces, change to lowercase, then reapply those tags back to the file with tag.

Additionally, removing tags from the system that are just numbers would be helpful also.

Update: I need to do this specifically:

Hazel handles the recursion so for every file it finds:

  • Store the $working_folder and the $filepath
  • cut the $working_folder from the $filepath
  • cut the $result at the _-. <space> , characters and store into a list or array
  • loop through the list or array and sed 's/^[ \t]*//;s/[ \t]*$//' to remove leading and trailing whitespace (from http://sed.sourceforge.net/sed1line.txt)
  • convert the array to a comma separated string to feed into tag -s $comma_separated_tags $filepath

I just don't know how to put that into a bash script format.

Best Answer

run 'find' from a root directory searching for plain files (-type f?) and -exec a bash script on each file (or perhaps automator).

Looks like you'll need a bit of 'basename' and 'sed'.

Append to an empty string for each input path, and treat that as a list for that file. I believe with 'tag' that you can pass a comma separated list, so you can append one (dealing with first having no comma) or post process using 'sed'. worst case loop over the list string and run 'tag' each time.

'tag' might be smart enough to append tags it doesn't already have what's passed, otherwise the top of the script might want to bail if the file already contains tags.

Not trivial to write, but if you're not familiar with bash, tag and sed it would be a good learning exercise.