Numbering Files added to Folders

Hi

I'm adding files (can be text files, emails, pics, etc.) to folders. Sometimes the folders are newly created for the file being moved (usually from the Desktop in iCloud to a folder in iCloud) and sometimes they are existing folders with files already in them. I can always move from the Desktop if that simplifies things.

Mostly these are client destination folders and the files being added are my various records of my interactions with them or notes about them or invoices to them, etc.

If there are a lot of interactions there can be a lot of files. To help me keep track of the order of interactions (and flow of our conversations) I've created a palette that adds a prefix (1,2,3...) to the file name. This requires selecting the file then clicking on the number in the "sequencing" palette. This works well for the initial ordering when I haven't done so ongoingly and have to sort things out.

You'd think (well at least I think) that sorting the folder view by date created or date added would keep things in order but I haven't found that to be the case so I usually end up going in and wrangling things into order manually. The real mess is then finding additional items later that require a bunch of renumbering to get it earlier in the sequence. Sometimes it's easier to cheat and just add a "a" or "b" after the number it comes after then renumber 20 items. @DanThomas has a nice mechanism for this in his MACRO: Palette Organizer.

Anyway, there seems to be two cases.

  1. Newly created folder with the first file being added.
  2. Existing folder with existing numbered files and a new file being added that needs to continue the existing sequence. This might be manually dragged in or moves by a KM macro aimed at that destination folder.

Is this clear enough to work with? If not, what is unclear?

Thoughts on how to do this or better use of existing Finder functions?

Thanks for reading!

Are you after a way of finding the current last number so that you can add a new file and have it automatically add the number?

What I would do is write a macro that takes the path to a file, either the currently selected file or the file just moved in to the folder by Keyboard Maestro. Either way, the file should already be in the target folder.

Then use Get File Attribute action to get the parent path of the file.

Then use For Each action with the Folder Contents collection to iterate through each of the files.

For each of the files, use Get File Attribute to get the file name, and if the name starts with a digit, use Search using Regular Expression action to search for the initial digits ^(\d+). If that number is more than the current best number (set initially to 0), then store the new number in the best number variable.

At the end, increment the best number by one and Rename a File to have that number at the start of the name.

1 Like

Hi Peter,

Thank you for taking the time. I saw in another post that you are focused on getting the next version of KM out.

To answer your question, yes, one instance would be to have a Finder folder with files prefixed with sequential numbers to which I add a file that would automatically add the next number of the sequence to that newly added file.

Another instance would be to add a sequential prefix number to a collection of files by the criteria of the date the file was added to the folder.

When I think about it more closely, that your focused question provokes, it's a pretty difficult thing to be asking for. That somehow, the macro will:

  1. recognize that I'm adding a file to folder,
  2. analyze the file names within that folder and recognize that there is a numbering sequence to the file names and then
  3. add the next number of that sequence to the beginning of the name of the newly added file

My current level of understanding allows me look around the forum to take some pieces to make this:


out of what you suggested.

I know enough to know this isn't in the ballpark. Hell, this demonstrates that I haven't even passed the driver's test to drive to the ballpark or even know where the ballpark is. :wink:

I see I'm currently in a position where I will not spend the time to learn this and am putting this request on pause for now.

Thank you for the space to get clearer about making requests that are currently too far for me to take on. I'll think more closely about the resource cost of my questions before making them going forward.

Thanks again.

No need to be so hard on yourself. You were much closer to the ballpark than you gave yourself credit for :wink:

This example macro worked in my testing to add an incremented prefix to the currently selected file:

Add Numerical Prefix to Selected File.kmmacros (5.1 KB)
image

And this one worked automatically on files added to the test folder (the desktop, naturally):

Auto-Add Numerical Prefix to Newly Added File.kmmacros (5.5 KB)
image

While I differed from Peter's described method slightly by using For Each to test the file prefixes rather than If Then Else and Search Using Regular Expression, the rest is pretty much exactly as he laid out. Hopefully you can adopt these examples to your needs and learn from them to create your own in the near future.

1 Like

Hi Gabe,

First and foremost, thank you. They go far to narrow the gap I need to cross.

Quotes are funny things to lookup as they’re usually various attributions.

I’m reminded of this one:

"You can’t cross a chasm in two small jumps.
(https://quoteinvestigator.com/2014/03/13/abyss-two/)

Someday a bridge may exist doing away with the leaping, but not this day.

Thanks again.

1 Like

@BernSh, If one of the above posts solves your problem/question as originally stated, please check the "Solved" checkbox (click for details) at the bottom of that post.

Otherwise, please post your remaining questions/issues about this problem.
If you have other questions, please start a new topic.

Thanks @JMichaelTX. I have started to do this and will slowly update all my previous posts.

My questions/issues are too numerous and seem to be a simple ignorance of programming in general and KM ‘programming’ here specifically.

Peter’s way which I completely appreciate leaves a gap too far to really attemp and Gabe's generous offer and kind words aren’t enough either, thus the leaping quote.

I’m not sure how to frame a new topic or work with this instance to resolve it to a point I’d call solved given the ground I think I need to cover. I’ve begun studying AppleScripts and it’s very slow going. Hopefully, I’ll gradually have enough distinctions to begin translating them here and make further headway on this and other things I’d like to do.

I glean small bits from each interact and am appreciative for the opportunity to do so. If using this forum this way messes up the form, structure, design, or intent of this forum I’ll redesign my approach to honor what is needed and wanted here.

Thank for driving things to completion, and as always, your commitment to workability.

While we generally encourage users to give us the big picture when requesting help, if the big picture is not clear to you, or you're still working on it, then maybe it would be better to break the problem down into bite-size chunks, and ask questions about that.

Frankly, I'd put AppleScript on the back burner for now. It uses a syntax and model unlike any other language I've ever seen, and I found it very hard to get started with it. Actually, JavaScript for Automation (JXA) is simpler to learn, and more powerful, because it is based on the core JavaScript engine used for all web pages.

IAC, put scripts on the back burner for now, and focus on KM. Most things you want to automate can be done using non-scripting KM Actions.

I'll repeat: Keep it simple to get started. Learn how to use KM for some small, simple tasks, and then you can learn how to combine them for more complex tasks.

Good luck! Like the Motel 6 commercial says: "We'll leave the light on for you" (meaning we'll be here to help).

1 Like