Re: count of Finder windows: Is this a bug?
Re: count of Finder windows: Is this a bug?
- Subject: Re: count of Finder windows: Is this a bug?
- From: Axel Luttgens <email@hidden>
- Date: Sun, 21 Nov 2010 00:08:43 +0100
Le 18 nov. 2010 à 21:04, Gil Dawson a écrit :
> Thanks to Jim Matthews of FetchSoftWorks for helping me isolate this one.
>
> This script:
>
> tell application "Finder"
> log "count of Finder windows=" & (count of Finder windows)
> repeat with w in Finder windows
> log "name of w=" & name of w
> end repeat
> end tell
>
> Produces nonsense in the log:
>
> (*count of Finder windows=3*)
> (*name of w=ASEDitor Recordings.scptFourth & Pyratania.png*)
> (*name of w=BU G3 15GBAppleScript Editor.appGalveston5Nov2010 1.png*)
> (*name of w=BU MBProHD 306GBBackUpAndReplaceSection.scptVegasRodeo.JPG*)
> (*name of w=MBProHDBlanken->iCalMCCVin.appTehachapi Loop*)
> (*name of w=Scratch 120Old stuffWaveland.png*)
> Result: error
> "Can’t make name of item 6 of every «class brow» of application \"Finder\" into type Unicode text." number -1700 from name of item 6 of every «class brow» to Unicode text
>
> 1) It's the names of a scrambled list of some items, but not windows.
> 2) Item 6? There are only 3 Finder windows open.
> (*count of Finder windows=3*)
>
>
> However, this script:
>
> tell application "Finder"
> log "count of Finder windows=" & (count of Finder windows)
> set fws to Finder windows
> repeat with w in fws
> log "name of w=" & name of w
> end repeat
> end tell
>
> Produces what I expected:
>
> count of Finder windows=3
> name of w=MBPro
> name of w=AppleScripts
> name of w=2010
>
> So, is this a bug or some nuance of syntax that I have missed?
>
> --Gil
Hello Gil,
Looks like there are slight bugs at work, leading to syntactical ambiguities.
In the following, I'll assume that two Finder windows are open, say on the startup disk and on the "Users" folder. It's interesting to run the snippets while looking at the output they produce in the events history.
It appears that Finder windows have items (in the sense of Finder items: files, folders...) as elements:
tell application "Finder"
-- Get items displayed in the first window:
items of first Finder window
-- Get items displayed in the second window:
items of second Finder window
-- Get items displayed in both windows:
items of Finder windows
end tell
Note that the third command acts upon all windows collectively, and thus returns a flat list of all displayed Finder items, not a list of lists of Finder items.
The Finder's dictionary doesn't state that windows have files, folders... as elements, and this could be viewed as a bug, at least a documentation bug.
One has to resort to the Finder's Scripting Guide (1994) for a quick mention: "If the window is a container window, its elements include all the items currently displayed inside the window".
But it could also be argued that the handling of window items isn't fully consistent:
tell application "Finder"
-- Get first item displayed in each window:
item 1 of Finder windows
end tell
That is, one doesn't get the first item of above flat list: the "item i of ..." reference form applies to each window individually.
Yet, that kind of mapping is more typical of the Finder's usual behavior.
Anyway, since Finder windows have elements, on should be able to count those elements; but there is "count" and "count":
tell application "Finder"
-- Counting windows with AppleScript's count command:
count Finder windows
--> 2
-- Counting windows with Finder's count command:
count (a reference to Finder windows)
--> 8
-- Another way to invoke Finder's count command:
count Finder windows each item
--> 8
end tell
The count of open windows on the one hand and the number of Finder items displayed in open windows on the other...
And we now have the explanation; a "repeat with loopVariable (in list)" loop, as in:
tell application "Finder"
repeat with W in Finder windows
name of W
end repeat
end tell
in fact first counts items then makes use of consecutive references of the form "item i of list". Thus, one isn't working on windows but on the Finder items displayed in those windows, moreover with two differing meanings given to "item" (a collective one and a "mapped" one):
tell application "Finder"
count every Finder window
--> 8
get name of item 1 of every Finder window
--> {"Shared", "Applications"}
get name of item 2 of every Finder window
--> {"luttgens", "Developer"}
get name of item 3 of every Finder window
--> error number -1728 from item 3 of every Finder window
Syntactically, you were perfectly correct.
But should you have explicitly warned that Finders windows have elements, you sure would have been very cautious wrt the usage of a "repeat with loopVariable (in list)" loop.
And Jim's proposal precisely avoids those ambiguities around that "item" keyword which belongs to AppleScript but also is the name of a ubiquitous Finder class.
As a side note, if you were only interested in the name of open windows, it may also prove useful to make use of Finder's usual behavior, as in:
tell application "Finder"
name of Finder windows
--> {"Users", "Boot"}
end tell
or some variant of such code.
HTH,
Axel
_______________________________________________
Do not post admin requests to the list. They will be ignored.
AppleScript-Users mailing list (email@hidden)
Help/Unsubscribe/Update your Subscription:
Archives: http://lists.apple.com/archives/applescript-users
This email sent to email@hidden