Get an `SBObject` instance's real type

84 views Asked by At

(This question is tagged c# because I'll be doing this from Xamarin, but this is more of a framework question; the language shouldn't matter.)

I'm trying to interact with Music (née iTunes) using Scripting Bridge. To that end, I can get the SBApplication and ask it for properties; for example, playerState will get me whether it's currently playing, and sources will basically get me the entire library, hierarchically.

But that's fairly low-level, so I dug into sdef and sdp to generate some XML and a header file, in order to write some matching wrapper classes that match those described.

The part where I get stuck is basically inheritance. The playlists property can contain different kinds of playlists (such as MusicPlaylist, MusicFolderPlaylist, etc., all of which inherit from MusicPlaylist), and I'm only interested in MusicUserPlaylist instances.

But I'm confused how to even ask an instance its type at all, much less efficiently. I see that debugDescription answer this, for example:

<SBObject @0x6000008bf180: <class 'cUsP'> id 28712
of <class 'cSrc'> id 63 of application "Music" (8034)>

And I can see from the sdef that cUsP is what I'm looking for:

<class name="user playlist" code="cUsP"
description="custom playlists created by the user"
inherits="playlist" plural="user playlists">
    …
</class>

But… am I really supposed to parse description for this? Surely there's a nicer way? I see that the constructor always takes an elementCode:

- (instancetype) initWithElementCode:(DescType)code
properties:(nullable NSDictionary<NSString *, id> *)properties data:(nullable id)data;

(I'm actually not a 100% on whether DescType is the same thing as e.g. class 'cUsP'?)

And yet, there doesn't appear to be a way to get that element code back?

TL;DR: given an SBElementArray that can contain various subclasses, how do I verify that an instance's dynamic scripting bridge type is a certain class?

0

There are 0 answers