In order to store an object's values for external (outside AS) access, I need to be able to get every property of that object, and then I'd try to coerce it to text and store it somehwere.
How do I get the list of properties that an object holds. As an example, I can write this:
tell me
get properties
end tell
That works for a script object.
But for many other objects, I simply get an error such as "descripter type mismatch", like here:
tell application "iTunes"
get properties of file track 1
end tell
Now, I know the excellent Script Debugger can do it (it can show any object's entire set of properties), so it should be possible in written AppleScript as well. What's the secret to this?
There is a trick you can use, because you can force Applescript to tell you the error, and this text includes the properties of the object that was the target.
Script Debugger is Applescript, just with a bunch of programming tools placed around it. But a "descriptor type mismatch" really shouldn't enter into it. Can you show your code, because this works just fine in Script Editor:
Different applications will behave different, but without example code, there are too many variations to say definitively.
Update per comment and updated question: Again, different applications behave differently. An application actually has to have a
properties
property in order to get a record returned to you (though sometimes this is different from other information that can be gained from an object). Typically, this is implemented at a root class—item
in most cases; iTunes doesn't allow for this. Not even Script Debugger can get around that.The ability for an app to return a "properties" property has always existed, but it took considerable more work pre-Cocoa than after. Pre-Cocoa, the developer had to build an AEList structure populated with keys and values for each property, then return it in an typePropertyList descriptor. Many developers did not bother. With Cocoa Scripting, you basically get this for free AS LONG AS you use KVC-compliant names for all of the properties of your class AND you get the terminology and cocoa-keys in your SDEF file properly configured.
BTW, in 2016, iTunes 12.3.3,
correctly returns a long list of properties.
Mark Alldritt, the author of Script Debugger, was so kind to explain the "secret" to me.
Script Debugger uses some special AppleScript API functions, including OSAGetPropertyNames(), to get to this information.
Hence, if I write a wrapper in, for instance, C, I can probably get to this, too.
Update
The Cocoa Scripting API has a dedicated classes for this (
NSScriptSuiteRegistry
andNSScriptClassDescription
) - the framework builds this information from reading an app's scripting definition (.sdef) file. With that, all the available class and their properties can be learned quite easily.