Looks like no one’s replied in a while. To start the conversation again, simply ask a new question.

AppleScript - list folder command

Just used the LIST FOLDER command for the first time on my new MacBook Air M1 (so using Big Sur). The order the names are presented in appears random now, when previously they were presented in name order. Why has this been changed? Or have I done something wrong?

MacBook Air 13″, macOS 11.0

Posted on Nov 26, 2020 9:43 AM

Reply

Similar questions

7 replies

Nov 26, 2020 1:35 PM in response to RedRedRobin9

When you assign the output of list folder, the folder names are taken in random order. If you want the names in alphabetical order, then the the following you perform that arrangement:


set theFolder to ((path to desktop as text) & "TestX:") as alias

tell application "Finder"
	set aList to (name of every item of folder theFolder whose name does not start with ".")
end tell


And I used a whose clause to exclude any "dot" files such as .DS_Store.


See the AppleScript Language Guide.

Nov 27, 2020 6:58 AM in response to VikingOSX

Hi


Sorry I'm not very good with AppleScript and you reply has confused me!


This is my code that used to present to files in alphabetical order, but now gives them in a random order:


set cards_folder to "/Users/..../Documents/..../"

set file_names to list folder (cards_folder) without invisibles

set num_files to count of file_names

repeat with file_no from 1 to num_files

set card_file_name to item file_no of file_names



Nov 27, 2020 9:09 AM in response to RedRedRobin9

Your list folder syntax does not retrieve files in sorted order, and AppleScript by itself, does not have a sort command. Thus, we must tell the Finder to retrieve the files, and it will do so with them inherently sorted ascending. Finder also has a sort command, but it is unnecessary here. Without Finder, you would have to write your own sort routine.


use scripting additions

set cards_folder to POSIX file "/Users/.../Docments/.../" as alias

tell application "Finder"
	-- tell finder to exclude invisibles which are UNIX files that begin with a dot
	-- because Finder AppleScript dictionary does not have a boolean property to
    -- test for document invisibles
	set file_names to (name of every item of folder cards_folder whose name does not start with ".")
end tell

set num_files to count of file_names

repeat with file_no from 1 to num_files
	set card_file_name to item file_no of file_names
end repeat
return



AppleScript - list folder command

Welcome to Apple Support Community
A forum where Apple customers help each other with their products. Get started with your Apple ID.