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.

2 Likes

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

Hey gglick

I'm back on the case and hopefully a few years wiser.

I'm looking to edit this to append the result to the end of a file name and add a space so
'Scan 16' when selected in a Finder folder is changed to 'Scan 17' as it works now the name is changed to '17Scan 16.'

What makes this append the result as a prefix? I tried changing the instances of prefix to suffix thinking 'LocalPrefix' was a token and not a declared variable as I don't see where it gets declared. That gave a 'Set Variable to Calculation failed to evaluate LocalCounter+1. Macro ā€œAdd Numerical Suffix to Selected Fileā€ canceled (while executing Set Variable ā€œLocalCounterā€ To Calculation ā€œLocalCounter+1ā€)'

Is it the RegEx /d+$ ?

That translates to 'one or more digits followed by one or more nondigits, and that should be the whole string, not a substring' over at Stackoverflow which seems to have no pre/suffix value.

Hi @BernSh,

It's not clear to me exactly what it is you're trying to accomplish. Looking back at your OP, it seems like the original goal was to add numerical prefixes to files, but from what you've said today, I'm not sure if you want to change the prefix to a suffix, or if you want to be able to increment numbers in selected file names. If it's the former, you need to change this final action from this:

to this:

The %LocalCounter% variable is where the file name's number is stored, so to make it a suffix instead of a prefix, you just need to change the renamed file name so that it comes after the file name instead of before. The LocalPrefix part is also a variable, not a token, though it isn't declared with a "Set Variable toā€¦" action. Instead, it's declared here, at the beginning of the For Each action:

When you changed the instances of "Prefix" to "Suffix", that stopped the LocalCounter variable from receiving the data it needs to calculate the number, which is why you got that "Set Variable to Calculation" error.

The regex \d+$ is used to mean "one or more digits at the end of a string", which, while not inherently possessed of any pre/suffix value, would generally be used as a suffix given its position at, well, the end of a string.

As for the second possible interpretation of your last post, trying to increment numbers in selected file names, this new macro should be able to do that regardless of where the number is in the name:

Increment Numbers in Selected Files.kmmacros (4.1 KB)

2 Likes

This is really nice. And I was wondering if it could be adapted to keep the number padding of the original number. As it is, it will increment 043 to 43 and lose the leading zero in the rename.

There must be a simpler way to do this, but the below works to renumber 043 to 044 in the filename or 0001 to 0002 etc.

Increment Numbers in Selected Files - keeping number padding.kmmacros (7.4 KB)

Click to Show Image of Macro

1 Like

I looked through your macro and didn't see you using the formatting option for calculations. (Forgive me if I missed something.)

Remember that you can do something like this:

Increment Numbers and Keep Number Padding.kmmacros (3.0 KB)
Keyboard Maestro Export

Unfortunately you can't assign the format value with a variable, so you can't assemble one on the fly ā€“ but some variant of this ought to work for most things.

@peternlewis ā€“ any chance you'll make the format field accept a variable?

1 Like

Yes, because as you say you cannot put a variable in that formatting field and I wanted it to work based on the original padding used in the filename. So, I used %CalculateFormat% instead. And yes, it would be a lot simpler if the formatting field could take a variable.

1 Like

Got it. My eyes passed right by %CalculateFormat% when I was looking through the macro.
:man_facepalming:

1 Like

Hey gglick, thank you for your time and attention. It is generous and a gift. Fitting for this time of year.

This exactly answered the question I asked poorly. @Zabobon's extension is a nice bonus though a bit tougher to follow.

2 Likes