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.
Newly created folder with the first file being added.
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?
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.
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.
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:
recognize that I'm adding a file to folder,
analyze the file names within that folder and recognize that there is a numbering sequence to the file names and then
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:
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.
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.
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.
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.
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).
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.
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:
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:
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.