-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathvolume_control.py
executable file
·135 lines (123 loc) · 4.07 KB
/
volume_control.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
#! /usr/local/bin/python
import os
import sys
import commands
import alfred
def execute(cmd):
return commands.getstatusoutput(cmd)[1]
def get_volume():
return execute("""osascript -e 'output volume of (get volume settings)'""")
def get_muted():
return execute("""osascript -e 'output muted of (get volume settings)'""")
def set_volume(vol):
os.system("""osascript -e 'set volume output volume %d'""" % vol)
def set_muted(muted):
os.system("""osascript -e 'set volume output muted %s'""" % muted)
def get_volume_summary():
if get_muted() == 'true':
return "Muted, original volume: " + get_volume() + "%"
else:
return "Current volume: " + get_volume() + "%"
def parse_volume(s, default = 10):
try:
return int(s)
except:
return default
def process_query(q):
results = []
if(len(q) == 0):
results.append(alfred.Item(
attributes = {'arg': '', 'valid': 'false'},
title = get_volume_summary(),
subtitle = ""
))
op = q[0].lower() if len(q) > 0 else ""
if op == "":
results.append(alfred.Item(
attributes = {'arg': '', 'valid': 'false'},
title = "Set Volume to ...",
subtitle = "vol ${num}"
))
else:
try:
new = int(op)
except:
new = -1
if new >= 0:
results.append(alfred.Item(
attributes = {'arg': str(new), 'autocomplete': ''},
title = "Set Volume: %d%%" % new,
subtitle = "vol ${num}"
))
if('mute'.startswith(op)) and get_muted() != 'true':
results.append(alfred.Item(
attributes = {'arg': 'mute', 'autocomplete' : 'mute'},
title = "Mute",
subtitle = "vol mute"
))
if('unmute'.startswith(op)) and get_muted() == 'true':
results.append(alfred.Item(
attributes = {'arg': 'unmute', 'autocomplete': 'unmute'},
title = "Unumte",
subtitle = "vol unmute"
))
if('up'.startswith(op)):
if len(q) >= 2:
val = parse_volume(q[1], 10)
arg = 'up ' + str(val)
else:
val = 10
arg = 'up 10'
results.append(alfred.Item(
attributes = {'arg': arg, 'autocomplete': 'up'},
title = "Volume up by %d%%" % val,
subtitle = "vol up ${num}",
subtitleCmd = "Hold cmd to double the increament."
))
if('down'.startswith(op)):
if len(q) >= 2:
val = parse_volume(q[1], 10)
arg = 'down ' + str(val)
else:
val = 10
arg = 'down 10'
results.append(alfred.Item(
attributes = {'arg': arg, 'autocomplete': 'down'},
title = "Volume down by %d%%" % val,
subtitle = "vol down ${num}",
subtitleCmd = "Hold cmd to double the decreasement."
))
if('max'.startswith(op)):
results.append(alfred.Item(
attributes = {'arg': '100', 'autocomplete': 'max'},
title = "Volume Max",
subtitle = "vol max"
))
if('low'.startswith(op)):
results.append(alfred.Item(
attributes = {'arg': '25', 'autocomplete': 'low'},
title = "Low Volume: 25%",
subtitle = "vol low"
))
if('mid'.startswith(op)):
results.append(alfred.Item(
attributes = {'arg': '50', 'autocomplete': 'mid'},
title = "Middle Volume: 50%",
subtitle = "vol mid"
))
if('high'.startswith(op)):
results.append(alfred.Item(
attributes = {'arg': '75', 'autocomplete': 'high'},
title = "High Volume: 75%",
subtitle = "vol high"
))
if(len(q) > 0):
results.append(alfred.Item(
attributes = {'arg': '', 'valid': 'false'},
title = get_volume_summary(),
subtitle = ""
))
xml = alfred.xml(results) # compiles the XML answer
alfred.write(xml)
if __name__ == "__main__":
process_query(sys.argv[1:])