Error in using "is in" with list of Finder object specifiers

Hello all,

I want to see if a file is in a list of files.

Since the list is the result of a Finder “get selection”, it is a list of Finder object specifiers.

I set the variable to another file specification.

It compiles fine, and the class of the variable is “document file”. The class of the list is “list” as expected.

Here is the test app I wrote:


tell application "Finder"
	set thefiles to (get selection)
	set a to (document file "Cookies.plist" of folder "Desktop" of folder "holbrook" of folder "Users" of disk "Grumpy")
	a is in thefiles --> "Finder got an error: unknown object type."
end tell

Anybody seen this before?

If I change the last line to


	(a as file specification) is in thefiles --> No error, but result always "False"
end tell

Then it runs, but even though Script Debugger shows both a and the selection to have the exact same Finder file object, the “is in” operator returns False.

a is a document file, and thefiles is a list of document files.

What’s causing this runtime error? Anyone know?

Thanks a lot for any advice.
Cheers

I usually go after that sort of info this way:


tell application "Finder" to set found to exists ((get selection as text) & "Epi-2.gif")

Hi Johnny,

if you want to compare a list, the operator must also be a list.
(In many cases AppleScript does this coercion automatically)

tell application "Finder"
	set thefiles to (get selection)
	set a to (document file "Cookies.plist" of folder "Desktop" of folder "holbrook" of folder "Users" of disk "Grumpy")
	{a} is in thefiles
end tell

Thanks so much for the tips. The {a} syntax does indeed work, but when I try and extrapolate it back to my original app, then I am getting no matches.

If I may, let me go ahead and post the whole script, which is pretty short.

The comparison always comes up “false”.


-- Script to invert the Finder selection

tell application "Finder"
	set {theSelection, selectedCount} to {get selection, count (get selection)}
	
	if selectedCount is 0 then return
	if selectedCount is 1 then set theSelection to theSelection as list
	set allFiles to (items of (insertion location as alias)) -- enclosing folder
	
	set inverselist to {} -- Make a list of the unselected items
	
	repeat with theItem in allFiles
		if {theItem} is not in theSelection then --> always returns "false"
			set end of inverselist to theItem -- Add unselected item to list
		end if
	end repeat
	set the selection to the inverselist -- Select all the items not in the orig selection
end tell

Thanks again for taking a look at this.

Hi Johnny,

the magic word is contents of

tell application "Finder"
	tell (get selection) to set {theSelection, selectedCount} to {it, count it}
	if selectedCount is 0 then return
	if selectedCount is 1 then set theSelection to theSelection as list
	set allFiles to (items of (insertion location as alias)) -- enclosing folder
	
	set inverselist to {} -- Make a list of the unselected items
	
	repeat with theItem in allFiles
		if {contents of theItem} is not in theSelection then --> always returns "false"
			set end of inverselist to theItem -- Add unselected item to list
		end if
	end repeat
	set the selection to the inverselist -- Select all the items not in the orig selection
end tell

Much appreciated, Stefan.

I did notice that the “theItem” object in Script Debugger had a “contents” element, but wasn’t sure I had ever noticed that before.

Now when one writes {x}, I think that is not equivalent to writing “x as list”, but I am not sure I grok the distinction.

Thanks again. It runs perfectly now. Of course I will not be able to resist trying to further optimize it somehow.

the index variable of a repeat block is e.g. item 5 of {1, 2, 3, 4, 5}
not 5
so in some cases like this or if you want to change the value directly you need to use contents of

Ahh… got it. Thanks again. I was thinking it was the fact that it was an index variable.

So I could code

repeat with i from 1 to count allFiles
if (item i of allFiles) is not in theSelection…


end repeat

You can, the result is the same, but I prefer always the repeat with anItem in aList form
unless I need access to the number of the index variable