]>
Commit | Line | Data |
---|---|---|
d1a147d4 ER |
1 | import ScriptingBridge |
2 | ||
3 | // Protocol to obtain the track from Spotify | |
4 | @objc protocol SpotifyTrack { | |
5 | @objc optional var name: String {get} | |
6 | @objc optional var artist: String {get} | |
7 | } | |
8 | ||
9 | // Protocol to interact with Spotify | |
10 | @objc protocol SpotifyApplication { | |
11 | @objc optional var currentTrack: SpotifyTrack? {get} | |
12 | } | |
13 | ||
14 | extension SBApplication : SpotifyApplication {} | |
15 | ||
16 | // Source that reads track artist and name from current Spotify track | |
17 | class SpotifySource: Source { | |
18 | ||
19 | // Calls the spotify API and returns the current track | |
20 | var currentTrack: Track? { | |
21 | ||
22 | if let spotify: SpotifyApplication = SBApplication(bundleIdentifier: "com.spotify.client") { | |
23 | ||
24 | // Attempt to fetch the title from a song | |
25 | if let currentTrack = spotify.currentTrack { | |
26 | if let track = currentTrack { | |
27 | if let name = track.name { | |
28 | if let artist = track.artist { | |
29 | ||
30 | return Track(withName: name, andArtist: artist) | |
31 | } | |
32 | } | |
33 | } | |
34 | } | |
35 | } | |
36 | ||
37 | return nil | |
38 | } | |
39 | ||
40 | } | |
41 |