Applescript - how can I select all *non*-folders in an expanded list view window?

If all folders are expended in the list view , here are two solutions.

First solution:

This AppleScript

my deselectAllFolders()

on deselectAllFolders()
    script o
        property sel : {}
    end script
    tell application "Finder"
        set o's sel to selection as alias list
        set tc to count o's sel
        repeat with i from 1 to tc
            if class of item (item i of o's sel) is folder then set item i of o's sel to missing value
        end repeat
        set selection to aliases of o's sel -- select all items without folders
    end tell
    return "" -- to not put all files into the result (it's very slow to show an huge list in the editor )
end deselectAllFolders

Cons:

  • All items must be selected before executing the script.
  • Slow on huge list of items.

Second solution:

This AppleScript

set pScript to "from Foundation import NSFileManager, NSURL, NSDirectoryEnumerationSkipsHiddenFiles, NSURLIsDirectoryKey, NSURLIsPackageKey; from ScriptingBridge import SBApplication
def procFolder(fold):
    p = df.contentsOfDirectoryAtURL_includingPropertiesForKeys_options_error_(fold, [NSURLIsDirectoryKey, NSURLIsPackageKey], NSDirectoryEnumerationSkipsHiddenFiles, None)[0]
    for f in p:
           r=f.getResourceValue_forKey_error_(None, NSURLIsPackageKey, None)
           if r[0] and r[1]:
               allFiles.append(f)
           else:
               r=f.getResourceValue_forKey_error_(None, NSURLIsDirectoryKey, None)
               if r[0] and r[1]:
                     procFolder(f)
               else:
                     allFiles.append(f) 

allFiles = []
df=NSFileManager.defaultManager() 
finderApp = SBApplication.applicationWithBundleIdentifier_(\"com.apple.finder\")
tPath = finderApp.FinderWindows()[0].target().get()
procFolder(NSURL.URLWithString_(tPath.URL()))
finderApp.setSelection_(allFiles)"

do shell script "/usr/bin/python -c " & quoted form of pScript
  • No need to select all the items in the list view.
  • Fast (1.5 second on 8500 items)