Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Opens Musixmatch website searching for the current iTunes track
#! /usr/bin/swift
import ScriptingBridge
import AppKit
@objc protocol iTunesTrack {
optional var artist: String {get}
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 artist = track?.artist!
let album = track?.album!
let trackName = track?.name!
let music = (artist ?? "") + " - " + (trackName ?? "")
let encoded_music = music.stringByAddingPercentEncodingWithAllowedCharacters(.URLHostAllowedCharacterSet()) ?? ""
let url = "https://www.musixmatch.com/search/" + encoded_music
print("Current track: \(music)")
print("Opening: \(url)")
NSWorkspace.sharedWorkspace().openURL(NSURL(string: url)!)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.