List of files in folder - sort order of results?

If I ask Finder for a list of files in a folder it appears the sort order of the returned list is always alphanumeric. Is it a safe assumption that this is always the case? (I’m going to reference some large lists and I’d rather not have to re-sort them as I simply want to walk the list once.)

Mark

Hi Mark,

since Tiger there is a sort command in the dictionary of the Finder
for example


tell application "Finder" to sort theItems by modification date

Thanks for that, although I think it answers a different question ;). I’m not trying to sort a Finder window. In my context your code fails:


tell application "Finder"
	set theItems to name of every file of folder "Macintosh HD:Users:mwra:Documents:preview test:previews-lite1:"
	sort theItems by modification date
end tell

My issue is not whether I can sort a visual folder window, it’s whether I need to sort an AS list of its contents. I’m trying to figure out if an AppleScript list of a folder’s file, as returned by scripting Finder, always uses filename order or whether I need to explicitly re-sort the list in case it is in some other order. Filename order is what my script needs and that filename ascending to be the default list order returned by Finder.

However, if the user had ordered the folder in a different sort order (in Finder, such as by your code method) would a query of the folder’s contents still return a filename ordered list? My tests indocate not, but I’m not AS-savvy enough to feel confident I’ve tested this effectively - thus I ask here so those more expert can set me right.

Mark

the default sort order of the Finder is always by name

Great. Thanks! Good to have that confirmed.

Mark

Hate to tell you, but it is not true.

I am trying to find an answer too. I have a script, such that:

–Drag a folder of pdfs to the droplet
on open these_items
display dialog these_items as string
–etc.
end

So, if I have pdfs named: Proof_01, Proof_02, Proof_03, Proof_04 (the order in which the pdfs are sorted in the Finder Window, you would think I would see a dialog box, such that

user:Desktop:Folder_1:Proof_01user:Desktop:Folder_1:Proof_02user:Desktop:Folder_1:Proof_03user:Desktop:Folder_1:Proof_04
–and there are no spaces between list items

but instead I get

user:Desktop:Folder_1:Proof_04user:Desktop:Folder_1:Proof_02user:Desktop:Folder_1:Proof_03user:Desktop:Folder_1:Proof_01

See? OUT OF ORDER! Argh.

So what, Stefan, do we need to do to get the list items in the same order as the Finder Window?

Thanks.

The sort order of the Finder is by alpha, but you aren’t using the Finder. Your code is sorting by drop order.

That’s the point :slight_smile:

Can’t you then use the Finder to sort the list?

tell application "Finder"
	set theSortedList to (sort theItems by name)
end tell

Ah, that would explain why this failed:

I’d not set a variable to the output. A hazard of the infrequent AppleScripter. Can I do this:


tell application "Finder"
	set theItems to name of every file of folder "Macintosh HD:Users:mwra:Documents:preview test:previews-lite1:"
	set theItems to (sort theItems by modification date)
end tell

Or, must I use a different variable for the outcome of the sort? I ask lest trial give me a false positive trying, by inexperience, something that ideally I shouldn’t.

Thanks, Mark

No, you’re going to sort just the extracted names, which won’t work, because there is no modification date


tell application "Finder"
	set theItems to every file of folder ((path to documents folder as text) & "preview test:previews-lite1:")
	set theItems to (sort theItems by modification date)
end tell

to retrieve the names, you must use a repeat loop


tell application "Finder"
	set theItems to every file of folder ((path to documents folder as text) & "preview test:previews-lite1:")
	set theItems to (sort theItems by modification date)
	set theNames to {}
	repeat with oneFile in theItems
		set end of theNames to name of oneFile
	end repeat
end tell

I see. Many thanks.

Mark