Last active
January 2, 2018 21:42
-
-
Save silasjmatson/281e0de082990fea2169 to your computer and use it in GitHub Desktop.
Kill iTunes whenever it opens
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// | |
// AppDelegate.swift | |
// iTunesKiller | |
// | |
// Created by Silas J. Matson on 12/18/14. | |
// | |
import Cocoa | |
@NSApplicationMain | |
class AppDelegate: NSObject, NSApplicationDelegate { | |
@IBOutlet weak var window: NSWindow! | |
func applicationDidFinishLaunching(aNotification: NSNotification) { | |
NSWorkspace.sharedWorkspace().notificationCenter.addObserver(self, selector: "appLaunchedInSharedWorkspace:", name: NSWorkspaceDidLaunchApplicationNotification, object: nil); | |
} | |
func applicationWillTerminate(aNotification: NSNotification) { | |
NSWorkspace.sharedWorkspace().notificationCenter.removeObserver(self); | |
} | |
func appLaunchedInSharedWorkspace(notif: NSNotification!) { | |
var appName = notif.userInfo!["NSApplicationName"] as String; | |
if (appName == "iTunes") { | |
// Kill iTunes | |
var iTunesInstance = notif.userInfo!["NSWorkspaceApplicationKey"] as NSRunningApplication; | |
killProgram(iTunesInstance); | |
} | |
// We don't care | |
} | |
func killProgram(iTunesInstance: NSRunningApplication) { | |
if (iTunesInstance.terminate()) { | |
NSLog("Success! iTunes tried to be annoying and failed!"); | |
} else { | |
NSLog("iTunes doesn't want to be killed. Let's nuke it!"); | |
if (iTunesInstance.forceTerminate()) { | |
NSLog("Finally. Killed the sucker."); | |
} else { | |
NSLog("This iTunes crap is a zombie. Maybe try a `kill -9`?"); | |
} | |
} | |
} | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment