Finder Question

Why does the first script below work properly, counting the number of items selected in the frontmost Finder window, while the second does not - returns zero for me in List view.

tell application "Finder"
	set s to selection
	set c to count s
end tell

but:

tell application "Finder" to set c to (count selection) --> always 0

I don’t have a direct answer for you Adam, but consider this (you’ll love it ;)):

tell application "Finder" to set c to (count (get selection)) --> works

If you don’t like that. well, ask Mark how he feels: Set index label just like OS9

Aww, sheesh, Bruce. I must have tried every combo I could think of including several "get"s, but not that one. Anyway, thanks.

You have no idea how many times I’ve had problems getting (pun intended) an object’s name from the Finder.

Yup, I played with this for a half hour before I gave up and used 2 “sets”.

Nice to know there’s another way…

It’s from your script, Torajima. I was trying to compress your solution a bit and used it as you can see.

The problem is that, as it stands, Finder’s selection is a property reference that requires evaluation to offer anything remotely useful. As I mentioned recently in the discussion about Embedded Script Objects for Speed in Processing, both ‘set’ (in the context of a property or variable) and ‘get’ will evaluate a reference. The ‘count’ operation can then be applied to what will then be a regular AppleScript list.

Little wonder that this behaviour creates some confusion - since it’s a bit lame. Other applications (Mail, Address Book, iPhoto, iTunes, etc.) manage to evaluate ‘selection’ without an explicit ‘get’ - as did the pre-Mac OS X Finder (which even offered filtering, such as: items of selection whose name contains “some text”).

(Incidentally, some applications, such as TextEdit and Safari, don’t yet feature a selection property.)