Sorting: Macro that changes the number before the name

I don't know how much information is necessary and useful here... I'll keep it brief for now. Essentially, I'm asking for help creating a macro that sorts documents by name in an app.

In the screenshot above, documents are sorted by a number before the name. If I want to place “James” between ‘Harry’ and “George,” I change the number for “James” to 150.

Of course, I can do this manually. However, it would be ideal to have a macro that (after a trigger) asks me for a number when “James” is selected.

The macro should leave the original name unchanged but change the number as I enter it.

Perhaps someone has a much better idea for achieving the same thing. If so, I would be grateful for a tip :smiley: Thanks.

What app are you trying to change these names/numbers in?

-rob.

What is the idea of changing number in Jame’s position? Do you want to have sorted list based on 2nd and 3rd word but still have the numbers in beginning? If yes, remove 1st word, next sort the list and add number prefix (increment by defined step). I can help you in defining such process?

@griffman It could be the Finder. This app works exactly the same when sorting by name is selected. Perfect for testing.

Yes, please :slight_smile:

As for the rest, I don't know how I could explain it any better than I already have.

The number before the name is a “tool” for changing the position of a document in a list without changing the name of the document.

In my imagination, I select a document, press a shortcut key, and now, for example, a small window appears. I enter a number, press Enter, and the selected document gets a new number in front of its name. I have no idea if that's possible.

[Edited 2025-12-28 14:12. See below for a version that removes an unnecessary variable assignment and which adds some explanatory comments.]

Change numerical prefix for file.kmmacros (3.7 KB)

2 Likes

Very nice, @kevinb. It looks terribly complicated, but it seems to do exactly what I wanted :slight_smile:

I'll have to try it out with different apps for a few days. Thanks again for now!

1 Like

Oh! Here it is again with some explanations.

I have also got rid of one unnecessary variable assignment that I had accidentally left in! :slight_smile:

Change numerical prefix for file.kmmacros (5.3 KB)

1 Like

You are probably not interested anymore with my solution but maybe someone else will use it for other purposes.

Below KM macro assume that you call it with selection of files which names starting from numeric values like:

011 Charli Delta 
058 Whiskey Zulu
112 Alfa Bravo
329 Golf Kilo

and rename that files (in the same directory) based on order got after sorting file names without numeric prefix but add newly created numeric prefix to the file name like

1000 Alfa Bravo
1001 Charli Delta
1002 Golf Kilo
1003 Whiskey Zulu

The initial value of new prefix is assigned to KM Variable local_initPfx. If that value is less then 100, then new number prefix is formatted with 3 digits (with zeros in the beginning).

The core solution is in form of python script. In macOS exists python 3.9 what is enough for that script, but if you have any other (probably later version) it also should works.

Script takes list of files from standard input.

Inn may case I trigger it from StreamDeck, so please change trigger to your environment.

Because selection from Finder represents files as full path, first part of script split file names to based and base name and check if more then one based exists in the input. In such way script can’t be usable and finish without further actions.

The rest is fully described in comments but if you need help to understand any part, just ask on the forum.

Rename files with number prefix Macro (v11.0.4)

Rename files with number prefix.kmmacros (6.2 KB)

1 Like

Thank you very much @nutilius . I will take a closer look at this over the next few days :slight_smile: