MacOS – Mac OS Finder – Read File Folder Order/Positions (Arrange By) thru Command Line

command linefindermacos

In the Finder , Arrange -> None provides the ability to specify a custom sort for the files within a folder.

Is there any way to read this info from the command line? A Mac OS utility equivalent to ls with the ability to get the sort data.

I realize that because of the graphical nature of the Finder folder, this info will be x,y screen coordinates

Ideally I could use this utility to build a shell script or command that returns a sorted list of files (kinda like ls -R and variants)

Best Answer

You can obtain this information using AppleScript, which is, of course, something you can execute from the command line using osascript.

Each Finder item has an AppleScript property called position that stores the {x, y}position of the item within its parent window (there’s another property called bounds that stores the coordinates that define the bounding rectangle of the item).

    tell application "Finder" to get the position of every file in folder "My Folder"
        --> {{40, 199}, {230, 43}, {40, 43}, {40, 121}, {40, 277}, {230, 121}, {267, 184}, {230, 277}, {420, 43}}

(Can you spot the item I moved off its grid-aligned position ?)

From the command line, you might implement it like this:

    osascript -e "tell app \"finder\" to get position of items in folder posix file \"$PWD\""

which, for the same folder as above, returns

    40, 199, 230, 43, 40, 43, 40, 121, 40, 277, 230, 121, 267, 184, 230, 277, 420, 43

For a specific file:

    osascript -e "tell app \"finder\" to get position of file \"Self Portrait I.jpg\" in folder posix file \"$PWD\""
        --> 230, 43