Swift: Undefined Symbols: iTunesApplication

881 views Asked by At

I'm trying to create Swift OS X app now, and found difficulty using ScriptingBridge.

I included proper iTunes.h file, and Xcode is not giving any error when I wrote "iTunesApplication" as type.

However, when I compile(run) the app, it gives me error :( Does anybody know about this issue?

Undefined symbols for architecture x86_64:
"_OBJC_CLASS_$_iTunesApplication", referenced from:
__TFC12LoveYouChloe11AppDelegate10showWindowfS0_FPSs9AnyObject_T_ 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)

And here is my code:

var iTunes: iTunesApplication = SBApplication.applicationWithBundleIdentifier("com.apple.iTunes") as iTunesApplication
iTunes.playpause()
2

There are 2 answers

1
Garrett On

The best way to solve this is to take the generated Objective-C Scripting Bridge header and convert it into a native Swift. I wrote a Python script (here) that can do that for you. You can see my answer here for a better explanation of what exactly is going on if you're interested.

2
Pranav On

Instead of

var iTunes: iTunesApplication = SBApplication.applicationWithBundleIdentifier("com.apple.iTunes") 

use

var iTunes: AnyObject = SBApplication.applicationWithBundleIdentifier("com.apple.iTunes")

Sometimes the compiler gets confused when you are accessing two or more properties in a row(no idea why) so you may need to use a temporary variable. E.g.:

Instead of:

let songName: String = iTunes.playlists[0].songs[0].name

Try:

let song: AnyObject = iTunes.playlists[0].songs[0]
let songName = song.name

Or Alternatively the faster (this also works if you declare iTunes as an SBApplication) :

let songName: String = iTunes.valueAtIndex(0, inPropertyWithKey: "playlists").valueAtIndex(0, inPropertyWithKey: "songs").valueForKey("name")

EDIT: You can also generate the .swift headers as specified here: https://github.com/tingraldi/SwiftScripting