Contents of Directory in which order is get?

Hi,

Just a simple question:

When I get contents of a directory with command like:

tell application “Finder”
set filesList to name of files in folder “Lion:Applications:”
end tell

The array is always returned “sorted” regardless the possible sort in the Finder related to the dir?

Ame

The order is actually defined by the file system the folder’s on. HFS returns them sorted alphabetically, but there’s no guarantee if the folder’s on another file system.

On my Mavericks machine, the Finder returns folder items sorted lexically but case-sensitively (ie. “aardvark” comes after “Zzyzz”). However, its ‘sort’ command sorts case-insensitively.

The sort command matches the sort order in Finder windows, I think. So 9ZZ comes before 10A. It’s what’s known as “localizedStandardCompare:” According to the docs:

From what I can find, HFS sorts all directories on file operations, something not all file systems do. So ls returns the same order as the Finder minus the sort.

Just to note:

The (get selection) in finder will return a sort order matching what you see in finder.
If I used size sort order in the finder and reversed the order to ascending. Then the sort order returned would match what I am seeing in the finder.

Just found the ‘sort’ command in the finders sdef.

This means you can get a sorted list from the files system and then iterate over it.

example:

set theList2 to {}

set fl to POSIX file "/Users/UserName/Pictures/screenshots/foo"
tell application "Finder"
	set filesList to (sort files in folder fl by creation date)
	repeat with i from 1 to number of items in filesList
		set this_item to item i of filesList
		copy name of this_item to end of theList2
	end repeat
end tell
log theList2

Hello.

My two cents, in lieu of using Finder’s sort command, is that it can be wise to have the sorting performed within a consider numeric strings block. That way, the sort command not only sort in case senisitively, but also sort numbered files correctly. :slight_smile:

Thanks.

But in my tests with numbered files the list was returned correctly. I.e reflecting what I see in the finder.
Which is what I think the OP wants.

This is because afaik it is using the same finder item property of the finder as the finder itself
(name, size, creation date).

And I could not get it to be wrong … can you post an example of a set of file names that would return an incorrect order that does not match the finder?

Hello.

I’m sorry, it used to be that way, that you had to include the consider numeric strings clause to get it right.

That seems to be fixed/changed. :slight_smile:

Hello

If numbers at the beginning of names are a single digit ones, the considering clause makes no difference.
It’s only if we have numbers with several digits that it’s meaningful.

Yvan KOENIG (VALLAURIS, France) samedi 7 décembre 2013 15:36:17

The considering clause is a red herring where the finder’s sort command is concerned

AFAIK:
It will always be ignored because the finders sort command is comparing a property and only returning a sort ordered list sorted using the specified property of the objects. Not a text comparison

I.e the sort command can be asked to sort by creation date. : date class
The considering clause is aimed at text: considering / ignoring (text comparison)

You cannot influence finder’s sort command comparison any other way but by it’s by parameter.

How you deal with the returned results is another matter.

Hello.

@mark hunte:
Yes, the herring seem to have changed colour, for it used to be this way, I have written a script for a user somewhere here, where that consider numeric strings solved the problem, and there should be another one here for Santa I think, that also uses the same considering numeric strings clause.

Yvan’s answer should also tell you that it used to work.

The fun thing about it, was of course that it worked because of the clause, and not by a parameter. :slight_smile:

This is how I honestly remember it.

Hello.

I’ll just post this example I found from AppleScript in a Nutshell, as evidence of how the considering clauses may affect Finder, this is of course how it used to be, unfortunately not including the sort command, but I have no time to dig up the former examples.

tell application "Finder"
   considering case but ignoring punctuation, white space and hyphens
      set theTruth to ("voracious appetite" is equal to "voracious, ¬
      appetite") --returns true
   end considering
end tell

This is an example I believe worked back in february when I was still on Snow Leopard:

considering numeric strings
	set mf to POSIX file (do shell script "ls -1d ~/Library/Caches/TemporaryItems/junk/") as alias as text
	set L to {}
	tell application "Finder"
		set theList to (sort every file of folder mf by name)
		repeat with af in theList
			set end of L to name of af
		end repeat
	end tell
end considering

Hi McUsrII,

Your points are what actually made me understand how the finder’s sort command is actually working. i.e making me pay more attention to whats going on.:slight_smile:

Luckily I have a 10.6.8 Mac.

After just dusting it off. I ran the code with and without the considering clause.

And there is no difference in the results. Both return the correct order depending on the finder sort property I specify.

by name:

{"19104.jpeg", "19110.jpeg", "19115.jpeg", "19130.jpeg", "19137.jpeg", "190916.jpeg", "190926.jpeg", "190933.jpeg", "190938.jpeg", "190944.jpeg", "190948.jpeg", "190953.jpeg", "190958.jpeg", "191010.jpeg", "191019.jpeg", "191025.jpeg", "191037.jpeg", "191042.jpeg", "191053.jpeg", "191112.jpeg", "191117.jpeg", "191131.jpeg", "191141.jpeg", "191156.jpeg", "191210.jpeg", "191219.jpeg", "191227.jpeg", "191234.jpeg", "191240.jpeg", "191251.jpeg", "191321.jpeg", "191341.jpeg", "191352.jpeg"}

by creation date:

{"190916.jpeg", "190926.jpeg", "190933.jpeg", "190938.jpeg", "190944.jpeg", "190948.jpeg", "190953.jpeg", "190958.jpeg", "19104.jpeg", "191010.jpeg", "191019.jpeg", "191025.jpeg", "191037.jpeg", "191042.jpeg", "191053.jpeg", "19110.jpeg", "19115.jpeg", "191112.jpeg", "191117.jpeg", "191131.jpeg", "191141.jpeg", "191156.jpeg", "191210.jpeg", "191219.jpeg", "191227.jpeg", "191234.jpeg", "191240.jpeg", "191251.jpeg", "19130.jpeg", "19137.jpeg", "191321.jpeg", "191341.jpeg", "191352.jpeg"}

The only difference in the OS’s is the creation date list in 10.6 is descending and in 10.9 it is ascending.

I think what may be confusing people is if you use the considering clause on a on an existing list of text items or to compare multiple text items as the clause is intended to be used then you should see what you are describing.

But the finder’s sort command is not ever going to considering anything before or after it returns it’s list.

it runs oblivious to anything outside of it’s parameters.

I admit I have never used the sort command before today that I can remember. But I cannot see how this would have been any different unless somewhere in the finders sort command before 10.6.8 the was a bug?

Hello Mark.

So you have been right in this all the time. :slight_smile: I am not going to bother to find something from before Tiger.

Very few apps support considering/ignoring clauses. I remember when it was added to InDesign, and it was considered a great rarity.

So the saying is true. There is always a first time…:slight_smile: