Selecting Multiple Files

Hi. One can select and open multiple files like so:


property thePath : "Macintosh HD:AppleScripts:"

tell application "Finder"
	activate
	select {¬
		alias (thePath & "1.pdf"), ¬
		alias (thePath & "2.pdf"), ¬
		alias (thePath & "3.pdf") ¬
			}
	open selection
end tell

One can also programatically open multiple files using a list like so:


property thePath : "Macintosh HD:AppleScripts:"

property theFiles : {¬
	"1.pdf", ¬
	"2.pdf", ¬
	"3.pdf"}

tell application "Finder"
	activate
	repeat with i from 1 to 3
		open alias (thePath & item i of theFiles)
	end repeat
end tell

However, using multiple open commands is slower than opening a selection of multiple files, especially with large lists.

What I would like is to programatically select multiple files, and then open the selection. Is this possible?

J

Hi,

You can use lists of references in the Finder open command. When you open selection, you are using a list of Finder references with the open command. alias lists and lists of file references may also be used. Although string paths are not references, lists of these work also. The following script will error when the list of names are used:

tell application “Finder”
activate
set s to selection
set alias_list to {}
set path_list to {}
set name_list to {}
repeat with this_item in s
set end of alias_list to this_item as alias
set end of path_list to this_item as string
set end of name_list to name of this_item
end repeat
open alias_list
delay 1
close every window
open path_list
delay 1
close every window
open name_list – error
end tell

I used a couple of selected folders on the desktop here because their windows may easily be closed.

You can use names of disks also:

tell app “Finder”
open {“Macintosh HD”} – no error, these are disks
end tell

The best thing to do is use complete references (Finder, alias, and file references) because this is what the Finder dictionary says the clas of the direct parameter should be. You never know if a string path may not work in the next versions.

gl,

That’s because you’re giving the Finder lots of individual instructions to access the disk instead of sending it off with a single “shopping list”. But it’s not usually necessary to use the Finder’s selection in a script. You can use something like:

property thePath : "Macintosh HD:AppleScripts:"

property theFiles : {¬
   "1.pdf", ¬
   "2.pdf", ¬
   "3.pdf"}

tell application "Finder"
   activate
   open (every file of folder thePath whose name is in theFiles)
end tell

Caveat: Unlike your original scripts, this won’t error if any of the files don’t exist.

Thanks Nigel. I was going to mention that, but my Jaguar Finder doesn’t allow the filter reference form ‘whose’ with the ‘contained by’ operator.

My question is how is Joe getting the list of file names? Then the filter refernce form may not be necessary.

The two code snippets I posted were merely generic samples meant to illustrate what I wanted to do.

The list of files was defined by me, but the same could just as well be generated dynamically (e.g., from a database).

Nigel’s code works.

Thanks.

J

Hi, Kel. I seem to remember noticing the same thing when I first started using Jaguar, but it’s working OK here now. I didn’t notice when it started going right. Perhaps it was with one of the updates. I’m up to 10.2.8 now, though the Finder’s still apparently 10.2.1. Strange… :?

Hi Nigel,

Yeah, it’s working now! :slight_smile:

set name_list to {“Picture 2.pdf”}
set f to choose folder
tell application “Finder”
every file of f whose name is in name_list
end tell

No errors. I have the same versions of OS and Finder. I wonder if it could have been the System Events beta. Who knows.

Thanks a lot. I never would have checked it out because I thought Apple abandoned us when Panther came out.

Have a good day,