Accessing SBElementArray content from Scripting-Bridge in swift

382 views Asked by At

I've been trying to use, in a swift code, the various SBElementArray generators defined in the iTunes.h ScriptingBridge header, for example:

  • List of playlists: (SBElementArray<iTunesPlaylist *> *) playlists;
  • List of artworks associated to a track: (SBElementArray<iTunesArtwork *> *) artworks;

But when i try to use a method associated to the type contained in those array:

let playlists: SBElementArray = iTunes.playlists()
if let playlist = playlists[0] as? iTunesPlaylist {
    print(playlist.name)
}

I get a compile error:

Undefined symbols for architecture x86_64:
 "_OBJC_CLASS_$_iTunesPlaylist", referenced from:
  objc-class-ref in AppDelegate.o
ld: symbol(s) not found for architecture x86_64
clang: error: linker command failed with exit code 1 (use -v to see invocation)

This seems to be limited to the SBElementArray as I have no problem accessing current track name with the following :

let track: iTunesTrack = iTunes.currentTrack;
print(track.name)

I'm also guessing that it has something to do with the type casting I'm trying to do from 'anyObject' to 'iTunesPlaylist' in my code (which i think i need to be bale to access the playlist content or whatever artwork i would like to display), because the following code:

let playlists: SBElementArray = iTunes.playlists()
print(playlists[0])
print(type(of: playlists[0]))

corectly returns:

<ITunesPlaylist @0x6080000402d0: ITunesPlaylist 0 of application "iTunes" (93931)>
ITunesPlaylist
1

There are 1 answers

0
Conor On

The downcasting is trying to create an optional and initialize an iTunesPlaylist that is not allowed and not present in the linker. Force it as you always know it to be a iTunesPlaylist or check the type.

if playlists.count > 0 {
    let playlist : = playlists[0]
    if playlist is iTunesPlaylist {
        print(playlist.name)
    }
}