diff --git a/Sources/systemaudio/main.swift b/Sources/systemaudio/main.swift index 08ed08b..018aa8c 100644 --- a/Sources/systemaudio/main.swift +++ b/Sources/systemaudio/main.swift @@ -1,5 +1,6 @@ import ArgumentParser import SimplyCoreAudio +import Foundation let simplyCA = SimplyCoreAudio() @@ -11,7 +12,7 @@ struct SystemAudio: ParsableCommand { var output = false @Argument(help: "the device to use for new input or output") - var deviceName : String? + var deviceUid : String? mutating func run() throws { @@ -25,49 +26,55 @@ struct SystemAudio: ParsableCommand { devices = [] } - if deviceName != nil { - let device = devices.first(where: { $0.name == deviceName }) + if deviceUid != nil { + let device = devices.first(where: { $0.uid == deviceUid }) if input { device?.isDefaultInputDevice = true - print(deviceName!) + print(device!.name) } else if output { device?.isDefaultOutputDevice = true - print(deviceName!) + print(device!.name) } else { print("confusion!") } return } - print("") - print("") + var response: [String : [Any]] = [:] + var items = [Any]() for device in devices { let isOutput = device.channels(scope: .output) > 0 let isInput = device.channels(scope: .input) > 0 - let display = device.name - print(" ") - print(" \(display)") + var item: [String: Any] = [:] + item["title"] = device.name + item["uid"] = device.uid + item["arg"] = device.uid + item["autocomplete"] = device.name // add output/input to display if isOutput { - print(" output.png") + item["icon"] = ["path": "output.png"] if device.isDefaultOutputDevice { - print(" Currently selected ") + item["subtitle"] = "Currently selected" } } if isInput { - print(" input.png") + item["icon"] = ["path": "input.png"] if device.isDefaultInputDevice { - print(" Currently selected ") + item["subtitle"] = "Currently selected" } } - print(" ") + items.append(item) } - print("") + response["items"] = items + + let jsonData = try JSONSerialization.data(withJSONObject: response, options: []) + let jsonString = String(data: jsonData, encoding: String.Encoding.ascii)! + print(jsonString) } } diff --git a/alfred/info.plist b/alfred/info.plist index d620775..e4be2a1 100644 --- a/alfred/info.plist +++ b/alfred/info.plist @@ -147,7 +147,7 @@ lastpathcomponent onlyshowifquerypopulated - + removeextension text @@ -162,29 +162,6 @@ version 1 - - config - - concurrently - - escaping - 102 - script - ./systemaudio -o "$1" - scriptargtype - 1 - scriptfile - - type - 0 - - type - alfred.workflow.action.script - uid - 05A9889D-04A7-4901-9FC9-6DD97AB7E885 - version - 2 - config @@ -234,6 +211,29 @@ version 3 + + config + + concurrently + + escaping + 102 + script + ./systemaudio -o "$1" + scriptargtype + 1 + scriptfile + + type + 0 + + type + alfred.workflow.action.script + uid + 05A9889D-04A7-4901-9FC9-6DD97AB7E885 + version + 2 + readme diff --git a/dev.sh b/dev.sh index 0f2c237..a7ec421 100755 --- a/dev.sh +++ b/dev.sh @@ -6,7 +6,7 @@ mkdir -p dev cd dev ln -sf ../alfred/* ./ -ln -sf ../.build/x86_64-apple-macosx/release/systemaudio . +ln -sf ../.build/x86_64-apple-macosx/debug/systemaudio . prefs=~/Library/Preferences/com.runningwithcrayons.Alfred-Preferences.plist syncfolder=$(/usr/libexec/PlistBuddy -c "print :syncfolder" $prefs 2>/dev/null || echo)