Take the 2-minute tour ×
Ask Different is a question and answer site for power users of Apple hardware and software. It's 100% free, no registration required.

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).

share|improve this question

3 Answers 3

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.

share|improve this answer
    
Thank you for your answer George. Do you mind expanding it a bit, I don’t get it. Thank you. –  lejonet May 18 at 17:56
1  
@lejonet Edited—hope that helps –  grgarside May 18 at 18:02

There's a command line tool called Tag (https://github.com/jdberry/tag) which allows you to specify file patterns.

share|improve this answer

Yes, you can use Automator to do this pretty easily.

  1. Open Automator: /Applications/Automator
  2. Select Application or Workflow (differences explained at bottom)
  3. Under Library > Files & Folders, drag Ask for Finder Items to the right where it says Drag actions or files here to build your workflow
  4. Select the check box "Allow Multiple Selection"
  5. Drag Filter Finder Items to the right under Ask for Finder Items
  6. Change Filter Finder Items options to choose items based on what you want
  7. Drag Label Finder Items to the right and place it third in the list
  8. Hit Run to execute the Workflow

There are other items in the Automator library you can pick to handle the selection of the files you want to process, but the 2nd and 3rd actions should stay the same for your needs.

Here's a screen-shot of my final Workflow: enter image description here

Workflow vs Application: A Workflow is basically a set of actions that can only be run from inside Automator. So you'd save that workflow and to run it again you'd open it in Automator and press Run.

An Application is exactly like a Workflow, but you can use File > Export to publish it as an Application which can be run just like any other app on your computer.

Hope this helps!

EDIT:

This is a link to a .DMG that contains two different Automator applications that will get you started hopefully. It includes documentation which you should read first.

Link to DMG containing custom/editable Automator apps

Keep in mind, these Workflows allow for user input because I didn't know exactly what you wanted to do. So they're probably not precisely what you want, but they should give you a good foundation to customize them for your needs.

share|improve this answer
    
This can't apply custom tags? –  grgarside May 18 at 18:34
    
This ok, but how to add custom tags now ? like text tags? –  Buscar웃 May 18 at 19:27
    
If you're talking about creating new Tags on the fly that will appear in your Finder's list of tags on the system then things get a bit more complicated. Can you give me a specific example of a scenario? Without knowing specifics there are two ways to handle custom tags. Either create custom tags in Finder ahead of time (which will show up in the Label Finder Items action above. Or you can add Spotlight Comments which are also searchable. For that you'd replace Label Finder Items with the Set Spotlight Comments action. –  sgelliott May 18 at 20:14

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.