1
1
import ArgumentParser
2
2
import SimplyCoreAudio
3
+ import Foundation
3
4
4
5
let simplyCA = SimplyCoreAudio ( )
5
6
@@ -11,7 +12,7 @@ struct SystemAudio: ParsableCommand {
11
12
var output = false
12
13
13
14
@Argument ( help: " the device to use for new input or output " )
14
- var deviceName : String ?
15
+ var deviceUid : String ?
15
16
16
17
17
18
mutating func run( ) throws {
@@ -25,49 +26,55 @@ struct SystemAudio: ParsableCommand {
25
26
devices = [ ]
26
27
}
27
28
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 } )
30
31
if input {
31
32
device? . isDefaultInputDevice = true
32
- print ( deviceName! )
33
+ print ( device! . name )
33
34
} else if output {
34
35
device? . isDefaultOutputDevice = true
35
- print ( deviceName! )
36
+ print ( device! . name )
36
37
} else {
37
38
print ( " confusion! " )
38
39
}
39
40
return
40
41
}
41
42
42
- print ( " <?xml version='1.0'?> " )
43
- print ( " < items> " )
43
+ var response : [ String : [ Any ] ] = [ : ]
44
+ var items = [ Any ] ( )
44
45
45
46
for device in devices {
46
47
let isOutput = device. channels ( scope: . output) > 0
47
48
let isInput = device. channels ( scope: . input) > 0
48
49
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
52
55
53
56
// add output/input to display
54
57
if isOutput {
55
- print ( " <icon> output.png</icon> " )
58
+ item [ " icon " ] = [ " path " : " output.png " ]
56
59
if device. isDefaultOutputDevice {
57
- print ( " < subtitle> Currently selected </subtitle> " )
60
+ item [ " subtitle " ] = " Currently selected "
58
61
}
59
62
}
60
63
61
64
if isInput {
62
- print ( " <icon> input.png</icon> " )
65
+ item [ " icon " ] = [ " path " : " input.png " ]
63
66
if device. isDefaultInputDevice {
64
- print ( " < subtitle> Currently selected </subtitle> " )
67
+ item [ " subtitle " ] = " Currently selected "
65
68
}
66
69
}
67
70
68
- print ( " </ item> " )
71
+ items . append ( item)
69
72
}
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)
71
78
}
72
79
}
73
80
0 commit comments