Tuesday, May 11, 2021

#1 2021-02-05 04:51:31 am

Fredrik71
Member
Registered: 2019-10-23
Posts: 707

Sort, filter hidden and folders

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.

Applescript:

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

Applescript:

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

Applescript:

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)

Last edited by Fredrik71 (2021-02-05 07:11:07 am)


if you are the expert, who will you call if its not your imagination.

Offline

 

Board footer

Powered by FluxBB

RSS (new topics) RSS (active topics)