How to list in Terminal only files visible in Finder?
Originally posted as a comment on the off-chance this might produce the required result, although wasn’t able to test myself at the time. However, the OP confirmed that, on his system (Big Sur 11.6, Apple Sillicon, APFS), the following command returns the contents of a directory that matches what is visible in a Finder window that is opened and pointed at the directory path:
tell application id ("com.apple.systemevents") ¬
to get every item in the folder named ¬
"/path/to/folder" where it is visible
This returns a list
of file
and folder
references that resolve much faster than Finder file references. It will be even more performant if one retrieves a list of file paths instead:
tell application id ("com.apple.systemevents") ¬
to get the POSIX path of every item in ¬
folder "/path/to/folder" whose visible ¬
is true
Since System Events accepts simple file paths in place of file references as parameters in many of its commands (e.g. move
, delete
, etc.), this can speed things up dramatically, and in most cases, will be infinity time quicker than Finder without blocking it.
Should you require an alias list
in order to make use of commands only available through Finder, e.g. reveal
, then†:
tell application id ("com.apple.systemevents") ¬
to get every alias in the folder named ¬
"/path/to/folder" where it is visible
tell application id ("com.apple.Finder") ¬
to get the result as alias list
† tbc