Sort order problem when working with Finder selection

I am in the process of digitising a large amount of vinyl 12" records. I am using Audio Hijack and have a system whereby I split files regularly while recording, so a 3-track EP may end up with 10 recordings, 7 of which are discarded, leaving something like this:

Shiva | Work It Out 002 | 002 | Work It Out | 1996 | House.flac
Shiva | Work It Out 004 | 004 | Work It Out | 1996 | House.flac
Shiva | Work It Out 006 | 006 | Work It Out | 1996 | House.flac

I have set up a file rename macro that takes a finder selection and then replaces the number there with a simple counter that is correct, so it should be renamed as this:

Shiva | Work It Out 1 | 1 | Work It Out | 1996 | House.flac
Shiva | Work It Out 2 | 2 | Work It Out | 1996 | House.flac
Shiva | Work It Out 3 | 3 | Work It Out | 1996 | House.flac

And it does, except there is a problem:

The image above shows that when iterating through the files I have selected, it is not always getting the right order. That is mainly because I sometimes have to re-sort the finder window (for various reasons) and may forget.

Is there a way that I can, in my macro, sort the finder selection so that it is ALWAYS in the sort by name ascending order, regardless of whatever sort order was in the Finder window?

I have realised that I have been renaming files incorrectly in places, and this affects things (as I later use those file counters to apply track names from a Discogs lookup). I have no idea how many I have got wrong now :frowning:

Pasted_Image_03_10_2022__12_33

TBH, I wouldn't rely on the sort order of the selection. Even if you could, you must have sorted the Finder window non-alphabetically for a reason and it would be plain rude for the macro to change that without asking :wink:

Instead I'd use a "For Each" action to go through the files, appending each path to a variable, sort the variable, then "For Each" through the variable doing what's needed. For example:

Sorted Finder Selection.kmmacros (4.0 KB)

Summary

That will sort your files by their full path, which is fine if they are all in the same folder. But it's easy enough to change it if you want to sort by name regardless of location.

There are other ways of doing this, but this method has the advantage of being "KM native" so will be easy for you to adapt for other situations.

Ah yes, that would work nicely. I have done something similar in the past, but clean forgot about that approach. To be fair, I have forgotten so much about things I've done in KM it's scary. :smiley: