From cea5d02c771085328bc5cc910f16a089b088d4f4 Mon Sep 17 00:00:00 2001 From: Sophie Bell <94227866+SBell6hf@users.noreply.github.com> Date: Sun, 31 Jul 2022 13:32:28 -0700 Subject: [PATCH] Allow setting replacement for iTunes/Music (#26) * Allow setting replacement for iTunes/Music * Tweaked Replacement Script To Run /usr/bin/open Co-authored-by: Tom Taylor --- README.md | 15 ++++++++++++++ noTunes/AppDelegate.swift | 43 +++++++++++++++++++++++++-------------- 2 files changed, 43 insertions(+), 15 deletions(-) diff --git a/README.md b/README.md index 027db89..61e2c5d 100644 --- a/README.md +++ b/README.md @@ -76,6 +76,21 @@ Quit the app via Activity Monitor or run the following command in Terminal: osascript -e 'quit app "noTunes"' ``` +### Set replacement for iTunes / Apple Music + +Replace `YOUR_MUSIC_APP` with the name of your music app in the following command. +```bash +defaults write digital.twisted.noTunes replacement /Applications/YOUR_MUSIC_APP.app +``` + +Then `/Applications/YOUR_MUSIC_APP.app` will launch when iTunes/Music attempts to launch. + +The following command will disable the replacement. + +```bash +defaults delete digital.twisted.noTunes replacement +``` + ## License The code is available under the [MIT License](https://github.com/tombonez/notunes/blob/master/LICENSE). diff --git a/noTunes/AppDelegate.swift b/noTunes/AppDelegate.swift index 4e06d0d..00dbf26 100644 --- a/noTunes/AppDelegate.swift +++ b/noTunes/AppDelegate.swift @@ -13,9 +13,9 @@ import ServiceManagement class AppDelegate: NSObject, NSApplicationDelegate { let defaults = UserDefaults.standard - + let statusItem = NSStatusBar.system.statusItem(withLength: NSStatusItem.variableLength) - + @IBOutlet weak var statusMenu: NSMenu! @IBAction func hideIconClicked(_ sender: NSMenuItem) { @@ -23,14 +23,14 @@ class AppDelegate: NSObject, NSApplicationDelegate { NSStatusBar.system.removeStatusItem(statusItem) self.appIsLaunched() } - + @IBAction func quitClicked(_ sender: NSMenuItem) { NSApplication.shared.terminate(self) } - + @objc func statusBarButtonClicked(sender: NSStatusBarButton) { let event = NSApp.currentEvent! - + if event.type == NSEvent.EventType.rightMouseUp { statusItem.menu = statusMenu statusItem.popUpMenu(statusMenu) @@ -44,33 +44,33 @@ class AppDelegate: NSObject, NSApplicationDelegate { } } } - + func applicationDidFinishLaunching(_ aNotification: Notification) { statusItem.image = NSImage(named: "StatusBarButtonImageActive") - + if let button = statusItem.button { button.action = #selector(self.statusBarButtonClicked(sender:)) button.sendAction(on: [.leftMouseUp, .rightMouseUp]) } - + if defaults.bool(forKey: "hideIcon") { NSStatusBar.system.removeStatusItem(statusItem) } - + self.appIsLaunched() self.createListener() } - + func createListener() { let workspaceNotificationCenter = NSWorkspace.shared.notificationCenter workspaceNotificationCenter.addObserver(self, selector: #selector(self.appWillLaunch(note:)), name: NSWorkspace.willLaunchApplicationNotification, 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.bundleIdentifier == "com.apple.iTunes") { self.terminateProcessWith(Int(runningApp.processIdentifier), runningApp.localizedName!) @@ -81,7 +81,7 @@ class AppDelegate: NSObject, NSApplicationDelegate { } } } - + @objc func appWillLaunch(note:Notification) { if statusItem.image == NSImage(named: "StatusBarButtonImageActive") || defaults.bool(forKey: "hideIcon") { if let processName:String = note.userInfo?["NSApplicationBundleIdentifier"] as? String { @@ -89,18 +89,31 @@ class AppDelegate: NSObject, NSApplicationDelegate { switch processName { case "com.apple.iTunes": self.terminateProcessWith(processId, processName) + self.launchReplacement() case "com.apple.Music": self.terminateProcessWith(processId, processName) + self.launchReplacement() default:break } } } } } - + + func launchReplacement() { + let replacement = defaults.string(forKey: "replacement"); + if (replacement != nil) { + let task = Process() + + task.arguments = [replacement!]; + task.launchPath = "/usr/bin/open" + task.launch() + } + } + func terminateProcessWith(_ processId:Int,_ processName:String) { let process = NSRunningApplication.init(processIdentifier: pid_t(processId)) process?.forceTerminate() } - + }