0

Is anyone aware of a way to automatically apply a given Finder tag to a folder based on its contents?

For example, I can easily run a Finder search for files based on their "Document Container". But it seems there is no way to get containers based on files they contain. What I'm trying to do is to apply tag "X" to all folders that contain one or more files with file extension "Y". It seems there's no way to do it directly from Finder but I haven't been lucky with Automator either as I can't seem to find a way to select a range of folders based on what they contain.

4
  • Are you looking to apply custom tags or just default tags of “Red,Green,Blue, etc.” ? Can you give an example of an exact tag name and a specific file extension/s? I may have a relatively simple solution for you. I actually created a folder action for myself which automatically adds tags to the files added to that folder depending on their extensions.. Basically…(if name extension is this… add this tag, if name extension is that… add that tag…). I can easily tweak my code to suit your needs
    – wch1zpink
    Commented Jan 30, 2021 at 2:29
  • @wch1zpink That'd be awesome! Yes, what you write sounds exactly what I was envisioning. Essentially, I have an archive of video and audio files and they are being transcribed. Each folder in the archive has either one audio file or audio plus video (plus some other files that are irrelevant). Now as I'm adding transcription files (with extension .eaf) I'd love the folder get a red Finder tag, telling me it's been transcribed.
    – jan
    Commented Jan 30, 2021 at 18:15
  • @wch1zpink So essentially, what I'd need is something like "for each file, if its name extension is eaf then add this tag to the folder in which it sits" (not to the file itself) – if you have a script that does something like that I'd be very grateful.
    – jan
    Commented Jan 30, 2021 at 18:17
  • Let me know how you make out with the solution I provided in my answer to your post.
    – wch1zpink
    Commented Feb 1, 2021 at 22:15

2 Answers 2

2

The proper tool for this job is Hazel.

It can apply tags based on file names, file extensions, even file contents and a lot more. It can also do that on sub-folders as well.

The app has been around for a long time, its developer is very well respected, and the app is well worth its asking price.

There’s a free trial available which you should download and try before buying, but it’s an essential tool for me, and much easier to deal with than Automator and Folder Actions.

Updated To Add

I’m 99% sure that Hazel can do this (if I am understanding the idea properly), and would recommend checking out this page as a starting point:

https://www.noodlesoft.com/manual/hazel/advanced-topics/processing-subfolders/

The developer is also very responsive. The best place to ask questions is here:

https://www.noodlesoft.com/forums/

4
  • 2
    How would a Hazel rule look like which tags folders as described in the question?
    – nohillside
    Commented Sep 15, 2020 at 5:14
  • I’m actually using Big Sur and Hazel isn’t fully compatible yet, so I can’t say precisely, but I I believe that you would want to point Hazel at the parent folder, setup the rules, and then have it set to work on sub-folders. The instructions that I would start with are noodlesoft.com/manual/hazel/advanced-topics/…
    – TJ Luoma
    Commented Sep 16, 2020 at 3:52
  • Thank you, I have downloaded Hazel, and read up on how to process subfolders, but I still can't seem to find out how to test for the existence of files contained in a folder. I've posted on their forum now and will post back here to see if I get any help there.
    – jan
    Commented Sep 16, 2020 at 18:29
  • In the meanwhile, if anyone knows of an alternative solution I'd still be curious :)
    – jan
    Commented Sep 16, 2020 at 18:30
0

In Script Editor.app, paste this following AppleScript code into a new document then name and save it as a “scpt” file to /Users/YOUR_SHORT_NAME/Library/Workflows/Applications/Folder Actions folder.

on adding folder items to theFolder after receiving theNewItems
    --  Called after items have been added to a folder
    --  theFolder is a reference to the modified folder
    --  theNewItems is a list of references to the items added to the folder 
    repeat with i from 1 to count of theNewItems
        try
            set thisFile to item i of theNewItems 
            tell application "Finder"
                if name extension of thisFile is "eaf" then
                    set label index of theFolder to 2 -- 2 = Red
                end if
            end tell
        end try
    end repeat
end adding folder items to

(* Anytime Files Are Removed From The Folder That
This Folder Action Is Attached, If None Of The Remaining Files
(If Any) Do Not Have The Name Extension "eaf", The Red Label
Tag Will Be Removed From The Containing Folder *)

on removing folder items from theFolder after losing removedItemNames
    --  Called after items have been removed from a folder
    --  theFolder is a reference to the modified folder
    --  removedItemNames is a list of names of items removed from the folder
    delay 3 -- A Little Buffer If Removed Files Are Large Or Alot
    tell application "Finder"
        set fileRef to a reference to (files of theFolder whose name extension is "eaf")
        if contents of fileRef is {} then
            set label index of theFolder to 0 
        end if
    end tell
end removing folder items from

After your file is saved to that location, you can attach that Folder Action to any folder of your choice by ctrl + click the folder in Finder.app then select “Services” then “Folder Actions Setup…”. From there it’s pretty much self-explanatory. Just attach your new folder actions script to the folder and you should be good to go.

Now anytime a file with the extension “eaf” is added to that folder, the “Red” Finder tag will be added to the folder. On the flip-side, I also added the functionality to monitor the folder when items are removed that if none of the remaining files have the name extension “eaf”, the “Red” Finder tag will be removed from the folder.


If you already have a bunch of “eaf” files scattered all over your computer within different folders and you would like to add the “Red” Finder label tag to the containing Folders of those “eaf” files then… In Script Editor.app, paste this following AppleScript code into a new document and run the script.

set containingFolders to {}

set theFiles to paragraphs of ¬
    (do shell script "find ~ -type f -iname '*.eaf'") as list
    
tell application "Finder"
    repeat with i from 1 to count of theFiles
        set thisItem to item i of theFiles
        set aContainer to container of ¬
            (thisItem as POSIX file as alias) as alias
        if aContainer is not in containingFolders then ¬
            set end of containingFolders to aContainer
    end repeat
    repeat with thisContainer in containingFolders
        set label index of thisContainer to 2
    end repeat
end tell
5
  • Keep in mind, this code will only monitor the folder you attach it to. The code will need to be adjusted if you intend on adding sub folders. For example if you want to monitor for files with the extension “eaf” of any sub folders which get added to your main folder, we can add functionality to the code to automatically attach the folder action to any folders added to your main folder. This in turn will monitor the sub folders for the files with the extension “eaf” as well.
    – wch1zpink
    Commented Jan 31, 2021 at 21:16
  • You could eliminate set thisItem to item i of theFiles and use repeat with thisItem in theFiles instead of repeat with i from 1 to count of theFiles as there does not seem to be an explicit need to get a count, as you have used if theFiles is "" then return prior to the repeat loop. Commented Feb 2, 2021 at 14:44
  • @user3439894 In your honest opinion, is there any disadvantage to just removing if theFiles is "" then return and leaving my repeat loop the way that it is now? Is it better coding etiquette to avoid using repeat with i from 1 to count of.. and use repeat with i in list instead?
    – wch1zpink
    Commented Feb 2, 2021 at 20:53
  • Also, forgot to mention that as list is not necessary, as set variable to paragraphs of makes variable a list If you remove as list and run the set variable to paragraphs of line with return class of variable after it, you'll see it returns list. As to the repeat loop as is, it's unnecessary coding analogous to the needles use of cat in e.g. cat file | command when command file works without cat in a shell. When I first learned to repeat through a list I learned it the way you are using it. I try to only use it that way as necessary, which it this case it is not. Commented Feb 2, 2021 at 22:13
  • There are times it's appropriate/necessary, but is really necessary here. Commented Feb 2, 2021 at 22:13

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .