Inverse selection in Finder

I use @ccstone’s excellent macro to select files in a folder in Finder.

But sometimes I then would like to inverse the selection.

Does anybody know of a way to do this in Finder?

I found this in a Google search, and it appears to work:

tell application "Finder"
    set inverted to {}
    set fitems to items of window 1 as alias list
    set selectedItems to the selection as alias list
    repeat with i in fitems
        if i is not in selectedItems then
            set end of inverted to i
        end if
    end repeat
    select inverted
end tell
3 Likes

This is awesome. I don’t know what I need it for but I am saving it.

1 Like

Thanks, Dan.

I tried with Google, but did not find what I needed.

Need to practise my Google Fu.

1 Like

It’s really interesting what you find when you test a search such as β€œInverse selection in Finder”

This is what I found…

3 Likes

Great! I never noticed this. Thanks for finding.

This β€œCommand Inverting Selection” actually dates back to MacPaint inverting pixels when drawing with the Command key down I believe.

And it works in Keyboard Maestro too.

4 Likes

Hey Christian,

I've wanted to do this on occasion, but I can't point to a specific use-case at the moment.

More often I want to select everything except items that contain a specific string.

In my macro that Jimmy cites above you can do some magic with a regular expression to accomplish that:

e/^(?>(?!\.mp4).)+

This will select everything but items whose name contains β€œ.mp4”.

-Chris

I know this is an old topic, but here's my "contribution"

I found the exact same script when I Googled this same question and some comments said that this script works well on a folder with maybe a few dozen files, but not when there are maybe 1,000 or so (they said they got a timeout error).
I just tested it with a folder with close to 400 files and it worked, so maybe macOS somehow "fixed" this, maybe it gets worse the more files you have, etc, but just wanted to leave it here in case someone get that error, it means it's "expected" somehow.

For just a few files, I think that running a script takes "longer" than using the manual approach:

  • Select the files you want to "keep"
  • Click and hold ⌘
  • Click the empty space next to the top file and drag to the last file (while still holding ⌘).
    This works for all 4 view options:
    Kapture 2023-11-11 at 15.45.30

Kapture 2023-11-11 at 15.48.33

Kapture 2023-11-11 at 15.49.13

Kapture 2023-11-11 at 15.52.04

2 Likes