Skip to content

Commit

Permalink
Option to run script when sample rate change
Browse files Browse the repository at this point in the history
  • Loading branch information
vincentneo committed Dec 7, 2023
1 parent 84bf620 commit 1d8a891
Show file tree
Hide file tree
Showing 3 changed files with 66 additions and 0 deletions.
37 changes: 37 additions & 0 deletions Quality/AppDelegate.swift
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@ class AppDelegate: NSObject, NSApplicationDelegate {

var statusItem: NSStatusItem?
var cancellable: AnyCancellable?

var currentScriptSelectionMenuItem: NSMenuItem?

private var _statusItemTitle = "Loading..."
var statusItemTitle: String {
Expand Down Expand Up @@ -62,6 +64,8 @@ class AppDelegate: NSObject, NSApplicationDelegate {
checkPermissions()

let menu = NSMenu()

menu.delegate = self

let sampleRateView = ContentView().environmentObject(outputDevices)
let view = NSHostingView(rootView: sampleRateView)
Expand Down Expand Up @@ -96,6 +100,17 @@ class AppDelegate: NSObject, NSApplicationDelegate {
aboutItem.submenu?.addItem(buildItem)
menu.addItem(aboutItem)

let scriptMenu = NSMenuItem(title: "Scripting", action: nil, keyEquivalent: "")
let selectScript = NSMenuItem(title: "Select Script...", action: #selector(selectScript(_:)), keyEquivalent: "")
let resetScript = NSMenuItem(title: "Clear selection", action: #selector(resetScript(_:)), keyEquivalent: "")
let currentScriptSelectionMenuItem = NSMenuItem(title: "No selection", action: nil, keyEquivalent: "")
self.currentScriptSelectionMenuItem = currentScriptSelectionMenuItem
scriptMenu.submenu = NSMenu()
scriptMenu.submenu?.addItem(selectScript)
scriptMenu.submenu?.addItem(resetScript)
scriptMenu.submenu?.addItem(currentScriptSelectionMenuItem)
menu.addItem(scriptMenu)

let quitItem = NSMenuItem(title: "Quit", action: #selector(NSApp.terminate(_:)), keyEquivalent: "")
menu.addItem(quitItem)

Expand Down Expand Up @@ -175,4 +190,26 @@ class AppDelegate: NSObject, NSApplicationDelegate {
}
}

@objc func selectScript(_ item: NSMenuItem) {
let panel = NSOpenPanel()
panel.canChooseFiles = true
panel.canChooseDirectories = false
panel.allowsMultipleSelection = false
panel.message = "Select a script that should be invoked when sample rate changes."

panel.begin { response in
Defaults.shared.shellScriptPath = panel.url?.path
}
}

@objc func resetScript(_ item: NSMenuItem) {
Defaults.shared.shellScriptPath = nil
}

}

extension AppDelegate: NSMenuDelegate {
func menuWillOpen(_ menu: NSMenu) {
currentScriptSelectionMenuItem?.title = Defaults.shared.shellScriptPath ?? "No selection"
}
}
10 changes: 10 additions & 0 deletions Quality/Defaults.swift
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ class Defaults: ObservableObject {
private let kUserPreferIconStatusBarItem = "com.vincent-neo.LosslessSwitcher-Key-UserPreferIconStatusBarItem"
private let kSelectedDeviceUID = "com.vincent-neo.LosslessSwitcher-Key-SelectedDeviceUID"
private let kUserPreferBitDepthDetection = "com.vincent-neo.LosslessSwitcher-Key-BitDepthDetection"
private let kShellScriptPath = "KeyShellScriptPath"

private init() {
UserDefaults.standard.register(defaults: [
Expand Down Expand Up @@ -40,6 +41,15 @@ class Defaults: ObservableObject {
}
}

var shellScriptPath: String? {
get {
return UserDefaults.standard.string(forKey: kShellScriptPath)
}
set {
UserDefaults.standard.setValue(newValue, forKey: kShellScriptPath)
}
}

@Published var userPreferBitDepthDetection: Bool


Expand Down
19 changes: 19 additions & 0 deletions Quality/OutputDevices.swift
Original file line number Diff line number Diff line change
Expand Up @@ -252,5 +252,24 @@ class OutputDevices: ObservableObject {
let delegate = AppDelegate.instance
delegate?.statusItemTitle = String(format: "%.1f kHz", readableSampleRate)
}
self.runUserScript(sampleRate)
}

func runUserScript(_ sampleRate: Float64) {
guard let scriptPath = Defaults.shared.shellScriptPath else { return }
let argumentSampleRate = String(Int(sampleRate))
Task.detached {
let scriptURL = URL(fileURLWithPath: scriptPath)
do {
let task = try NSUserUnixTask(url: scriptURL)
let arguments = [
argumentSampleRate
]
try await task.execute(withArguments: arguments)
}
catch {
print("TASK ERR \(error)")
}
}
}
}

0 comments on commit 1d8a891

Please sign in to comment.