diff --git a/Carol.xcodeproj/project.pbxproj b/Carol.xcodeproj/project.pbxproj index 468b007..b9f5dfa 100644 --- a/Carol.xcodeproj/project.pbxproj +++ b/Carol.xcodeproj/project.pbxproj @@ -20,6 +20,7 @@ A77750F22354ECAE000874C4 /* GetCurrentTrack.txt in Resources */ = {isa = PBXBuildFile; fileRef = A77750F12354ECAE000874C4 /* GetCurrentTrack.txt */; }; A77750F723578D99000874C4 /* ScriptExecutor.swift in Sources */ = {isa = PBXBuildFile; fileRef = A77750F623578D99000874C4 /* ScriptExecutor.swift */; }; A77750FA235790A1000874C4 /* ReadFileContent.swift in Sources */ = {isa = PBXBuildFile; fileRef = A77750F9235790A1000874C4 /* ReadFileContent.swift */; }; + A784050C2387D7F300ABF842 /* GetCurrentTrackFromMusic.txt in Resources */ = {isa = PBXBuildFile; fileRef = A784050B2387D7F300ABF842 /* GetCurrentTrackFromMusic.txt */; }; A79759BD2374975A009EAFB9 /* IllustratedView.swift in Sources */ = {isa = PBXBuildFile; fileRef = A79759BC2374975A009EAFB9 /* IllustratedView.swift */; }; A7983BDC23734F8200E6B5C5 /* States.swift in Sources */ = {isa = PBXBuildFile; fileRef = A7983BDB23734F8200E6B5C5 /* States.swift */; }; A7E3F4EE23709EA800892124 /* LyricsFinder.swift in Sources */ = {isa = PBXBuildFile; fileRef = A7E3F4ED23709EA800892124 /* LyricsFinder.swift */; }; @@ -55,6 +56,7 @@ A77750F12354ECAE000874C4 /* GetCurrentTrack.txt */ = {isa = PBXFileReference; lastKnownFileType = text; path = GetCurrentTrack.txt; sourceTree = ""; }; A77750F623578D99000874C4 /* ScriptExecutor.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ScriptExecutor.swift; sourceTree = ""; }; A77750F9235790A1000874C4 /* ReadFileContent.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ReadFileContent.swift; sourceTree = ""; }; + A784050B2387D7F300ABF842 /* GetCurrentTrackFromMusic.txt */ = {isa = PBXFileReference; lastKnownFileType = text; path = GetCurrentTrackFromMusic.txt; sourceTree = ""; }; A79759BC2374975A009EAFB9 /* IllustratedView.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = IllustratedView.swift; sourceTree = ""; }; A7983BDB23734F8200E6B5C5 /* States.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = States.swift; sourceTree = ""; }; A7E3F4ED23709EA800892124 /* LyricsFinder.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = LyricsFinder.swift; sourceTree = ""; }; @@ -169,6 +171,7 @@ isa = PBXGroup; children = ( A77750F12354ECAE000874C4 /* GetCurrentTrack.txt */, + A784050B2387D7F300ABF842 /* GetCurrentTrackFromMusic.txt */, A7EEFD4B2369ED14004EE0E7 /* GetAlbumArtSpotify.txt */, A7EEFD4D2369ED37004EE0E7 /* GetAlbumArtMusic.txt */, ); @@ -280,6 +283,7 @@ A7EEFD4E2369ED37004EE0E7 /* GetAlbumArtMusic.txt in Resources */, A73A826E2352672C00EA8A40 /* Preview Assets.xcassets in Resources */, A77750F22354ECAE000874C4 /* GetCurrentTrack.txt in Resources */, + A784050C2387D7F300ABF842 /* GetCurrentTrackFromMusic.txt in Resources */, A7EEFD4C2369ED14004EE0E7 /* GetAlbumArtSpotify.txt in Resources */, A73A826B2352672C00EA8A40 /* Assets.xcassets in Resources */, ); diff --git a/Carol/AppDelegate.swift b/Carol/AppDelegate.swift index 80e391f..cb4a884 100644 --- a/Carol/AppDelegate.swift +++ b/Carol/AppDelegate.swift @@ -17,6 +17,7 @@ class AppDelegate: NSObject, NSApplicationDelegate func applicationDidFinishLaunching(_ aNotification: Notification) { + // Create the SwiftUI view that provides the window contents. let contentView = ContentView() diff --git a/Carol/Scripts/GetCurrentTrackFromMusic.txt b/Carol/Scripts/GetCurrentTrackFromMusic.txt new file mode 100644 index 0000000..baecba7 --- /dev/null +++ b/Carol/Scripts/GetCurrentTrackFromMusic.txt @@ -0,0 +1,17 @@ +if application "Music" is running + tell application "Music" + if player state is stopped then + return "1" + else + set track_artist to artist of current track + set track_name to name of current track + set state to "" + if player state is paused then + set state to "(Paused)" + end if + return {track_artist, track_name, "Music"} + end if + end tell +else + return "2" +end if diff --git a/Carol/ViewModels/TrackViewModel.swift b/Carol/ViewModels/TrackViewModel.swift index cdcefed..b5cf26a 100644 --- a/Carol/ViewModels/TrackViewModel.swift +++ b/Carol/ViewModels/TrackViewModel.swift @@ -21,6 +21,7 @@ class TrackViewModel: ObservableObject @Published var state: States private var lyricsFinder: LyricsFinder private let apiKey: String + private let spotifyInstalled: Bool enum LyricsService { case LyricsOvh @@ -40,6 +41,7 @@ class TrackViewModel: ObservableObject state = States.loading lyricsFinder = LyricsFinder() apiKey = SecretsReader.shared.getSecretKeys() + spotifyInstalled = (NSWorkspace.shared.urlForApplication(withBundleIdentifier: "com.spotify.client") != nil) settingsMenu.addItem(withTitle: "About", action: #selector(aboutMenuItemClicked), keyEquivalent: "").target = self settingsMenu.addItem(NSMenuItem.separator()) settingsMenu.addItem(withTitle: "Quit", action: #selector(quitMenuItemClicked), keyEquivalent: "q").target = self @@ -80,7 +82,14 @@ class TrackViewModel: ObservableObject private func setTrack() { state = States.loading - executedTrackScript.executeScript("GetCurrentTrack") + if spotifyInstalled + { + executedTrackScript.executeScript("GetCurrentTrack") + } + else + { + executedTrackScript.executeScript("GetCurrentTrackFromMusic") + } if executedTrackScript.result.numberOfItems == 3 { @@ -173,8 +182,15 @@ class TrackViewModel: ObservableObject case .success(let value): let json = JSON(value) let uneditedLyrics = json["message"]["body"]["lyrics"]["lyrics_body"].stringValue - self.state = States.content - self.track!.lyrics = String(uneditedLyrics.split(separator: String.Element("*"), maxSplits: 1, omittingEmptySubsequences: true)[0]) + if uneditedLyrics.isEmpty + { + self.state = States.empty + } + else + { + self.state = States.content + self.track!.lyrics = String(uneditedLyrics.split(separator: String.Element("*"), maxSplits: 1, omittingEmptySubsequences: true)[0]) + } case .failure(_): self.state = States.empty }