How to change the appearance of empty folders in finder using Automator?

7

2

I'm trying to use Apple Automator to label empty folders in a heirarchy. Basically, I start off a project with a heirarchy of empty folders and fill them as I work. I want a workflow to label all empty folders (gray) that contain no files and their descendants also contain no files.

In other words, when I create the empty project, all folders should be gray, as there are no "files" anywhere, just empty folders. However, once I place a file somewhere, it's parent and all of it's grandparents would no longer be labeled gray.

I thought I found what I needed at How to change the appearance of empty folders in finder? however, it is labeling empty folders. In my project, only the last descendants would ever be gray because all of the parent folders contain folders, therefore, not empty.

Here's the code I'm running in a /bin/bash shell script in Automator:

find "$@" -type d -empty

And then I'm passing it to a "Label finder items" to turn the results gray.

Here's the result I'm getting when run on a test folder:

  • Test Folder (not gray)
    • Folder One (gray)
    • Folder Two (not gray)
      • Folder A (gray)
    • Folder Three (not gray)
      • Test.txt
    • Folder Four (not gray)
      • Folder A (not gray)
        • Test.txt

And here's the result I'm looking for:

  • Test Folder (not gray)
    • Folder One (gray)
    • Folder Two (gray)
      • Folder A (gray)
    • Folder Three (not gray)
      • Test.txt
    • Folder Four (not gray)
      • Folder A (not gray)
        • Test.txt

Any help will be GREATLY appreciated!

Oh, also, I beleive I can run the workflow from Hazel. If you have a better way to have it automatically run occasionally, I'd love to know that as well.

Thanks.

Edit: Just realized that the .ds_store files (I assume) are causing folders to also not appear empty, if, for instance, I delete the last file in a folder.

Gary

Posted 2012-12-31T07:09:56.270

Reputation: 71

Answers

3

I tried a few cases, and seems it works as you need:

Shell script, usr/bin/perl, pass input as arguments.

automator service

use File::Find;

find({ wanted => \&find_files, no_chdir => 1 }, @ARGV);

sub find_files
{
    if (-f $_)
    {
        ($file) = $_ =~ /.*\/(.*)$/;
        push (@all_files, $_) if ($file ne ".DS_Store");
    }
    else
    {
        push (@all_folders, $_);
    }
}

sub osascript($) { system 'osascript', map { ('-e', $_) } split(/\r/, $_[0]); } 

foreach(@all_files)
{
    @paths = split(/\//, $_);
    for $i (0..$#paths-1)
    {
        if ($i == 0)
        {
            $x = "$paths[$i]";
        }
        else
        {
            $x = "$last[$i-1]/$paths[$i]";
        }
        push(@last, $x);
        $temp{$x} ++;
    }
    @last = ();
 }

@not_gray_folders = keys %temp;
foreach(@all_folders)
{
    $folder = $_; $flag = 0;
    foreach(@not_gray_folders)
    {
        if ($folder eq $_)
        {
            $flag = 1;
        }
    }
    if (!$flag)
    {
        $label = 7;
    }
    else
    {
        $label = 0;
    }
    &osascript ("tell application \"Finder\" to set label index of alias POSIX file \"$folder\" to $label");
}

ifau

Posted 2012-12-31T07:09:56.270

Reputation: 191

Works beautifully at labelling the empty folders. Can you also make it label the non empty folders with no label color? I assume, if you can set a variable that I can get and add a new Label Finder Items, that would do it. – Gary – 2012-12-31T19:13:07.910

Unless you have a cleaner way, I set a variable, then I used the first part of my code above to clear the labels, then I got the varialble and used your code to color the empty folders gray. Works. If I don't hear from you with a better fix, I'll check your answer as the accepted answer. Thanks again. – Gary – 2012-12-31T19:20:22.210

I edited answer. Glad to help you. – ifau – 2012-12-31T20:20:28.543

This works beautifully. Only problem now is that Hazel doesn't detect the changes deep within the heirarchy so it doesn't run automatically, like I suspected it would. When I run it manually, it does exactly what I asked for. Thanks. Now, I just need to figure out how to best automatically run it. – Gary – 2013-01-01T20:59:58.067

Thanks again for the above script. I still don't have a good way to automatically run it. Any thoughts? My preference would be that it runs on any change in any folder, but I realize that may be hard or impossible to make happen. As a backup, I'd take running it on a schedule. Thanks. – Gary – 2013-01-17T14:50:05.917

I apologize that I did not answer. I looked a few programs, and all of them did not detect changes within the hierarchy. I could detect this with the shell, but it would be too cumbersome, so I wrote a native application: http://d.pr/f/Dc0k. Hope I helped you.

– ifau – 2013-01-19T01:23:45.223