Script to multi-select a collection of files in Finder

Hi all.

We periodically receive a text file from an outside source with the filenames of hundreds of encrypted files that need updating.

We have an in-house application (eUpdator) with a terrible UI to update them, but it doesn't have an Open File dialog or other other way to select the files. You can only do this by dragging & dropping the set of files from Finder onto eUpdator's window.

I'm looking for a way to group-select all the files in Finder so I can drag them onto eUpdator simultaneously rather than one file at time. All the files reside in the same folder.

I tried the AppleScript below in KM, but Finder.app locks up (spinning beachball) after theSourceFolder window opens. I need to relaunch Finder in order to continue.

set theSourceFolder to (choose folder with prompt "Choose Folder containing files to process")
set theFileListPath to choose file with prompt "Choose File List"
set theFileList to paragraphs of (read theFileListPath)

tell application "Finder"
    open theSourceFolder

    try
        select (every item of theSourceFolder whose name is in theFileList)
    end try
end tell

Can anyone see any obvious flaw in the script or have a better script implementation and can offer some help?

I scoured the forum, but only found examples using a For Each action to select & process one file at a time but none that can do a multiple-select.

How many files are in the folder, and how many items are in theFileList?

The operation as shown is Order N*M, where N is the number of files in the folder and M is the number of entries in theFileList, because for each file in the folder AppleScript has to check every entry in theFileList for a match.

If both numbers are large, then that is going to be very slow.

Do you know for sure that every item in theFileList will have a matching file in the folder? If so, then you don't really need to do the test like that - you can just select the files listed in theFileList explicitly.

Try something like this:

set theSourceFolder to (choose folder with prompt "Choose Folder containing files to process")
set theSourcePath to POSIX path of theSourceFolder

set theFileListPath to choose file with prompt "Choose File List"
set theFileData to read theFileListPath
set theFileList to paragraphs of (theFileData)

set sel to {}
repeat with i in theFileList
	set p to theSourcePath & i
	set sel to sel & POSIX file p
end repeat

sel
tell application "Finder"
	select sel
end tell
2 Likes

Wow! That worked a treat. And blazing fast too. The folder contains several thousand files and the file list is several hundred, so I guess it was getting hung up because of the number of searches.

Thank you VERY much.

Cheers.

1 Like

Quite.

The Finder scripting is OLD code and is very much less than efficient.

You may want to look at this for further automation ideas.

Drag Finder selection to "Found Image" in Chrome Browser window - #11 by ccstone

I'm not sure how will KM will handle the drag and drop for hundreds or thousands of files, but the technique works well with a smaller number of items.

-Chris