Is this item either a list or an NSList?

Is there a better method for determining if some variable contains either a list or an NSList? I have the bodge version sorted.

on Class_Of_Value_Is_Array(theValue)
	try
		if class of theValue is in {list} then return true
		return theValue's isKindOfClass:(current application's NSArray)
	on error e number n
		if n is -1708 then --e is "<yourArgument> doesn’t understand the “isKindOfClass_” message." i.e. It is not an NS Object
			return false
		else
			error e number n
		end if
	end try
end Class_Of_Value_Is_Array

How about a handler that tries to assign a list or NSArray, either of which would be true, and any other data class would be false?

use framework "Foundation"
use scripting additions

property ca : current application

set a to {""} as record
set b to ""
set _c to {""}
set d to ca's NSArray's arrayWithArray:["X", "Y", "Z"]
set e to 1 as integer
set f to 1.5 as number

display dialog "Passed record: " & my passed_array_test(a) as text
display dialog "Passed text: " & my passed_array_test(b) as text
display dialog "Passed list: " & my passed_array_test(_c) as text
display dialog "Passed NSArray: " & my passed_array_test(d) as text
display dialog "Passed Integer: " & my passed_array_test(e) as text
display dialog "Passed Number: " & my passed_array_test(f) as text
return

on passed_array_test(avalue)
	try
		ca's NSArray's alloc()'s initWithArray:avalue
		return true
	on error
		return false
	end try
end passed_array_test

Thanks VikingOSX. This method is better than my posted method, if for no other reason, for simplicity and brevity. I want to be able to directly compare Classes for AppleScript and NS objects, but that isn’t simple.

As you were only checking in your example code for an AppleScript list or an NSArray, any other class data assigned to an NSArray would produce false which is what I wrote. Attempting to test for AppleScript and Objective-C classes in a generic way, in my opinion, will drive you to create complicated logic in your code.

In many of my handlers I allow a single argument or lists of arguments. These arguments may be AppleScript lists or NSLists, and I have to determine which in order to iterate through them properly. Your method tests for this class, Arrays, nicely.

If I want the flexibility, I have to put the complexity somewhere. Thanks for your suggestion.

I think your original method is perfectly fine.

How about something like this?

use framework "Foundation"

on Class_Of_Value_Is_Array(theValue)
	tell current application's NSArray
		return (its arrayWithArray:{theValue})'s firstObject()'s isKindOfClass:(it)
	end tell
end Class_Of_Value_Is_Array

Nigel,

Terse and compact. Doesn’t rely on erroring. I do like this code!

Now I’m trying to apply this non-erroring structure to identifying NSArrays specifically. Not successfully, but trying. I’m not even sure that this is possible.

on Class_Of_Value_Is_NSArray(theValue)
	tell current application's NSArray
		try
			return (theValue's isKindOfClass:(it))
		on error
			return false
		end try
	end tell
end Class_Of_Value_Is_NSArray

The idea of getting equivalent ASObjC value(s) by putting the originals in a list and creating an NSArray from it is from Shane’s book, I think. But I don’t remember the context off hand.

use framework "Foundation"

on Class_Of_Value_Is_Array(theValue)
	tell current application's NSArray
		set ObjCValue to (its arrayWithArray:{theValue})'s firstObject()
		return ((ObjCValue's isKindOfClass:(it)) and (ObjCValue = theValue))
	end tell
end Class_Of_Value_Is_Array
1 Like

Well. That’s an interesting construct. I have some research to do. Thanks!

It’s basically the same as the earlier one with the added condition that the input object has to be equal to the one that’s just been shown to be an NSArray. :slight_smile: