[SOLVED] Rename Folder When New File Is Added/Deleted

Here's my attempt at that. Seems to work well! Just be aware that the numbers will only update when you navigate to the parent directory. Change the path in the green action at the top accordingly, and make sure you have a backup of any files you try this on, for peace of mind!

Update Folder Name with File Count.kmmacros (32 KB)

Macro screenshot

Hey Guys,

Just for fun โ€“ here's an AppleScript that will do the job. Of course the user is expected to build an appropriate KM macro with a folder trigger.

Note โ€“ this employs @Nige_S' idea of using a Finder Alias to reference the working folder.

-Chris


--------------------------------------------------------
# Auth: Christopher Stone
# dCre: 2022/11/01 19:55
# dMod: 2022/11/01 19:55 
# Appl: Finder
# Task: Rename a Folder by Appending The Count of Its Contained Items Using an Alias.
# Libs: None
# Osax: None
# Tags: @Applescript, @Script, @ASObjC, @Finder, @Rename, @Folder, @Item-Count
--------------------------------------------------------
use AppleScript version "2.4" --ยป Yosemite or later
use framework "Foundation"
use scripting additions
--------------------------------------------------------

# You must create a Finder Alias to the working folder.
# workingFolderAlias is the path to the ALIAS โ€“ not the working folder.

set workingFolderAlias to "~/test_directory/Keyboard_Maestro_Test_Folder/TEST alias"

--------------------------------------------------------

set workingFolderAlias to alias POSIX file (((current application's NSString's stringWithString:workingFolderAlias)'s stringByExpandingTildeInPath) as text)

tell application "Finder"
   set workingFolderRef to original item of workingFolderAlias
   set itemCount to length of (get items of workingFolderRef)
   
   if itemCount > 0 then
      tell workingFolderRef
         set workingFolderName to its name
         
         if workingFolderName contains "-" then
            set its name to (text 1 thru (offset of "-" in workingFolderName) of workingFolderName) & space & itemCount
         else
            set its name to (get its name) & space & "-" & space & itemCount
         end if
         
      end tell
   end if
   
end tell

--------------------------------------------------------

Hiya Chris,

Your script renames the original folder and leaves the alias alone. Is this intentional?

Also, if the folder name includes "-", everything following it will be erased and replaced. As an example, Project A - Part 1, containing 4 files will become Project A - 4. I safeguarded against this sort of thing in my last attempt, although I prefer the alias idea, as it doesn't rely on navigating to the parent path to update the count. Do you think you might be able to tweak your script?

Here's the macro I'm using to run your script:

@ccstone - File Number in Alias Name.kmmacros (22 KB)

Macro screenshot

You can either keep the original folder names static and use aliases as "display names" (my suggestion) or keep the aliases static and use those aliases to reference an always-changing original folder (Chris's method, letting the OS track the alias->renamed file for us).

Which you use will depend on what else you want to do -- I'd find it very confusing to have to reference aliases instead of the originals if targeting those folders with other macros (result of the second method), others won't like that the "display folders" have alias icons and aren't "real" folders (result of the first). YMMV.

For a multi-folder setup like @iamdannywyatt's proposing I think I'd go the first method -- that way you could have your original folders scattered all over your file system, but have one folder with all the aliases for a single window on all "current counts".

I guess this wouldn't work for what I'm trying to achieve because my goal is to sometimes have a finder window with some folders (the ones with the numbers) and then next to it, the window with the files/folders that I will dragging into those folders. For example, let's say I want 4 folders and each have a particular numbers of files I want inside.
Folder 1 - 12
Folder 2 - 5
Folder 3 - 17
Folder 4 - 32

Now I go to the other window and start dragging and dropping files and I will be able to keep track of how many files I have inside each one of those folders, so that means that the focused window will be the one with the source files, so I guess the macro wouldn't get triggered. Right?

In that case, you're better off with this or @ccstone's suggestion.

Yep.

Since the OP didn't have hyphens in his working folder name I didn't think that was relevant except when there was already an existing item count.

Try this:

--------------------------------------------------------
# Auth: Christopher Stone
# dCre: 2022/11/01 19:55
# dMod: 2022/11/02 16:26
# Appl: Finder
# Task: Rename a Folder by Appending The Count of Its Contained Items Using an Alias.
# Libs: None
# Osax: None
# Tags: @Applescript, @Script, @ASObjC, @Finder, @Rename, @Folder, @Item-Count
# Vers: 2.00
--------------------------------------------------------
use AppleScript version "2.4" --ยป Yosemite or later
use framework "Foundation"
use scripting additions
--------------------------------------------------------

# You must create a Finder Alias to the working folder.
# workingFolderAlias is the path to the ALIAS โ€“ not the working folder.

set workingFolderAlias to "~/test_directory/Keyboard_Maestro_Test_Folder/TEST alias"

--------------------------------------------------------

set workingFolderAlias to alias POSIX file (((current application's NSString's stringWithString:workingFolderAlias)'s stringByExpandingTildeInPath) as text)

set AppleScript's text item delimiters to " - "

tell application "Finder"
   set workingFolderRef to original item of workingFolderAlias
   set itemCount to length of (get items of workingFolderRef)
   
   if itemCount > 0 then
      tell workingFolderRef
         
         set workingFolderName to its name
         set newWorkingFolderName to text items of workingFolderName
         
         if length of newWorkingFolderName = 1 then
            set newWorkingFolderName to newWorkingFolderName & itemCount
         else if length of newWorkingFolderName > 1 then
            try
               last character of (newWorkingFolderName as text) as integer
               set last item of newWorkingFolderName to itemCount
            on error errMsg number errNum
               set newWorkingFolderName to newWorkingFolderName & itemCount
            end try
         end if
         
         set name of workingFolderRef to (newWorkingFolderName as text)
         
      end tell
   end if
   
end tell

--------------------------------------------------------

-Chris

I must have misunderstood something then. What should the folder trigger be set to? It can't be the original folder, as its name changes.

Fair point. I suppose I'm thinking of broader applications. I use a lot of hypens in folder names.

I tried it and it still chopped everything after an existing hypen. This is why I thought checking if the current name ends in Files) was a nifty workaround. Then again, I'm obviously doing something wrong, so maybe it does work when set up properly.

Nah, that was my goof...

I thought you could use the alias file path instead of the actual folder path, but Keyboard Maestro only sees the alias and not what it points to.

I should have tested more thoroughly.

I'm not sure how I'd handle this problem in light of the Folder-Trigger issue.

  • AppleScript Folder Attachment Script?
  • LaunchD Script?
  • Rewrite the XML of the macro when the folder path changes?
    • The Keyboard Maestro Editor would have to remain open to do this.

It shouldn't. I tested with:

Test
Test-Stuff
Test - Stuff
Test - 1
Test-Stuff -1
Test - Stuff - 1

What are you starting with?

1 Like

Similar to your examples. It's not unthinkable that my macro is causing an issue. Seems moot at this point.

This works, so maybe it's the best option, @iamdannywyatt. Have you tried it?

Not yet. Will see if I can find time today to check this whole thread, because there are so many options now and I need to sit and see them all.

Meanwhile I was thinking about a possible workaround.
I don't know if this is possible, but can KM work with Finder tags?
If so, what if KM is "watching" a folder with a "Work" tag and so even if the name of the folder itself changes, KM will just be focused on the tag so we can say "if there's a folder with the tag WORK and it changes the number of items inside, rename it WORK - NUMBER OF ITEMS"?

Would this be a possible solution? That way we don't need aliases and all that.

Folder-change triggers require a path to the folder concerned, and only trigger on changes to the folder (ie not when you add items to a sub-folder) so you couldn't even have a static-named "master" that triggered when you added items to folders inside it.

I still think aliases are the route to go -- if only because it lets you have your work folders in lots of different places while you can have just one folder, open all the time, showing your updating aliases.

Of course, you could just open the work folders in the Finder, turn on the "Status Bar", and simply look at the number of items in them!

1 Like

Keyboard Maestro can't monitor the contents of subfolders and use that as a trigger, but Hazel can (and the names of those subfolders can change)... and Hazel can trigger a Keyboard Maestro Macro. So, that could be used to trigger @noisneil's Macro that changes subfolder names without needing aliases.

Personally, I think the simplest solution is to trigger that same Macro with a hotkey. And just press that hotkey when you want to see the updated folder count.

Link to Hazel monitoring subfolders (of changing names):
https://www.noodlesoft.com/manual/hazel/advanced-topics/processing-subfolders/

2 Likes

I happen to know @iamdannywyatt uses Hazel (he turned me on to it!), so this sounds like a perfect marriage!

1 Like

Sweet!

Even sweeter!

Which means I can stop banging my head against the current brick wall, which is "find a nice way to update the dictionary". The problem, in case anyone has an easy answer:

I want a Dictionary with key/value pairs of {/path/to/original,/path/to/alias}. The key value is easy to set -- pop a "Prompt for folder", let the user select a folder.

But when you try to do that for an alias to set the value, the alias is resolved to the original folder so you end up with {/path/to/original,/path/to/original} -- which I really, really, should have seen coming...

You can get round it by selecting the alias in the Finder then running the macro and using "Finder selection", but that feels a bit clunky. Anyone have a better way?

@Zabobon Good call! It works!

CleanShot 2022-11-03 at 11.31.31

Update Folder Name with File Count (Hazel Version).kmmacros (30 KB)

Macro screenshot

Hazel Screenshot

@iamdannywyatt, if this is too slow for you, the hotkey idea probably is best.

3 Likes

How would I get that number for Hazel?
I'm assuming that number is particular to each computer?
Or should I copy the one you shared?

Also, besides the green box to set the folder, is there any other thing that needs to be changed? I see that purple/blue box. Is that something we need to modify or something?

In case I have multiple folders inside the Test folder, this will automatically update them or do I need to make changes?

EDIT: @noisneil I found the number (which is actually the same as yours. I thought it would be different on different computers). I didn't know about the UUID thing. I was just messing around trying to see if there was a "copy macro number", tried that option and it gave me the number.

Also, it's working! :slight_smile: Thank you so much for that!

Hazel would be able to do all this, but I think there's an issue with the app. If you use the condition you used and then under "Do the following..." you pick Rename, then under Pattern you add the name of the folder, for example "Work - " and then after that you add "Other..." and pick "Number of items".

He also first suggested this condition:
image

For some reason, it's not working for me. The developer was trying to show me how it works, but it's not for me. Can you try it on your computer? Maybe with both conditions (one at a time) and see if they work? Maybe it's my OS, maybe it's my version of Hazel?

image

I also saw that I don't need to do anything to the main folder. I just need to add new folders and it will know what to do.

1 Like

I'll have a go tomorrow. :+1:t3:

1 Like

Ok just tried. It does change the folder name, but the item count is incorrect:

I think the Hazel/KM solution is great and there's probably no real advantage to trying to get it all happening in Hazel alone.

1 Like

Yeah I was getting some really weird numbers as well.

The only advantage I see with just using Hazel is that there's less stuff to set up. If you just want to monitor 1 folder with folders inside, that's not an issue. When you start monitoring more folders, then you have double the work.

But yeah, it's not a big deal anyway. The solution you shared works as it should, even though Hazel is sometimes very slow when it comes to updating, but it is what it is.

I appreciate you testing Hazel and also sharing the macro. I was looking at what you did and I wouldn't get there at all by myself. That's some advanced stuff.

1 Like