Select files in Finder

Hi

Often I have a folder full of files, where I need to select some of them. Either based on extension or some part of their name.

What I would like to have was a macro I could trigger.
It ask for what string should be used to select files in the active finder window.
And the only these files are selected in Finder.
Then I can decide to open them, batch rename them or delete them. This should not be part of the macro.

As I see it, then I need to use AppleScript as I can not find a way to do this with Keyboard Maestro Actions.

This is a example of a folder, where I would like to select all files with "_report" in the name.

Sounds like good middle ground between command line and GUI ...

Presumably simple globbing like *.html or *2014* etc rather than regex ?

If so, a rough start might look like this (the second osascript stage is in Applescript - I haven't figured out how to do Finder selections with JXA Paths yet)

AS Select Files in Active Finder by Filename pattern copy.kmmacros (4.1 KB)

3 Likes

This works like a charm.
Thanks.

Good.

(I’ve just updated it slightly (above) to use only Applescript, for backward compatibility)

Simply beautiful. I bought Houdahspot 4 for this functionality, to be able to search for files using wildcards because I had done it for ages on Windows using Everything.

Thanks for this, very useful for isolating files quickly. And I also miss Everything too.

Hey Jimmy,

I've been doing that for almost 2 decades with AppleScript, an osax that groks regex, and a utility that runs scripts.

THIS MACRO REQUIRES INSTALLATION OF THE SATIMAGE.OSAX (APPLESCRIPT EXTENSION).

Files { Select in Front Window } { KM Version }.kmmacros (7.6 KB)

β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”
Prefix	Example			Notes
β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”
None	any text		Any name containing your literal text is returned.
/	/j\..+tolkien		RegEx looks for your pattern anywhere in file name.
e/	e/.*j\..+tolkien.+	e/ is a mnemonic for exact (allows fancy patterns).
s/fi	None			Selects all files β€” s/ is a mnemonic for select.
s/fo	None			Selects all folders.
s/im	None			Selects all images.
suf/	suf/jpg			Selects by suffix
β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”

The script should be easily adapted to JXA. (Yosemite-only)

I did at one time adapt this to use Keyboard Maestro's regex, but due to the very slight pauses between actions it turned out to be slower than the AppleScript ( and I don't like to wait :).

The macro is very fast, but due to the way the Finder works it slows down as the number of files in a folder increase. So selection in a folder with a thousand files will be much slower than in a folder with 500 hundred files.

Selection on my system (mid-2010 i7 MacBook Pro) in a folder of 500 files is less than 2 seconds, and with fewer files it gets close to instant.

-Chris

2 Likes

I keep getting an error every time I invoke the macro. How do I resolve it?

Can’t make Β«class aliaΒ» "" of application "Finder" into type alias.
Num: -1700

<img src="/uploads/default/1194/f7c06afa8077f18c.png" width="422" height="172

Edit: I realized that the error was occurring because the Finder was opening to "all my files" instead of a real folder.

Did you install the scripting addition?

If yes, then I needed to restart before it was loaded. May a logout and login can do the trick.

Hey Mirizzi,

Thanks for the update. I should be able to account for that issue and will look into it in a minute.

-Chris


Edit: Post 7 Macro: bug fix.


I’ve updated the macro in post 7 to throw an error when the front Finder window is a Spotlight search or is otherwise unavailable for selecting files.

While I was at it I took a stab at selecting files in a Spotlight Search Window with System Events but couldn’t manage to select more than one row in the list-view window at once. I’ll look into this a bit more but am not optimistic about a workable solution.

-Chris

Brilliant Chris. Quick question, can the macro be edited so that it works with Pathfinder and Finder?

Hey Duncan,

Path Finder's AppleScript support is very poor. The more people who bug Cocoatech about it the better.

-Chris

Thanks Chris, out of interest do you know if Total Finder is AppleScript friendly?

Hey Duncan,

I used to use Total Finder, but it screws with the indexing of windows enough that I gave up on it.

I have about 75 AppleScripts I use regularly in the Finder, and I just didn’t need the hassle.

-Chris

Chris, I just discovered your post/macro/script. Awesome! :sunglasses:

Great script/macro – many thanks!

2 posts were split to a new topic: Extracting MP3 Tags from a Text File into Keyboard Maestro Variables

Wow, thanks ComplexPoint! Very useful here!

Is it still possible to use this on Sonoma? @ccstone