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

This could be a workaround. I could have a main folder with the real folders (because I will be using more than 1, even though I used 1 as an example) and then have another location with the Aliases and those would update the name.

But how would KM know that the file to update is the alias, since the name will always be different?

Ah, you need the folder name for the trigger. Missed that.

I think I'd go in a different direction from renaming the folder (which would force a backup every time Time Machine ran). A notification maybe.

I don't really use Time Machine, but forcing a backup isn't a problem anyway.
The issue seems to be the name being different every time.

Can't KM scan all folders inside a main folder and check if they have a specific name and if so, rename it? For example:

if there's an item with "work - ", rename it "work - number of items"

I know this sounds similar to what you said, @noisneil, but not quite. In this case we are triggering the main folder, so for example, every time the main (parent) folder's size changes (or the modified date changes), it triggers the macro. The macro then scans the content inside that parent folder and renames the folders. Does it make sense?

Yes, that's a problem. And that makes me wary of using a folder name for a file count. I think I'd rather just have a popup window that persists in the corner of the desktop if a notification isn't sufficient.

I like the idea! So I suppose we'd have to use a global variable to keep track of the alias name. With that in mind:

Update Folder Alias Name with File Count.kmmacros (25 KB)

Macro screenshot

Setup:

  • Move your "Work" folder somewhere for safe keeping. Make an alias of it and move the alias to wherever the original was before. Don't rename the alias.
  • Set the macro's folder trigger to the original folder in its new location.
  • Enable the red group, drag a file to the alias, and then undo. This is just to set a starting value for the OldAliasPath global variable.
  • Disable the red group.

Now, try adding or removing files from the alias (or original) folder and you should see the file count reflected in the alias name, like so:

CleanShot 2022-11-01 at 19.13.20

NB: If you want to use this for multiple folders, you'll need a unique global variable for each one. OldAliasPath1, OldAliasPath2, OldAliasPath3, etc.

3 Likes

I saw that on your macro, there's a yellow triangle on the "FOR EACH" action:

image

When I click I see this:

image

Did you miss something? Is there a typo?

Don't worry about that. The For Each action often shows that alert. I can't remember why now, but Peter did once explain that you can ignore it.

I was thinking a Dictionary would be better. Something like

{
    {
        OriginalFolder: /path/to/original/folder 1
        AliasFolder: /path/to/alias/folder 1-
        Count: 3
    }
    {
        OriginalFolder: /path/to/original/folder 2
        AliasFolder: /path/to/alias/folder 2-
        Count: 17
    }
}

You should then be able to use one macro with multiple watched folder triggers, pulling the OriginalFolder from the %TriggerValue%, composing paths from the relevant Dictionary entry, then updating the relevant Count value to the new item count.

To my shame I still don't know enough about KM Dictionaries to knock up a POC. Maybe tomorrow...

The trouble is you can't get the alias path from the triggervalue. Having said that, I guarantee I know less about dictionaries than you do, so maybe I've missed your point...?

I guarantee I know less about dictionaries than you do!

Having thought about it, it's because we're just counting the number of items, and not actually using the variable to do anything. It isn't really an error, but the Editor will still warn you about it.

1 Like

With the Dictionary as above you can ask for:

the AliasFolder value of the Dictionary item whose OriginalFolder has the value '/path/to/original/folder'

...and:

the Count value of the Dictionary item whose OriginalFolder has the value '/path/to/original/folder'

...then concatenate the two to get the path to the currently-named alias. (And that's why you have to update Count to the new value, after you do the rename, ready for next time.)

You should be able to do this when you drop a file/folder into the OriginalFolder, and I'm assuming that %TriggerValue% will include the path to OriginalFolder even when you drop a file into the alias (it is, after all, OriginalFolder that's changing and therefore firing the trigger).

Yes, it does. :+1:t3:

There isn't a "folder size change" trigger though. You might be able to use the front window trigger, which would do nothing unless you open the parent folder in a Finder window (i.e. the Desktop wouldn't be a great choice). It could then run the count when you go to actually view the folder.

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.