Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

I am writing an app in Swift on OS X. Is there a way to get the current playing audio in iTunes? I would like to get details such as title, artist, etc.

What I've found already:

  1. Here is an Objective C example, but not all those methods are available with Swift.

  2. This example uses AppleScript, which I might have to do. But I'd rather use Swift if possible.

like image 869
quemeful Avatar asked May 17 '16 20:05

quemeful


People also ask

What is metadata in iTunes?

What is metadata? Metadata refers to a playlist's title information, artists, genre, original release date, release date, and much more. Your music must be delivered with metadata to display correctly in Apple Music and the iTunes Store.


1 Answers

Here's a simple example how to get the properties from the current track:

Tested on (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
        }
    }
}
like image 61
jackjr300 Avatar answered Oct 20 '22 00:10

jackjr300