Skip to content

Commit

Permalink
Allow setting replacement for iTunes/Music (#26)
Browse files Browse the repository at this point in the history
* Allow setting replacement for iTunes/Music

* Tweaked Replacement Script To Run /usr/bin/open

Co-authored-by: Tom Taylor <[email protected]>
  • Loading branch information
SBell6hf and tombonez authored Jul 31, 2022
1 parent b4ec2df commit cea5d02
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 15 deletions.
15 changes: 15 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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).
43 changes: 28 additions & 15 deletions noTunes/AppDelegate.swift
Original file line number Diff line number Diff line change
Expand Up @@ -13,24 +13,24 @@ 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) {
defaults.set(true, forKey: "hideIcon")
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)
Expand All @@ -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!)
Expand All @@ -81,26 +81,39 @@ 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 {
if let processId = note.userInfo?["NSApplicationProcessIdentifier"] as? Int {
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()
}

}

0 comments on commit cea5d02

Please sign in to comment.