Take the 2-minute tour ×
Super User is a question and answer site for computer enthusiasts and power users. It's 100% free, no registration required.

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.

share|improve this question

1 Answer 1

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");
}
share|improve this answer
    
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 Dec 31 '12 at 19:13
    
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 Dec 31 '12 at 19:20
    
I edited answer. Glad to help you. –  ifau Dec 31 '12 at 20:20
    
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 Jan 1 '13 at 20:59
    
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 Jan 17 '13 at 14:50

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.