Skip to content

Commit

Permalink
Updated method to check if Spotify is installed or not
Browse files Browse the repository at this point in the history
  • Loading branch information
AnaghSharma committed Nov 22, 2019
1 parent b20605a commit f6b4962
Show file tree
Hide file tree
Showing 4 changed files with 41 additions and 3 deletions.
4 changes: 4 additions & 0 deletions Carol.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -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 */; };
Expand Down Expand Up @@ -55,6 +56,7 @@
A77750F12354ECAE000874C4 /* GetCurrentTrack.txt */ = {isa = PBXFileReference; lastKnownFileType = text; path = GetCurrentTrack.txt; sourceTree = "<group>"; };
A77750F623578D99000874C4 /* ScriptExecutor.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ScriptExecutor.swift; sourceTree = "<group>"; };
A77750F9235790A1000874C4 /* ReadFileContent.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ReadFileContent.swift; sourceTree = "<group>"; };
A784050B2387D7F300ABF842 /* GetCurrentTrackFromMusic.txt */ = {isa = PBXFileReference; lastKnownFileType = text; path = GetCurrentTrackFromMusic.txt; sourceTree = "<group>"; };
A79759BC2374975A009EAFB9 /* IllustratedView.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = IllustratedView.swift; sourceTree = "<group>"; };
A7983BDB23734F8200E6B5C5 /* States.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = States.swift; sourceTree = "<group>"; };
A7E3F4ED23709EA800892124 /* LyricsFinder.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = LyricsFinder.swift; sourceTree = "<group>"; };
Expand Down Expand Up @@ -169,6 +171,7 @@
isa = PBXGroup;
children = (
A77750F12354ECAE000874C4 /* GetCurrentTrack.txt */,
A784050B2387D7F300ABF842 /* GetCurrentTrackFromMusic.txt */,
A7EEFD4B2369ED14004EE0E7 /* GetAlbumArtSpotify.txt */,
A7EEFD4D2369ED37004EE0E7 /* GetAlbumArtMusic.txt */,
);
Expand Down Expand Up @@ -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 */,
);
Expand Down
1 change: 1 addition & 0 deletions Carol/AppDelegate.swift
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ class AppDelegate: NSObject, NSApplicationDelegate

func applicationDidFinishLaunching(_ aNotification: Notification)
{

// Create the SwiftUI view that provides the window contents.
let contentView = ContentView()

Expand Down
17 changes: 17 additions & 0 deletions Carol/Scripts/GetCurrentTrackFromMusic.txt
Original file line number Diff line number Diff line change
@@ -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
22 changes: 19 additions & 3 deletions Carol/ViewModels/TrackViewModel.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand Down Expand Up @@ -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
{
Expand Down Expand Up @@ -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
}
Expand Down

0 comments on commit f6b4962

Please sign in to comment.