Problem with classDescriptionForClass method

foundation
asobjc

#1

Shane,

At first I tried using classDescriptionForClass from the class “NSClassDescription” but it always returned nil.

The Apple docs said:
In macOS 10.6 and later, this method (and as a result classDescription methods of any object) will return nil when the sdef contains no element for the Cocoa class, but there is a element defined for a superclass.
This is incorrect, as object instances should never be required to be exactly a given class, any class should be allowed to be a subclass of the required class and receive the correct value.
This situation can have a serious impact on Cocoa Scripting, and there is no plan on changing this behavior.
Instead of using this method, you should use the classDescriptionForClass: method of NSScriptClassDescription instead.

So I tried using the NSScriptClassDescription class instead. The problem is no matter what class I give to the classDescriptionForClass method I always get exactly the same results. I tried a lot of things in foundation and all were the same. I tried passing the type by passing an object of the class I wanted info on, and I tried passing the class name as a string and still I got the same exact response.

I doesn’t make sense Apple would implement a method that always returns the same thing for everything so I figured I am doing something wrong, or the method doesn’t work.

set TheVar to current application's NSString's stringWithString:"Test"
set TheAnswer to current application's NSScriptClassDescription's classDescriptionForClass:TheVar
class of TheAnswer --> (Class) NSScriptClassDescription
TheAnswer --> 
(*
(NSScriptClassDescription) Class: class ('pcls'), superclass: item, hidden: no
    Implementation class: NSObject
    Name: "class", plural name: "classes", description: "A class"
    Attribute: className ('pnam'), type: text ('ctxt'), access: read-write, hidden: no
        Name: "name", description: "The title of the class."
    Default subcontainer: <none>
    Primitive type: <none>
*)

Do you know if classDescriptionForClass works and if so do you know what I am doing wrong?

Bill


(Mark Alldritt) #2

I think the problem is that you are passing in a string to classDescriptionForClass when it wants an Objective-C object class. My ASObjC is not good enough to explain how to accomplish this - we’ll have to wait for the earth to rotate enough that Shane wakes and can help us.


#3

Mark,

I just tried the script below and it still gave me the same response. But now that I think about NSString is an abstract class. Perhaps that has something to do with it.

use framework "Foundation"

set TheClass to current application's NSClassFromString("NSString")
class of TheClass --> (Class) NSString

current application's NSScriptClassDescription's classDescriptionForClass:TheClass

Bill


(Shane Stanley) #4

I’m not sure what you’re trying to achieve. When an app is scriptable, the sdef is used to create an NSScriptRegistry, and the NSScriptClassDescription class is used to store information about each class defined in the sdef. It’s not something you’d ever use from ASObjC – it’s only used in Objective-C to build containers in object specifiers.

You will get a different result if you use a class from the host app’s sdef, though. For example:

use framework "Foundation"
use framework "AppKit"

current application's NSScriptClassDescription's classDescriptionForClass:(current application's NSApplication)

It’s a sort of human-readable summary or description of what the object contains (hence the word description in the method name).

In terms of your database, though, NSScriptClassDescription probably best merits an NA.


#5

I started out with with trying to use classDescriptionForClass but it always returned nil. The documentation said it broke and to NSScriptClassDescription. Which I did but that didn’t work out well either.

In the beginning I wanted to see if classDescriptionForClass returned anything useful about the class.

Bill