Skip to content

Commit

Permalink
iTunes will know quit on launch and toggling states
Browse files Browse the repository at this point in the history
  • Loading branch information
tombonez committed Jan 5, 2017
1 parent 4c8255d commit 49604c7
Showing 1 changed file with 20 additions and 5 deletions.
25 changes: 20 additions & 5 deletions noTunes/AppDelegate.swift
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ class AppDelegate: NSObject, NSApplicationDelegate {
statusItem.menu = nil
} else {
if statusItem.image == NSImage(named: "StatusBarButtonImage") {
self.appIsLaunched()
statusItem.image = NSImage(named: "StatusBarButtonImageActive")
} else {
statusItem.image = NSImage(named: "StatusBarButtonImage")
Expand All @@ -43,22 +44,36 @@ class AppDelegate: NSObject, NSApplicationDelegate {
button.sendAction(on: [.leftMouseUp, .rightMouseUp])
}

self.createListeners()
self.appIsLaunched()
self.createListener()
}

func createListeners() {
func createListener() {
let workspaceNotificationCenter = NSWorkspace.shared().notificationCenter
workspaceNotificationCenter.addObserver(self, selector: #selector(self.appWillLaunch(note:)), name: .NSWorkspaceWillLaunchApplication, object: nil)
}

func appIsLaunched() {
let apps = NSWorkspace.shared().runningApplications
for currentApp in apps.enumerated() {
let runningApp = apps[currentApp.offset]

if(runningApp.activationPolicy == .regular) {
if(runningApp.localizedName == "iTunes") {
self.terminateProcessWith(Int(runningApp.processIdentifier), runningApp.localizedName!)
}
}
}
}

func appWillLaunch(note:Notification) {
if statusItem.image == NSImage(named: "StatusBarButtonImageActive") {
if let processName:String = note.userInfo?["NSApplicationName"] as? String {
if let processId = note.userInfo?["NSApplicationProcessIdentifier"] as? Int {
switch processName {
case "iTunes":
self.terminateProcessWith(processId,processName)
default:break
case "iTunes":
self.terminateProcessWith(processId, processName)
default:break
}
}
}
Expand Down

0 comments on commit 49604c7

Please sign in to comment.