Skip to content

Commit

Permalink
Don't choke if the active app isn't installed #6
Browse files Browse the repository at this point in the history
  • Loading branch information
deanishe committed Jan 29, 2018
1 parent d56a483 commit 3d30a01
Show file tree
Hide file tree
Showing 7 changed files with 127 additions and 3 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
.alfredversionchecked

# Created by https://www.gitignore.io/api/python

### Python ###
Expand Down
Binary file not shown.
2 changes: 1 addition & 1 deletion src/info.plist
Original file line number Diff line number Diff line change
Expand Up @@ -401,7 +401,7 @@ or Tunnelblick app from https://tunnelblick.net/</string>
<string>Viscosity</string>
</dict>
<key>version</key>
<string>3.0.1</string>
<string>3.0.2</string>
<key>webaddress</key>
<string>https://github.com/deanishe/alfred-vpn-manager</string>
</dict>
Expand Down
116 changes: 116 additions & 0 deletions src/shimo.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,116 @@
#!/usr/bin/osascript -l JavaScript

ObjC.import('stdlib')

Array.prototype.contains = function(val) {
for (var i; i < this.length; i++) {
if (this[i] === val)
return true
}
return false
}

app = Application('Shimo')
help = `shimo.js <command> [<name>]
Usage:
shimo.js (connect|disconnect) <name>
shimo.js list
shimo.js -h
Options:
-h, --help Show this message and quit
`

// Return true if specified VPN is connected.
function isActive(name) {
for (var i=0; i < app.connections.length; i++) {
var conn = app.connections[i]
if (conn.name() == name) {
return conn.state() == 'Connected'
}
}
return false
}


// Show CLI help.
function showHelp(errMsg) {
var status = 0
if (errMsg) {
console.log(`error: ${errMsg}`)
status = 1
}
console.log(help)
$.exit(status)
}


// Parse command-line flags.
function parseArgs(argv) {
if (argv.length == 0 || argv.contains('-h') || argv.contains('--help')) {
showHelp()
}

var command = argv[0],
opts = {command: command, name: ''}

if (command === 'list') {
return opts
}

if (command === 'connect' || command === 'disconnect') {
if (argv.length < 2) {
showHelp('no VPN name specified')
}
opts.name = argv[1]
return opts
}

showHelp(`unknown command: ${command}`)
}


// Connect to specified VPN.
function connect(name) {
if (isActive(name)) {
console.log(`VPN ${name} is already connected`)
return
}
app.connect(name)
}


// Disconnect specified VPN.
function disconnect(name) {
if (!isActive(name)) {
console.log(`VPN ${name} is not connected`)
return
}
app.disconnect(name)
}

// Return JSON mapping of connections to connected state.
function list() {
var accounts = {}

for (var i=0; i < app.accounts().length; i++) {
var acc = app.accounts[i]
accounts[acc.name()] = acc.connected()
}

return JSON.stringify(connections)
}


function run(argv) {
opts = parseArgs(argv)
switch (opts.command) {
case 'connect':
return connect(opts.name)
case 'disconnect':
return disconnect(opts.name)
case 'list':
return list()
}
}
Binary file added src/update-available.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
4 changes: 2 additions & 2 deletions src/vpn.py
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,7 @@ def info(self):
"""Return application info or `None` if not installed."""
if self._info is False:
self._info = appinfo(self.name)
log.debug('appinfo=%r', self._info)
log.debug('[%s] appinfo=%r', self.name, self._info)
return self._info

@property
Expand Down Expand Up @@ -336,7 +336,7 @@ def do_config(query):
# ------------------------------------------------------
# VPN apps
for app in get_all_apps():
if app.selected:
if app.selected and app.installed:
items.append(dict(
title=u'{} (active)'.format(app.name),
subtitle=u'{} is the active application'.format(app.name),
Expand Down
6 changes: 6 additions & 0 deletions src/workflow/util.py
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,7 @@ def utf8ify(s):
Returns:
str: UTF-8 string or string representation of s.
"""
if isinstance(s, str):
return s
Expand Down Expand Up @@ -124,6 +125,7 @@ def applescriptify(s):
Returns:
unicode: Escaped string
"""
return s.replace(u'"', u'" & quote & "')

Expand All @@ -142,6 +144,7 @@ def run_command(cmd, **kwargs):
Returns:
str: Output returned by ``check_output``.
"""
cmd = [utf8ify(s) for s in cmd]
return subprocess.check_output(cmd, **kwargs)
Expand Down Expand Up @@ -190,6 +193,7 @@ def run_jxa(script, *args):
Returns:
str: Output of script.
"""
return run_applescript(script, *args, lang='JavaScript')

Expand All @@ -206,6 +210,7 @@ def run_trigger(name, bundleid=None, arg=None):
name (str): Name of External Trigger to call.
bundleid (str, optional): Bundle ID of workflow trigger belongs to.
arg (str, optional): Argument to pass to trigger.
"""
if not bundleid:
bundleid = os.getenv('alfred_workflow_bundleid')
Expand All @@ -231,6 +236,7 @@ def appinfo(name):
Returns:
AppInfo: :class:`AppInfo` tuple or ``None`` if app isn't found.
"""
cmd = ['mdfind', '-onlyin', '/',
'(kMDItemContentTypeTree == com.apple.application &&'
Expand Down

0 comments on commit 3d30a01

Please sign in to comment.