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

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.

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?