Skip to content

Commit 3d30a01

Browse files
committedJan 29, 2018
Don't choke if the active app isn't installed #6
1 parent d56a483 commit 3d30a01

7 files changed

+127
-3
lines changed
 

‎.gitignore

+2
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
.alfredversionchecked
2+
13
# Created by https://www.gitignore.io/api/python
24

35
### Python ###
Binary file not shown.

‎src/info.plist

+1-1
Original file line numberDiff line numberDiff line change
@@ -401,7 +401,7 @@ or Tunnelblick app from https://tunnelblick.net/</string>
401401
<string>Viscosity</string>
402402
</dict>
403403
<key>version</key>
404-
<string>3.0.1</string>
404+
<string>3.0.2</string>
405405
<key>webaddress</key>
406406
<string>https://github.com/deanishe/alfred-vpn-manager</string>
407407
</dict>

‎src/shimo.js

+116
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,116 @@
1+
#!/usr/bin/osascript -l JavaScript
2+
3+
ObjC.import('stdlib')
4+
5+
Array.prototype.contains = function(val) {
6+
for (var i; i < this.length; i++) {
7+
if (this[i] === val)
8+
return true
9+
}
10+
return false
11+
}
12+
13+
app = Application('Shimo')
14+
help = `shimo.js <command> [<name>]
15+
16+
Usage:
17+
shimo.js (connect|disconnect) <name>
18+
shimo.js list
19+
shimo.js -h
20+
21+
Options:
22+
-h, --help Show this message and quit
23+
`
24+
25+
// Return true if specified VPN is connected.
26+
function isActive(name) {
27+
for (var i=0; i < app.connections.length; i++) {
28+
var conn = app.connections[i]
29+
if (conn.name() == name) {
30+
return conn.state() == 'Connected'
31+
}
32+
}
33+
return false
34+
}
35+
36+
37+
// Show CLI help.
38+
function showHelp(errMsg) {
39+
var status = 0
40+
if (errMsg) {
41+
console.log(`error: ${errMsg}`)
42+
status = 1
43+
}
44+
console.log(help)
45+
$.exit(status)
46+
}
47+
48+
49+
// Parse command-line flags.
50+
function parseArgs(argv) {
51+
if (argv.length == 0 || argv.contains('-h') || argv.contains('--help')) {
52+
showHelp()
53+
}
54+
55+
var command = argv[0],
56+
opts = {command: command, name: ''}
57+
58+
if (command === 'list') {
59+
return opts
60+
}
61+
62+
if (command === 'connect' || command === 'disconnect') {
63+
if (argv.length < 2) {
64+
showHelp('no VPN name specified')
65+
}
66+
opts.name = argv[1]
67+
return opts
68+
}
69+
70+
showHelp(`unknown command: ${command}`)
71+
}
72+
73+
74+
// Connect to specified VPN.
75+
function connect(name) {
76+
if (isActive(name)) {
77+
console.log(`VPN ${name} is already connected`)
78+
return
79+
}
80+
app.connect(name)
81+
}
82+
83+
84+
// Disconnect specified VPN.
85+
function disconnect(name) {
86+
if (!isActive(name)) {
87+
console.log(`VPN ${name} is not connected`)
88+
return
89+
}
90+
app.disconnect(name)
91+
}
92+
93+
// Return JSON mapping of connections to connected state.
94+
function list() {
95+
var accounts = {}
96+
97+
for (var i=0; i < app.accounts().length; i++) {
98+
var acc = app.accounts[i]
99+
accounts[acc.name()] = acc.connected()
100+
}
101+
102+
return JSON.stringify(connections)
103+
}
104+
105+
106+
function run(argv) {
107+
opts = parseArgs(argv)
108+
switch (opts.command) {
109+
case 'connect':
110+
return connect(opts.name)
111+
case 'disconnect':
112+
return disconnect(opts.name)
113+
case 'list':
114+
return list()
115+
}
116+
}

‎src/update-available.png

4.76 KB
Loading

‎src/vpn.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -117,7 +117,7 @@ def info(self):
117117
"""Return application info or `None` if not installed."""
118118
if self._info is False:
119119
self._info = appinfo(self.name)
120-
log.debug('appinfo=%r', self._info)
120+
log.debug('[%s] appinfo=%r', self.name, self._info)
121121
return self._info
122122

123123
@property
@@ -336,7 +336,7 @@ def do_config(query):
336336
# ------------------------------------------------------
337337
# VPN apps
338338
for app in get_all_apps():
339-
if app.selected:
339+
if app.selected and app.installed:
340340
items.append(dict(
341341
title=u'{} (active)'.format(app.name),
342342
subtitle=u'{} is the active application'.format(app.name),

‎src/workflow/util.py

+6
Original file line numberDiff line numberDiff line change
@@ -96,6 +96,7 @@ def utf8ify(s):
9696
9797
Returns:
9898
str: UTF-8 string or string representation of s.
99+
99100
"""
100101
if isinstance(s, str):
101102
return s
@@ -124,6 +125,7 @@ def applescriptify(s):
124125
125126
Returns:
126127
unicode: Escaped string
128+
127129
"""
128130
return s.replace(u'"', u'" & quote & "')
129131

@@ -142,6 +144,7 @@ def run_command(cmd, **kwargs):
142144
143145
Returns:
144146
str: Output returned by ``check_output``.
147+
145148
"""
146149
cmd = [utf8ify(s) for s in cmd]
147150
return subprocess.check_output(cmd, **kwargs)
@@ -190,6 +193,7 @@ def run_jxa(script, *args):
190193
191194
Returns:
192195
str: Output of script.
196+
193197
"""
194198
return run_applescript(script, *args, lang='JavaScript')
195199

@@ -206,6 +210,7 @@ def run_trigger(name, bundleid=None, arg=None):
206210
name (str): Name of External Trigger to call.
207211
bundleid (str, optional): Bundle ID of workflow trigger belongs to.
208212
arg (str, optional): Argument to pass to trigger.
213+
209214
"""
210215
if not bundleid:
211216
bundleid = os.getenv('alfred_workflow_bundleid')
@@ -231,6 +236,7 @@ def appinfo(name):
231236
232237
Returns:
233238
AppInfo: :class:`AppInfo` tuple or ``None`` if app isn't found.
239+
234240
"""
235241
cmd = ['mdfind', '-onlyin', '/',
236242
'(kMDItemContentTypeTree == com.apple.application &&'

0 commit comments

Comments
 (0)