Re: Selected Finder items returned by index?
Re: Selected Finder items returned by index?
- Subject: Re: Selected Finder items returned by index?
- From: Nigel Garvey <email@hidden>
- Date: Mon, 15 Oct 2001 14:07:22 +0100
"Bob.Kalbaugh" wrote on Sun, 14 Oct 2001 22:05:20 -0500:
>
Can someone confirm this?
>
>
I've noticed that the Finder appears to return selected items by index. I'm
>
on OS 8.5.1, AS 1.3.4 With the root folder open and all items selected I
>
run...
>
>
tell application "Finder"
>
activate
>
set items_selected to (name of selection)
>
end tell
>
-->{"System Folder", "Utilities", "Font Library", "Etc..."}
>
>
If I randomly open some of the folders, close them, select all, and run the
>
script again I get different results.
Meaning, of course, the same results in a different order.
>
So it appears that the items are
>
returned by index, yes? BTW, for anybody listening - selecting edit-copy
>
from the Finder menu, yields the same results (clipboard).
That's right. The Finder has its own internal lists of what's selected,
what's open, etc. These are dynamic and the ordering and content of these
lists changes as items are opened, closed, deleted, and so on. A Finder
reference to such a list returns the current state of that list. It's
something you have to mind when using a repeat loop that deletes, closes,
or opens things. It's always safer to resolve an application list
reference to a static list in your script and then to loop through that.
(Sometimes, depending on the circumstances, there are other ways.)
>
I'd like to have them returned alphabetically.
That's easy if you want the names of *all* the (visible) items in a
folder.
list folder myFolderPath without invisibles
--> Alphabetical list of names
or:
tell application "Finder"
list folder folder myFolder of disk myDisk without invisibles
end tell
But you still have a problem if you only want the names of selected items.
>
The Finder's sort reference
>
by property command sorts correctly, but I can't seem to glean the name from
>
the result without adding a repeat loop. I've tried too many iterations to
>
list but one example along the lines of what I'm trying is:
>
>
set items_selected to (name of (sort selection by name))
>
>
most result in:
>
--> Can't get name of {folder "Applications" of startup disk of application
>
"Finder" ...} - is this not a reference?
'Sort' is a command - which produces its own result - so the reference
chain is broken at that point.
>
-- kludge 1 (slow) --
>
>
tell application "Finder"
>
activate
>
set itemList to {}
>
set itemSelection to (sort the selection by name)
>
repeat with i in itemSelection
>
copy (name of i) to end of itemList
>
end repeat
>
end tell
>
itemList
This is the only way that works for me at the moment. Here, you're
looping through the result of the 'sort' command. This is not a reference
and so should be safe. You can speed up the loop unnoticeably by changing
the 'copy' line to a 'set':
set end of itemList to name of i
>
--- kludge 2 (somewhat faster) ---
>
>
tell application "Finder"
>
activate
>
copy -- why this works alphab'y here, and not manually - ???
>
set itemList to (the clipboard as string) -- thanks N.G.!
>
end tell
>
set newList to (every paragraph of itemList) as list
I was quite excited by this at first; but as I began to fool around with
the selected items, I found the clipboard order began to change. It's not
a safe method.
NG