I need to return an alpha list of folders in a job folder so I can choose one from a list. I’m getting some kind of random sort and can’t figure out how to make it work.
NOTE that it worked pre-Catalina, but not now.
Any assistance is appreciated
tell application "Finder"
activate
set WorkingPath to (path to home folder as Unicode text) & "Dropbox:2020 ClientProjects:" -- as list
set FolderList to list folder WorkingPath -- as list -- (with properties sort by name)
-- try
-- tell application "Finder" to set sort of FolderList to name
set PickAFolder to (choose from list FolderList with prompt "Choose a Folder" OK button name "This One!" cancel button name "Stop this Madness" without multiple selections allowed) -- as string
if PickAFolder is "false" then
return
end if
-- end try
end tell
I’m a little surprised this used to work. It appears to actually retrieve the files and folders in the WorkingPath, rather than just the folders. list folder is not a “Finder” command, so either should be placed outside the tell application block, or called using the continue handler. That said, it’s also long been deprecated, although that means very little in AppleScript as it’s been deprecated for coming on 15 years, but still works. unicode text is superfluous in almost every present-day use case, and in those where it is functionally distinct from text, it will return a string encoded as UTF16, which will probably not be what you intended. Just use text or string.
Don’t use try blocks. They will get in the way, as you probably found out as you had to comment them out. Ergo, they are of no use for most scripting scenarios, as you either need the command to be successful for your script to go on to produce the desired result; or you need to end up isolating the source of the error, which invariably means removing the try block.
Here’s one possible way to do it:
tell application id "com.apple.Finder"
script WorkingPath
property parent : path to downloads folder
property list : my folders as alias list
property name : name of my folders
property result : sort my list by its name
end script
repeat with f in (a reference to the WorkingPath's result)
set f's contents to f's name
end repeat
end tell
set [namedFolder] to [] & (choose from list ¬
WorkingPath's result ¬
with prompt ["Choose a Folder"] ¬
OK button name ["This One!"] ¬
cancel button name ["Stop this Madness"] ¬
without multiple selections allowed)
if the namedFolder = false then return
set folderpath to the WorkingPath's POSIX path & namedFolder
It ought to have been possible to do it more simply, like this:
tell application id "com.apple.Finder" to (sort the folders in the WorkingPath by name) as alias list
(or astext with appropriate text item delimiters set), but it isn’t (anymore). Hence the loop through in order to change each of the Finder references to simple folder names. The script goes on to return the posix path of the user’s chosen folder, or nothing if they cancelled.
With the APFS filing system, which is installed with Mojave or Catalina, many commands which used to return folder contents sorted alphabetically by default no longer do so. And as CF has observed, ‘list folder’ is long deprecated and returns the name of everything in the folder, including files and invisible items.
I don’t know about Catalina, but in Mojave, the Finder itself does still return folder contents sorted by name, so:
tell application "Finder" -- or tell application id "com.apple.Finder"
activate
set WorkingFolder to folder "Dropbox:2020 ClientProjects" of home
set FolderList to name of folders of WorkingFolder
end tell
set {PickAFolder} to (choose from list FolderList with prompt "Choose a Folder" OK button name "This One!" cancel button name "Stop this Madness" without multiple selections allowed) as list
if (PickAFolder is {false}) then return
tell application "Finder" to set chosenFolder to folder PickAFolder of WorkingFolder as alias
Otherwise, it’s the explicit sort and repeat loop, as CF suggests:
tell application "Finder" -- or tell application id "com.apple.Finder"
activate
set WorkingFolder to folder "Dropbox:2020 ClientProjects" of home
set FolderList to (sort folders of WorkingFolder by name)
repeat with f in FolderList
set f's contents to f's name
end repeat
end tell
set {PickAFolder} to (choose from list FolderList with prompt "Choose a Folder" OK button name "This One!" cancel button name "Stop this Madness" without multiple selections allowed) as list
if PickAFolder is {false} then return
tell application "Finder" to set chosenFolder to folder PickAFolder of WorkingFolder as alias