You can make a difference in the Apple Support Community!

When you sign up with your Apple Account, you can provide valuable feedback to other community members by upvoting helpful replies and User Tips.

Looks like no one’s replied in a while. To start the conversation again, simply ask a new question.

I can't seem to get Automator to search for labeled folders inside another folder

Does anyone know how to get Automator to simply search for all the labeled folders inside a certain folder? The results that come back are only files that I've tagged, e.g .txt etc - Not folders.

Posted on Jan 20, 2020 3:57 AM

Reply
Question marked as Top-ranking reply

Posted on Jan 21, 2020 8:25 AM

Lose the Allow multiple selections, or put mdfind in a for loop. It won't work as you have it now. Also, my bad for mispelling the kMDItemUserTags property. Personally, I would stick with one starting folder selection, omit the for loop, and let mdfind go recursive in the single, specified folder.


for f in "$@"
do
  mdfind -onlyin "$f" -name \( 'kMDItemKind == "Folder" && kMDItemUserTags == "*"' \)
done


10 replies
Question marked as Top-ranking reply

Jan 21, 2020 8:25 AM in response to eddiejervis

Lose the Allow multiple selections, or put mdfind in a for loop. It won't work as you have it now. Also, my bad for mispelling the kMDItemUserTags property. Personally, I would stick with one starting folder selection, omit the for loop, and let mdfind go recursive in the single, specified folder.


for f in "$@"
do
  mdfind -onlyin "$f" -name \( 'kMDItemKind == "Folder" && kMDItemUserTags == "*"' \)
done


Jan 20, 2020 8:17 AM in response to eddiejervis

The Automator Files & Folders library has a Find Finder items action in it. The following is that action configuration that says find every folder on my Desktop and whose label (aka tag) is Blue:


Of course, it reports success, but nothing is found, even though there is a conspicuous Blue tagged folder on my Desktop. By holding down the ⌘ key, you can also select multiple label colors too, or change the Label is not X in hopes that it will find any tagged folder. Simply does not work. In all cases, a green success is displayed without results. So this action is worthless.


Note that this is only searching for default Finder label names (e.g. Red, Green, Blue, etc.) and not custom named Finder tags.


However, using a Run Shell Script action, one can gain better control of finding all tagged folders in a directed hierarchy by using the Ask for Finder Items to pass along the starting folder location, and finding every tagged folder within it. I have used Desktop in the following example, but it could be your home directory, or any other folder starting point:



If my starting point is my home directory, and I select Desktop, then this workflow finds the Blue-tagged folder on the Desktop, and its Red-tagged folder content — and nothing else. These selections would be passed out of the Run Shell Script for additional processing.

Jan 21, 2020 1:36 AM in response to eddiejervis

Are you suggesting that even the Run Shell Script solution did not work for you?


You need to have Folders selected in System Preferences : Spotlight : Search Results categories. The Run Shell Script solution does work in finding folders that have a tag on them, including sub-folders. Tested it on Mojave 10.14.6, so I know it works.


There is also a way in AppleScript to do this with the standard collection of Finder labels (tags), or with custom, named tags, though the latter are not supported via standard Finder scripting dictionary.

Jan 21, 2020 9:26 AM in response to VikingOSX

Thank you so much, this has worked! Could I ask one more favour; could you breakdown the Shell Script? As I see it;


for f in "$@" (Is the Variable)

do

mdfind (<Is the folder finder?) -onlyin "$f" (<In the folder previously supplied) -name \( 'kMDItemKind == "Folder" && kMDItemUserTags == "*"' (<Is this the colour of the Tag?) \)


done


Thank you for your patience!

Jan 21, 2020 10:04 AM in response to eddiejervis

The bash for-loop construction expects one or more objects from the command-line, or in this automator example, the folder passed into the Run Shell Script via the as arguments setting. The $@ variable is a list of those passed in objects, and in this case, just the folder path. Each pass of the for-loop updates the variable f with an object from the "$@" list.


The mdfind(1) application works off of the Spotlight database, and has no syntax commonality with the BSD find(1) command other than having recursive behavior when working on a folder path. Let's say the full path of the folder passed to the Run Shell Script is:


/Users/yourname/Desktop/Dest


That becomes the f variable in the for-loop as it is the only item in "$@".


Then, the mdfind command would perform the following:

mdfind -onlyin "/Users/yourname/Desktop/Dest" -name \( 'kMDItemKind == "Folder" && kMDItemUserTags == "*"' \)


Any folder found in the above path that had any tag on it would be found by the Run Shell Script. I use the -onlyin argument to restrict where mdfind searches. If you removed it, and supplied /Users/yourname as the f variable path argument, then mdfind would search your entire home directory including its Library hierarchy. That would get out of control.


That wildcard used with kMDItemUserTags is asking for match any tagged folder regardless of the color. If you wanted to return all folder's with a green tag. The lowercase 'c' following the tag name means case-insensitive to match Green or green. Shouldn't need it, but there for failsafe.


mdfind -onlyin "$f" -name \( 'kMDItemKind == "Folder" && kMDItemUserTags == "Green"c' \)


I can't seem to get Automator to search for labeled folders inside another folder

Welcome to Apple Support Community
A forum where Apple customers help each other with their products. Get started with your Apple Account.