MacOS – How to tag files (with Finder tags) using parts of a file name

finder-tagmacos

I want to use parts of a file name to add specific Finder tags to a file. How can I do that, are there any scripts or applications available to automate this on OS X Mavericks? The only solution I can think of right now is Hazel, but this would be limited by the rule character of this application (choosing tags manually).

Best Answer

orig=/path/to/tagged-file
folder=/path/to/folder
string=*abc*
find "${folder}" -name "${string}" \
                 -exec xattr -wx com.apple.metadata:_kMDItemUserTags \
                       "$(xattr -px com.apple.metadata:_kMDItemUserTags '${orig}')" \
                       {} \;
  1. Create a file with the tags that you wish to apply to a certain set of files that will match a search string. In the script above, this file is located at /path/to/tagged-file and is set on the first line of the script to the orig variable. Replace this with the path to the file that you've created.

  2. On the second line of the script, change the path to the path of the containing folder that you wish to search through. This will be set to a variable called folder.

  3. On the third line, set the string to search for in the name of the file. *abc* will match 123abc123.txt.

  4. The fourth line and beyond is actually a single line that runs the find command to find the matching files then xattr to copy the tags from that original file to every file found.