This is a very basic question that may have been answered in a previous post, but I have been unable to find an answer.
For the sake of this discussion (although it won’t necessarily be true during normal usage), let’s say that every item of a desktop folder named “TestFolder” is selected. The following two commands seem to yield an identical result list of file references:
-- Version 1 works:
tell application "Finder" to get every item of folder "TestFolder" of desktop
-- Version 2 works also:
tell application "Finder" to get selection -- again, assuming that every item of "TestFolder" is selected
Despite the seemingly identical results of those two commands, I’ve noticed the following discrepancy in trying to get a list of the names of those items:
-- Version 1 works:
tell application "Finder" to get name of every item of folder "TestFolder" of desktop
-- Version 2 doesn't work:
tell application "Finder" to get name of selection
If there were a property that identified the selection status of items, then one could imagine getting the desired result with the following command:
-- Doesn't work because property "selection status" doesn't exist
tell application "Finder" to get name of every item of folder "TestFolder" of desktop whose selection status is true
However, no such property identifying selection status exists to my knowledge. My question is: Is there some way of getting the name of every selected item without having to resort to a repeat loop?
tell application "Finder"
set selectedItems to selection
return (name of item 1 of selectedItems)
--> foo.txt
return (name of item 2 of selectedItems)
--> bar.txt
end tell
When you ask the Finder for the selection, it returns an array of items (even if that array contains only one item). When you go to manipulate that data, you need to point to the item of the array you want.
Thank you for responding. I understand that the name of individual items can be gotten by specifying those individual items (“name of item1”, “name of item 4”, etc) from the list of file references. But the goal is to get the name of every selected item in a single command. The name of every item can be gotten with the following single command:
-- This works:
tell application "Finder" to get name of every item of folder "TestFolder" of desktop
What I am looking for would be something like the following, which don’t work:
-- The followin commands don't work because they are syntactically incorrect:
tell application "Finder" to get name of every selected item of folder "TestFolder" of desktop
-- or --
tell application "Finder" to get name of every item of folder "TestFolder" of desktop whose selection status is true
Is there a way to get the name of every selected item using a single command?
Not with OS X I’m afraid, though it used to be possible before that. It’s no longer possible to extend the Finder’s ‘selection’ reference to particular items in it or to their properties. The only thing you can do now is to ‘get’ the selection and then pick around in the list it returns.
Technically, this is exactly what happens when you receive the data from the Finder. But you have multiple items, or the possibility for them, so it has to return an array. You get all the items in one command, but you still need to tell the computer which item you want to manipulate.