Finder can't get name of selection

I did lots of forum searching, didn’t get anywhere

No dice:

tell application "Finder"
	set thefileName to name of selection
end tell
return thefileName

How would I get the (text of) the name of a selected file? Isn’t the Finder the App that would deliver this?

Model: 15" Alum
AppleScript: 1.10.3
Browser: Safari 416.12
Operating System: Mac OS X (10.4)


tell application "Finder" to set N to name of (selection as alias)

[EDIT: I should have mentioned that this form only works for a single selection.

Remember that Finder’s selection is a list of items; You have to handle it differently if you’re only want one item.

or
for single items

tell application "Finder"
	set thefile to name of (info for (selection) as alias)
	
end tell
return thefile

multi items

tell application "Finder"
	set thefile to selection
	repeat with i from 1 to number of items in thefile
		set this_item to name of (info for (item i of thefile) as alias)
	end repeat
end tell
return this_item

At least in Tiger, Mark, in the Finder, you don’t need ‘info for’ to get the name of the selection.

If the selection involves multiple files (or folders), this works without ‘info for’ as well:


set selections to {}
tell application "Finder"
	set thefiles to selection
	repeat with i in thefiles
		set end of selections to name of (i as alias)
	end repeat
end tell
selections

Contrast that with this (where you do need ‘info for’:


set selections to {}
tell application "Finder" to set s to selection
repeat with i in s
	set end of selections to name of (info for (i as alias))
end repeat

In case there’s any doubt, you shouldn’t need it in previous OS versions, either. In fact, pre-OS X, Finder’s selection property was even more accommodating ” to the extent that something like this would return either a name or a list of names (depending on how many items were selected):

tell application "Finder" to name of selection

(While this doesn’t currently work, my hope is still that it will be fixed in the next major upgrade…)

A minor point but, if you’re using Finder anyway, then you shouldn’t need the coercion to alias, either.
:slight_smile:

The reason I used the info one is because I never had any consistency doing it any other way, but I suspect thats down to me.

Actually if you do

tell application "Finder"
	set thefile to name of ((selection) as alias)
	
end tell
return thefile

It works,

where as

tell application "Finder"
	set thefile to name of (selection) 
	
end tell
return thefile

does not.
But in

tell application "Finder"
	set thefile to selection
	repeat with i from 1 to number of items in thefile
		set this_item to name of (item i of thefile)
	end repeat
end tell
return this_item

its ok I suspect because its been coerced into an item in " i " as an alias

The problem is that, at present, Finder’s selection doesn’t work as it used to (that is, in the way I described earlier) ” or (IMO) as it should.

A coercion to alias will work only if the selection is a single-item list (because AppleScript supports coercion of a single-item list to any value class to which the item could be coerced if it was not part of a list). However, if the selection is known to be a single-item list, coercion to alias is still unnecessary ” since there’s a more direct (read: faster) way to extract the item’s name property:

tell application "Finder" to name of item 1 of (get selection)

That apart, iterating through the selection’s items currently seems to to be the only way to collectively coerce their class or to extract some property or other. Note, though, that no implicit coercions to alias occur here ” only explicit ones. Properties are accessible in a repeat loop simply because each item (initially a Finder reference) is queried individually. (It should be/was possible to retrieve such information directly from every item of the selection, but there ya go…)

So, unless the intention is to take the list elsewhere (such as to another application, which wouldn’t understand a Finder reference), coercion to alias shouldn’t generally be required.

Thanks for that…

I hope it gets changed back in leopard it is a real pain in the A…