LSelect

Update: Now located at https://github.com/anoved/Finder-Scripts/tree/master/LSelect


Make Complex Finder Selections

example lselect dialog

lselect is an AppleScript that lets you select files in the Finder using shell glob syntax as you would to list files with ls. For an animated illustration of how it works, view this short screencast.

Download

See my comments in the script for more information about how it works and how it could stand to be improved. Other comments and discussion at Mac OS X Hints.

Changes

Version 1.1 improves compatibility with the Finder’s column view. One known remaining issue with column view is that if the last item in the list of matches is a directory, its contents will be revealed and other matches will lose selection. This may be a Finder bug.

Installation

Suggested installation location: ~/Library/Scripts/Applications/Finder/

The script can be invoked with the standard Mac OS X Script Menu, but I’ve found FastScripts to be a preferable alternative, primarily because of the ease with which reliable keyboard bindings can be assigned. I use ⌘G.

Usage

Actual usage scenarios exist, but if you have to ask what they are, this isn’t the tool for you.

Enter a glob pattern in the indicated field. This pattern will be compared against filenames in the current directory, and any complete matches will be selected. Briefly:

?
Match any one character.
*
Match any sequence of zero or more characters.
[abc]
Match any one character in abc. A range of characters may be specified as a-z, or 0-9, for example.
[^abc]
Match any one character not in abc.
{a,b,c}
Matches any of the strings a, b, or c, which may consist of more than one character each.

Spaces and certain other punctuation must be escaped with a backslash. For example, a folder called “Jim's Stuff” would be matched by the pattern “Jim\'s\ Stuff”. This requirement is not ideal. Please fix it!

Click Select Matches to select only those files that match the search pattern. If no files match, nothing will be selected. Click Add Matches to add any matches to the current selection. If Cancel is clicked the current selection will not be modified.

Similar Scripts & Utilities

3 Responses to “LSelect”

Posted by Ddes on Wednesday, April 29th, 2009 at 10:10 AM.

Thanks for this great script. Unfortunately, the version here doesn’t work for me. I’m on 10.5.6. There were, as far as I could tell, at least three issues.

1. The basic shell command was broken. Fixed this by replacing

("/bin/ls -d " & quoted form of pwd & query)

with

("/bin/ls " & quoted form of pwd & " | grep -i " & query)

2. if not (exists folder pwdAlias) then gives an error if the insertion location is a file, as happens at least when run from the toolbar in column view. Fixed this by replacing

	set pwdAlias to insertion location as alias
	if not (exists folder pwdAlias) then
		set pwdAlias to (container of pwdAlias) as alias
	end if
	set pwd to POSIX path of pwdAlias

with

	set pwdAlias to insertion location as alias
	try
		if not (exists folder pwdAlias) then
			set pwdAlias to (container of pwdAlias) as alias
		end if
	on error
		set pwdAlias to (container of pwdAlias) as alias
	end try
	set pwd to POSIX path of pwdAlias

which does work, despite being something of a hack.

3. At least after correcting the above two things, the select action was broken, probably because ls didn’t return full paths. Fixed this by inserting

set matchpath to pwd & matchpath

after

repeat with matchpath in paragraphs of matches

Now all is well, at least in my minimal testing. Anyway, here’s hoping formatting doesn’t get messed up, and thanks again for this script.

And yeah, I realize you wrote this years ago.

Posted by Jim DeVona on Sunday, May 3rd, 2009 at 11:06 AM.

Thanks for the fixes. For what it’s worth, I’m using version 1.1 on 10.5.6 without any trouble.

Posted by Zettt on Sunday, May 9th, 2010 at 4:00 AM.

Hi,

Just a little tweak for this script. Some applications prohibit the use of “display dialog”, to make their own application more stable, because they won’t lock up anymore while displaying a dialog.
Therefore those applications now use

tell application "System Events"

So you might want to add this before the display dialog thing (and end tell after, of course). I’ve tested this with Keyboard Maestro and LaunchBar and it works.
After “tell System Events” it’s also important to activate it to make the dialog come to the foreground. And at the end should be an activate, too, to make Finder become the most foreground application.
(I guess the display alert at the end also needs readjustment…)

http://dl.dropbox.com/u/6102/tmp/Select_files.zip