]> git.r.bdr.sh - rbdr/lyricli/blobdiff - Sources/lyricli/sources/itunes_source.swift
Add Catalina support, don't auto-open apps
[rbdr/lyricli] / Sources / lyricli / sources / itunes_source.swift
index 4e175c104c696c4fc5dd7c91c72b9d06926f9bb5..92b99695d81f590bbf22c9f117bc6e8f9ff976cf 100644 (file)
@@ -1,4 +1,5 @@
 import ScriptingBridge
+import Foundation
 
 // Protocol to obtain the track from iTunes
 @objc protocol iTunesTrack {
@@ -20,7 +21,12 @@ class ItunesSource: Source {
     // Calls the spotify API and returns the current track
     var currentTrack: Track? {
 
-        if let iTunes: iTunesApplication = SBApplication(bundleIdentifier: "com.apple.iTunes") {
+        if let iTunes: iTunesApplication = SBApplication(bundleIdentifier: bundleIdentifier) {
+            if let application = iTunes as? SBApplication {
+              if !application.isRunning {
+                return nil
+              }
+            }
 
             // Attempt to fetch the title from a stream
             if let currentStreamTitle = iTunes.currentStreamTitle {
@@ -58,4 +64,14 @@ class ItunesSource: Source {
         return nil
     }
 
+    private var bundleIdentifier: String {
+        if ProcessInfo().isOperatingSystemAtLeast(
+          OperatingSystemVersion(majorVersion: 10, minorVersion: 15, patchVersion: 0)
+          ) {
+          return "com.apple.Music"
+        }
+
+        return "com.apple.iTunes"
+    }
+
 }