I ended up doing a lot more work than I planned on this, but I'm pretty happy with what I came up with. I started with @deanishe's workflow and then edited the python script to get a list of URL handlers from the OS instead of requiring them to be hard-coded. This does slow down the workflow by about 500 ms which isn't ideal, but it gets the job done. I've also discovered Alfred-Workflow and the built in support for caching so I'm going to try that later in order to speed things up a bit.
It'd be great if there was a way to pull URL handlers directly from Alfred/Spotlight, but all I've been able to find so far is calling 'lsregister -dump' which is where most of the speed hit comes from. If anyone has any suggestions on how to speed that bit up I'd greatly appreciate it. In the meantime, here's the updated code I used for the workflow:
"""Open a URL in different browsers"""
from __future__ import print_function, absolute_import
from collections import namedtuple
import json
import sys
import os
import re
rexps = [
re.compile('^\s*(bundle)\s*id:\s*(\d*)'),
re.compile('^\s*(path):\s*(.*)'),
re.compile('^\s*(name):\s*(.*)'),
re.compile('^\s*(bindings):\s*(.*)')
]
def log(s, *args):
"""Simple STDERR logger."""
if args:
s = s % args
print(s, file=sys.stderr)
def gethandlers():
handlers = {}
bundle = ""
dump = os.popen("/System/Library/Frameworks/CoreServices.framework/Versions/A/Frameworks/LaunchServices.framework/Versions/A/Support/lsregister -dump")
for line in dump.readlines():
for rexp in rexps:
m = rexp.match(line)
if not m:
continue
key = m.group(1)
value = m.group(2)
if key == "bundle":
if bundle != value:
bundle = value
name = ""
path = ""
if key == "name" and not name:
name = value
if key == "path" and not path:
path = value
if key == "bindings" and 'http:' in value.split(","):
handlers[name] = path
dump.close()
handlers.pop('nwjs', None)
handlers.pop('VLC media player', None)
return handlers
def main():
"""Run Script Filter."""
url = ("http" + sys.argv[1]).decode('utf-8')
log('url=%r', url)
items = []
browsers = gethandlers()
for b in sorted(browsers):
items.append(dict(
title='Open in ' + b,
subtitle=browsers[b],
arg=url,
icon=dict(
path=browsers[b],
type='fileicon',
),
valid=True,
variables=dict(
browser=browsers[b],
url=url,
),
))
json.dump(dict(items=items), sys.stdout)
if __name__ == '__main__':
main()