Skip to content
This repository has been archived by the owner on Jan 16, 2020. It is now read-only.

Commit

Permalink
Add Sportify termination observer
Browse files Browse the repository at this point in the history
  • Loading branch information
simonmeusel committed Jul 19, 2019
1 parent 0dbe660 commit 35e2fab
Showing 1 changed file with 11 additions and 18 deletions.
29 changes: 11 additions & 18 deletions MuteSpotifyAds/SpotifyManager.swift
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
// MuteSpotifyAds
//
// Created by Simon Meusel on 29.05.18.
// Copyright © 2018 Simon Meusel. All rights reserved.
// Copyright © 2018 - 2019 Simon Meusel. All rights reserved.
//

import Cocoa
Expand Down Expand Up @@ -45,6 +45,16 @@ class SpotifyManager: NSObject {

super.init()

// Stop this application when Spotify gets closed
NSWorkspace.shared.notificationCenter.addObserver(forName: NSWorkspace.didTerminateApplicationNotification, object: nil, queue: nil, using: {
notification in

let app = notification.userInfo?[NSWorkspace.applicationUserInfoKey] as! NSRunningApplication
if (app.bundleIdentifier == "com.spotify.client") {
NSApplication.shared.terminate(self)
}
print(app.bundleIdentifier ?? "")
})

DispatchQueue.global(qos: .default).async {
self.startSpotify(foreground: true)
Expand Down Expand Up @@ -144,8 +154,6 @@ class SpotifyManager: NSObject {
* Returns true if spotify got muted or unmuted, false otherwise
*/
func trackChanged() -> Bool {
self.checkIfSpotifyIsRunning()

var changed = false

if isSpotifyAdPlaying() {
Expand Down Expand Up @@ -247,8 +255,6 @@ class SpotifyManager: NSObject {
* Runs the given apple script and passed logs to completion handler
*/
func runAppleScript(script: String) -> String {
self.checkIfSpotifyIsRunning()

let process = Process()
process.launchPath = "/usr/bin/osascript"
process.arguments = ["-e", script]
Expand All @@ -263,19 +269,6 @@ class SpotifyManager: NSObject {
return String(data: data, encoding: String.Encoding.utf8)!
}

/**
* Checks whether Spotify is running and terminates the application if it is closed
*/
func checkIfSpotifyIsRunning() {
if isRestarting {
return
}
let running = NSRunningApplication.runningApplications(withBundleIdentifier: "com.spotify.client").count != 0
if (!running) {
NSApplication.shared.terminate(self)
}
}

func toggleSpotifyPlayPause() {
_ = runAppleScript(script: SpotifyManager.appleScriptSpotifyPrefix + "playpause")
}
Expand Down

0 comments on commit 35e2fab

Please sign in to comment.