If you never have ‘dots’ in your folder you could filter with if statement like this.
And also filter the hidden files that start with a dot.
The default desktop folder on Mojave have 68 files (the subfolder is not counted)
There is 4 hidden files and 1 subfolder.
If the string contains “.” and do not start with “.” we will only get the files that has
basename.ext in return…
ex.
use framework "Foundation"
use scripting additions
set thePath to POSIX path of (path to desktop pictures folder)
set {theResult, theError} to (current application's NSFileManager's defaultManager())'s contentsOfDirectoryAtPath:thePath |error|:(reference)
set theSort to (theResult's sortedArrayUsingSelector:"compare:") as list
set theList to {}
repeat with i from 1 to count theSort
if (item i of theSort contains ".") and (item i of theSort does not start with ".") then
set theFilePath to thePath & item i of theSort
copy theFilePath to end of theList
end if
end repeat
return {count theList, theList}
To make a deeper sorted list of basename.ext include hidden directory we could use the method subpathAtPath
use framework "Foundation"
use scripting additions
set thePath to POSIX path of (path to desktop pictures folder)
set theResult to (current application's NSFileManager's defaultManager())'s subpathsAtPath:thePath
set theSort to (theResult's sortedArrayUsingSelector:"compare:") as list
set theList to {}
repeat with i from 1 to count theSort
if (item i of theSort contains ".") and (item i of theSort does not start with ".") then
set theFilePath to thePath & item i of theSort
copy theFilePath to end of theList
end if
end repeat
return theList
Now we could do something like this to filter the hidden directory but include the subfolders basename.ext
use framework "Foundation"
use scripting additions
set thePath to POSIX path of (path to desktop pictures folder)
set theResult to (current application's NSFileManager's defaultManager())'s subpathsAtPath:thePath
set theSort to (theResult's sortedArrayUsingSelector:"compare:") as list
set theList to {}
repeat with i from 1 to count theSort
if (item i of theSort contains ".") and ((item i of theSort does not start with ".") and (item i of theSort does not contain "/.")) then
set theFilePath to thePath & item i of theSort
copy theFilePath to end of theList
end if
end repeat
return {count theList, theList}
The first script give me 0.003s (doesn’t include subfolders) sort 68 items (basename.ext)
The last script give me 0.014s (include subfolders) to sort 87 items (basename.ext)