Skip to content

Instantly share code, notes, and snippets.

@silasjmatson
Last active January 2, 2018 21:42
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save silasjmatson/281e0de082990fea2169 to your computer and use it in GitHub Desktop.
Save silasjmatson/281e0de082990fea2169 to your computer and use it in GitHub Desktop.
Kill iTunes whenever it opens
//
// 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