Skip to content

Commit 43f13ec

Browse files
switch to JSON item filter, and use UID
1 parent e2ad9d0 commit 43f13ec

File tree

3 files changed

+48
-41
lines changed

3 files changed

+48
-41
lines changed

Sources/systemaudio/main.swift

Lines changed: 23 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import ArgumentParser
22
import SimplyCoreAudio
3+
import Foundation
34

45
let simplyCA = SimplyCoreAudio()
56

@@ -11,7 +12,7 @@ struct SystemAudio: ParsableCommand {
1112
var output = false
1213

1314
@Argument(help: "the device to use for new input or output")
14-
var deviceName : String?
15+
var deviceUid : String?
1516

1617

1718
mutating func run() throws {
@@ -25,49 +26,55 @@ struct SystemAudio: ParsableCommand {
2526
devices = []
2627
}
2728

28-
if deviceName != nil {
29-
let device = devices.first(where: { $0.name == deviceName })
29+
if deviceUid != nil {
30+
let device = devices.first(where: { $0.uid == deviceUid })
3031
if input {
3132
device?.isDefaultInputDevice = true
32-
print(deviceName!)
33+
print(device!.name)
3334
} else if output {
3435
device?.isDefaultOutputDevice = true
35-
print(deviceName!)
36+
print(device!.name)
3637
} else {
3738
print("confusion!")
3839
}
3940
return
4041
}
4142

42-
print("<?xml version='1.0'?>")
43-
print("<items>")
43+
var response: [String : [Any]] = [:]
44+
var items = [Any]()
4445

4546
for device in devices {
4647
let isOutput = device.channels(scope: .output) > 0
4748
let isInput = device.channels(scope: .input) > 0
4849

49-
let display = device.name
50-
print(" <item uid='\(display)' arg='\(display)' valid='YES' autocomplete='\(display)'>")
51-
print(" <title>\(display)</title>")
50+
var item: [String: Any] = [:]
51+
item["title"] = device.name
52+
item["uid"] = device.uid
53+
item["arg"] = device.uid
54+
item["autocomplete"] = device.name
5255

5356
// add output/input to display
5457
if isOutput {
55-
print(" <icon>output.png</icon>")
58+
item["icon"] = ["path": "output.png"]
5659
if device.isDefaultOutputDevice {
57-
print(" <subtitle> Currently selected </subtitle>")
60+
item["subtitle"] = "Currently selected"
5861
}
5962
}
6063

6164
if isInput {
62-
print(" <icon>input.png</icon>")
65+
item["icon"] = ["path": "input.png"]
6366
if device.isDefaultInputDevice {
64-
print(" <subtitle> Currently selected </subtitle>")
67+
item["subtitle"] = "Currently selected"
6568
}
6669
}
6770

68-
print(" </item>")
71+
items.append(item)
6972
}
70-
print("<items>")
73+
response["items"] = items
74+
75+
let jsonData = try JSONSerialization.data(withJSONObject: response, options: [])
76+
let jsonString = String(data: jsonData, encoding: String.Encoding.ascii)!
77+
print(jsonString)
7178
}
7279
}
7380

alfred/info.plist

Lines changed: 24 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -147,7 +147,7 @@
147147
<key>lastpathcomponent</key>
148148
<false/>
149149
<key>onlyshowifquerypopulated</key>
150-
<true/>
150+
<false/>
151151
<key>removeextension</key>
152152
<false/>
153153
<key>text</key>
@@ -162,29 +162,6 @@
162162
<key>version</key>
163163
<integer>1</integer>
164164
</dict>
165-
<dict>
166-
<key>config</key>
167-
<dict>
168-
<key>concurrently</key>
169-
<false/>
170-
<key>escaping</key>
171-
<integer>102</integer>
172-
<key>script</key>
173-
<string>./systemaudio -o "$1"</string>
174-
<key>scriptargtype</key>
175-
<integer>1</integer>
176-
<key>scriptfile</key>
177-
<string></string>
178-
<key>type</key>
179-
<integer>0</integer>
180-
</dict>
181-
<key>type</key>
182-
<string>alfred.workflow.action.script</string>
183-
<key>uid</key>
184-
<string>05A9889D-04A7-4901-9FC9-6DD97AB7E885</string>
185-
<key>version</key>
186-
<integer>2</integer>
187-
</dict>
188165
<dict>
189166
<key>config</key>
190167
<dict>
@@ -234,6 +211,29 @@
234211
<key>version</key>
235212
<integer>3</integer>
236213
</dict>
214+
<dict>
215+
<key>config</key>
216+
<dict>
217+
<key>concurrently</key>
218+
<false/>
219+
<key>escaping</key>
220+
<integer>102</integer>
221+
<key>script</key>
222+
<string>./systemaudio -o "$1"</string>
223+
<key>scriptargtype</key>
224+
<integer>1</integer>
225+
<key>scriptfile</key>
226+
<string></string>
227+
<key>type</key>
228+
<integer>0</integer>
229+
</dict>
230+
<key>type</key>
231+
<string>alfred.workflow.action.script</string>
232+
<key>uid</key>
233+
<string>05A9889D-04A7-4901-9FC9-6DD97AB7E885</string>
234+
<key>version</key>
235+
<integer>2</integer>
236+
</dict>
237237
</array>
238238
<key>readme</key>
239239
<string></string>

dev.sh

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ mkdir -p dev
66

77
cd dev
88
ln -sf ../alfred/* ./
9-
ln -sf ../.build/x86_64-apple-macosx/release/systemaudio .
9+
ln -sf ../.build/x86_64-apple-macosx/debug/systemaudio .
1010

1111
prefs=~/Library/Preferences/com.runningwithcrayons.Alfred-Preferences.plist
1212
syncfolder=$(/usr/libexec/PlistBuddy -c "print :syncfolder" $prefs 2>/dev/null || echo)

0 commit comments

Comments
 (0)