How to Sort a List of Filenames by a File Property

I'm trying to work out how to sort a list of filenames not by any text in the filenames themselves but by properties of those files. In particular I want to be able to sort on, separately, the position property (which has x and y components) or the comment property, which will be set previously.

For simplicity, I've been trying to do the comment version first in hopes that understanding how that works will illuminate how to do it for row and column positions instead of a single value.

I will be setting the comment value for each file by stepping through each of my Mission Control Desktops where I have placed a file on each desktop. I will get the number for the desktop and save it in the comment property for the file that is on that desktop. So when I sort the list of files by the comment value, it will be sorted into the order that matches the Mission Control Desktop order.

I haven't yet worked out how to sort that list of files according to the specific property. I know, conceptually, about various sorting algorithms, but making them work in AppleScript or KBM is my challenge here.

The second kind of sort is on the position property. For the Icon View of a Finder window, each file has a position property that is the xy position of the center of the icon in the Finder window. When those icons are positioned manually and then the Clean Up function is run, it aligns the icons to a grid. I want to list those files, in LtR reading order, across each row and then down row by row.

The ultimate goal is to verify that my arrangement of file icons in the Finder window (the position property) matches the order of the files as they have been placed in the various Mission Control Desktops (the comment property).

If I manually rearrange the order of the Mission Control Desktops (e.g., to put a project desktop next to my To Do List desktop) I will need to remember to manually rearrange the Finder window icons, so I want an alert that they don't match. I may eventually also be able to automatically rearrange the icons in the Finder window to match the Mission Control Desktop order and I will need to know when such an update is needed.

Where I'm stuck right now is how to write the sorting functions so that it arranges the list of files based on the specified property, either the one-dimensional comment number or the two-dimensional position.

Can anyone point me to sort functions that I might use as examples?

Here's one way to list files sorted by their comment attributes:

List Files by Sorted by Comment Attribute.kmmacros (23 KB)

Macro screenshot

I remember you figured out how to get the window position property where I failed. Can you show me how that's done so I can try to use it to build a sorted list macro?

Comment #24 of the thread:

That post includes AppleScript code that lists the filename, position, bounds, and comment properties of each file in the hardcoded folder. That's probably what you remember.

One thing I discovered, when AppleScript gets the filename from KBM, it doesn't know initially that KBM is giving it a POSIX path, you have to tell it. And you have to convert that POSIX path to an alias to be able to get the properties of the file. It reminds me of Alice talking to Humpty Dumpty in "Through the Looking Glass" where he explains, "That's not the name of the poem, that's what the name of the poem is called."

(Elsewhere I posted a version of that which added the default application, which is not a Finder property, you have to get it from System Events. If you need that, I can find the thread.)

Here is AppleScript code that works inside a KBM macro to get the position property:

set kmInst to system attribute "KMINSTANCE"

tell application "Keyboard Maestro Engine"
	set thisKBMFile to getvariable Ā¬
		"local_ChosenIconFilename" instance kmInst
end tell

set selectedFile to POSIX file thisKBMFile as alias

tell application "Finder"
	set thePosition to position of selectedFile
end tell

return thePosition

This is the core of what will probably become a subroutine for other macros to use in getting any particular file icon's position in the Finder window.

For testing, I put the above code in a macro that demonstrates that it has found the position by drawing a KBM rectangular highlight around the icon when that icon has been selected and the macro is run.

Get Center of Icon, Show Rectangle Macro (v10.2)

Get Center of Icon- Show Rectangle.kmmacros (10 KB)

Keyboard Maestro Export

Thanks for your help on this!

HA! I just noticed that I set the Variable "StatusBarHeightInPx" twice, once as straight text and then again as a calculation. Obviously the first one can be eliminated.

Ok give this a try:

List Files Sorted by Icon Position.kmmacros (24 KB)

Macro screenshot

1 Like

Thanks!

I had been trying to "roll my own" Sort routine in AppleScript. Even with help from ChatGPT, I was not getting anywhere. I tried explaining to ChatGPT that I wanted to sort first by x and then by y, with lots of prose description of what that meant, and what ChatGPT gave me was:

	-- Sort by row first, then by column
	set sortedList to sortList(sortedList, 1, false)
	set sortedList to sortList(sortedList, 2, false)

where parameter 2 was sorting by item 1 or item 2 in the list, i.e., x or y, and parameter 3 was a flag for whether the sortList function would sort ascending or descending (which I had not asked for and which I'm not entirely sure worked any more than the two-level sort).

Using the UNIX sort utility is a much more straightforward route. Thank you.

With this reminder, maybe I'll start using shell scripts in my KBM more frequently instead of trying to do everything with AppleScript. Sorting is certainly a natural fit for this.

@noisneil -- Thanks for these two macros. In my testing, they work straightforwardly as I had hoped. I'll mark the second script as the "Solution" to my OP.

ALSO, thanks for turning me on to the fact that the comments property is available directly in KBM, without having to spin off into AppleScript. That whole "Get File Attribute" action is a fountain of information. I also notice the the comment attribute is not marked "(r/o)" which means that I can set the Comment value directly from KBM instead of having to go into AppleScript as I am currently.

AND thanks for turning me on to the %FinderInsertionLocation% token to identify the active Finder window. Yet another step that I don't have to do with AppleScript. I had been using AppleScript to tell me the name of the active Finder window. Again, much easier.

1 Like

Get and Set Attribute are very handy, aren't they!

In case you don't already know, ctrl-cmd-T will present you with all available tokens. Worth having a look through them. One handy one I found through that is %FinderSelections%. Notice it's plural, which means it returns all selected files as a list. :+1:t3:

I found out about āŒƒāŒ˜T in just the last couple of weeks (was it something you mentioned in another thread?) and have been finding it very handy.

I'm also getting used to using āŒƒāŒ˜A instead of using the main Editor menu. Usually several fewer steps, at least in the feel of it. The way its search works is very sweet: I can type simply "c" and it initially gives me the action starting with "c" that I most recently used, which in my case is usually the Comment action. Nuances like that are inspiring.

This Keyboard Maestro thing continues to surprise and amaze me. It is one "highly evolved" toolbox.

1 Like

I did mention it recently, so maybe yeah.

Here's the macro I use for adding actions. It dismisses the main action menu, which I find a bit too obtrusive:

Action - Insert.kmmacros (55 KB)

Macro screenshot

Indeed! It makes you wonder what Peter's cooking up for v11!

I presume it's only enabled when the KBM Editor is active. What do you do when you need to enter a backtick in the KBM Editor?

... And ... this is getting off topic for the OP, so if there's another place you want to move this conversation to, please feel free.

There's a macro for that. I should probably change that hotkey but it comes from muscle memory. It's my add plugin hotkey in Logic Pro, which feels very similar in function.

1 Like

Hi, @August. You might find this macro useful: Insert Token

2 Likes

This only seems to work for folders containing a small number of files.

As an example, try this:

File Icon Position.kmmacros (21 KB)

Macro screenshot

With the green action pointing to TextEdit, we get -1, -1 as the position, which is clearly wrong.

Change the path in the green action to a file in a folder with fewer neighbouring files and it will return the correct coordinates.

Edit: I just created a test folder with 200 test files in it and it worked fine, so perhaps there's something about the Applications folder...?

1 Like

I finally got around to banging on this one, and it turns out that it sorts the first field, which is the numerical order position, alphabetically, so it sorts "4" after "25".

...
22,/Users/amohr/Documents/Personal/DeskSpaceIDs/[X] eXercise:Health āŒ„āŒ˜X .rtf
23,/Users/amohr/Documents/Personal/DeskSpaceIDs/[B] Books, etc. āŒ„āŒ˜B .rtf
24,/Users/amohr/Documents/Personal/DeskSpaceIDs/[&] Art Projects āŒ„āŒ˜& .rtf
25,/Users/amohr/Documents/Personal/DeskSpaceIDs/[M] My Memoir Writing  .rtf
4,/Users/amohr/Documents/Personal/DeskSpaceIDs/[L] Trello Desktop āŒ„ā‡§āŒ˜T .rtf

I accidentally ended up with a lot of files missing their Comment field, I suppose a glitch in my "automatic updating" of that field, so the actual list starts at "4" and then jumps to "12", etc. to "25". Which makes the problem even more obvious.

Somehow I have to tell the "sort" that the first field is numeric, not text. I can't find an option to the Filter that will accomplish this.

image

I guess I'll have to replace that tidy little filter with an "Execute Shell Script" action.