Skip to content

Commit

Permalink
switch to JSON item filter, and use UID
Browse files Browse the repository at this point in the history
  • Loading branch information
technicalpickles committed Aug 22, 2021
1 parent e2ad9d0 commit 43f13ec
Show file tree
Hide file tree
Showing 3 changed files with 48 additions and 41 deletions.
39 changes: 23 additions & 16 deletions Sources/systemaudio/main.swift
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import ArgumentParser
import SimplyCoreAudio
import Foundation

let simplyCA = SimplyCoreAudio()

Expand All @@ -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 {
Expand All @@ -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("<?xml version='1.0'?>")
print("<items>")
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(" <item uid='\(display)' arg='\(display)' valid='YES' autocomplete='\(display)'>")
print(" <title>\(display)</title>")
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(" <icon>output.png</icon>")
item["icon"] = ["path": "output.png"]
if device.isDefaultOutputDevice {
print(" <subtitle> Currently selected </subtitle>")
item["subtitle"] = "Currently selected"
}
}

if isInput {
print(" <icon>input.png</icon>")
item["icon"] = ["path": "input.png"]
if device.isDefaultInputDevice {
print(" <subtitle> Currently selected </subtitle>")
item["subtitle"] = "Currently selected"
}
}

print(" </item>")
items.append(item)
}
print("<items>")
response["items"] = items

let jsonData = try JSONSerialization.data(withJSONObject: response, options: [])
let jsonString = String(data: jsonData, encoding: String.Encoding.ascii)!
print(jsonString)
}
}

Expand Down
48 changes: 24 additions & 24 deletions alfred/info.plist
Original file line number Diff line number Diff line change
Expand Up @@ -147,7 +147,7 @@
<key>lastpathcomponent</key>
<false/>
<key>onlyshowifquerypopulated</key>
<true/>
<false/>
<key>removeextension</key>
<false/>
<key>text</key>
Expand All @@ -162,29 +162,6 @@
<key>version</key>
<integer>1</integer>
</dict>
<dict>
<key>config</key>
<dict>
<key>concurrently</key>
<false/>
<key>escaping</key>
<integer>102</integer>
<key>script</key>
<string>./systemaudio -o "$1"</string>
<key>scriptargtype</key>
<integer>1</integer>
<key>scriptfile</key>
<string></string>
<key>type</key>
<integer>0</integer>
</dict>
<key>type</key>
<string>alfred.workflow.action.script</string>
<key>uid</key>
<string>05A9889D-04A7-4901-9FC9-6DD97AB7E885</string>
<key>version</key>
<integer>2</integer>
</dict>
<dict>
<key>config</key>
<dict>
Expand Down Expand Up @@ -234,6 +211,29 @@
<key>version</key>
<integer>3</integer>
</dict>
<dict>
<key>config</key>
<dict>
<key>concurrently</key>
<false/>
<key>escaping</key>
<integer>102</integer>
<key>script</key>
<string>./systemaudio -o "$1"</string>
<key>scriptargtype</key>
<integer>1</integer>
<key>scriptfile</key>
<string></string>
<key>type</key>
<integer>0</integer>
</dict>
<key>type</key>
<string>alfred.workflow.action.script</string>
<key>uid</key>
<string>05A9889D-04A7-4901-9FC9-6DD97AB7E885</string>
<key>version</key>
<integer>2</integer>
</dict>
</array>
<key>readme</key>
<string></string>
Expand Down
2 changes: 1 addition & 1 deletion dev.sh
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down

0 comments on commit 43f13ec

Please sign in to comment.