Skip to content

Instantly share code, notes, and snippets.

@SLboat
Forked from bjhomer/currentTrack.swift
Created May 16, 2019 22:33
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save SLboat/ab7acd970b25923a0c0856b60f1676b5 to your computer and use it in GitHub Desktop.
Save SLboat/ab7acd970b25923a0c0856b60f1676b5 to your computer and use it in GitHub Desktop.
Using ScriptingBridge from Swift.
#! /usr/bin/swift
import ScriptingBridge
@objc protocol iTunesTrack {
optional var name: String {get}
optional var album: String {get}
}
@objc protocol iTunesApplication {
optional var soundVolume: Int {get}
optional var currentTrack: iTunesTrack? {get}
}
extension SBApplication : iTunesApplication {}
let app: iTunesApplication = SBApplication(bundleIdentifier: "com.apple.iTunes")
// Because these are all optional properties (to avoid providing an implementation), we have
// to use '!' to indicate we know the implementation exists.
let track: iTunesTrack? = app.currentTrack!
let album = track?.album!
let trackName = track?.name!
println("Current track: \(trackName) - \(album)")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment