Get audio metadata from current audio playback in iTunes on OS X

I am writing an application in Swift on OS X. Is there a way to get the current sound in iTunes? I would like details like title, artist, etc.

What I already found:

+4
source share
1 answer

Here is a simple example of how to get properties from the current track:

Tested ( El Capitan v10.11.5, iTunes 12.4 and Xcode 7.3 ).

import Cocoa
import ScriptingBridge
@objc protocol iTunesApplication {
    optional func currentTrack()-> AnyObject
    optional var properties: NSDictionary {get}
    //if you need another object or method from the iTunes.h, you must add it here
}

@NSApplicationMain
class AppDelegate: NSObject, NSApplicationDelegate {

    func applicationDidFinishLaunching(aNotification: NSNotification) {
        let iTunesApp: AnyObject = SBApplication(bundleIdentifier: "com.apple.iTunes")!
        let trackDict = iTunesApp.currentTrack!().properties as Dictionary
        if (trackDict["name"] != nil) {// if nil then no current track
            print(trackDict["name"]!) // print the title
            print(trackDict["artist"]!)
            print(trackDict["album"]!)
            print(trackDict["playedCount"]!)
            // print(trackDict) // print the dictionary
        }
    }
}
+5
source

All Articles