equaliser icon indicating copy to clipboard operation
equaliser copied to clipboard

A Way to Switch Presets via the Command Line

Open rohdea0826 opened this issue 1 month ago • 0 comments

To automatically switch EQ presets at night, I implemented a custom URL scheme for macOS with the help of AI. I trigger this command remotely through Home Assistant.

You can switch to the Night preset by running the following command:

open -g 'equaliser://preset/Night'

The -g flag prevents the app from stealing focus.

Here’s the implementation:

diff --git a/src/app/EqualiserApp.swift b/src/app/EqualiserApp.swift
index 702f728..d23ce51 100644
--- a/src/app/EqualiserApp.swift
+++ b/src/app/EqualiserApp.swift
@@ -5,9 +5,50 @@ import SwiftUI
 @MainActor
 final class AppCleanupDelegate: NSObject, NSApplicationDelegate {
     private weak var store: EqualiserStore?
+    private var pendingURLs: [URL] = []
 
     func setStore(_ store: EqualiserStore) {
         self.store = store
+
+        // Process URLs that arrived before the store was ready.
+        let urls = pendingURLs
+        pendingURLs.removeAll()
+
+        for url in urls {
+            handle(url)
+        }
+    }
+
+    func application(
+        _ application: NSApplication,
+        open urls: [URL]
+    ) {
+        for url in urls {
+            handle(url)
+        }
+    }
+
+    private func handle(_ url: URL) {
+        guard url.scheme?.lowercased() == "equaliser",
+              url.host?.lowercased() == "preset"
+        else {
+            return
+        }
+
+        let presetName = url.path
+            .trimmingCharacters(in: CharacterSet(charactersIn: "/"))
+            .removingPercentEncoding
+
+        guard let presetName, !presetName.isEmpty else {
+            return
+        }
+
+        guard let store else {
+            pendingURLs.append(url)
+            return
+        }
+
+        store.loadPreset(named: presetName)
     }
 }
 
@@ -62,8 +103,8 @@ struct EqualiserMain: App {
                 .environmentObject(windowActivation)
                 .onAppear {
                     windowActivation.launchAsMenuBarApp()
+
                     // Wire up appDelegate reference after @StateObject is initialized.
                     // MenuBarExtra is always visible, so this will always fire.
                     appDelegate.setStore(store)
                 }
         }
diff --git a/src/app/Info.plist b/src/app/Info.plist
index d1eae97..7d392af 100644
--- a/src/app/Info.plist
+++ b/src/app/Info.plist
@@ -20,6 +20,17 @@
        <string>APPL</string>
        <key>CFBundleShortVersionString</key>
        <string>1.3.3</string>
+       <key>CFBundleURLTypes</key>
+       <array>
+               <dict>
+                       <key>CFBundleURLName</key>
+                       <string>net.knage.equaliser</string>
+                       <key>CFBundleURLSchemes</key>
+                       <array>
+                               <string>equaliser</string>
+                       </array>
+               </dict>
+       </array>
        <key>CFBundleVersion</key>
        <string>ebd2e45</string>
        <key>LSMinimumSystemVersion</key>

rohdea0826 avatar Oct 20 '26 23:10 rohdea0826