forked from zzamboni/dot-hammerspoon
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathurl_handling.lua
87 lines (81 loc) · 3.05 KB
/
url_handling.lua
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
-- URL dispatcher
-- Sets Hammerspoon as the default browser for HTTP/HTTPS links, and
-- dispatches them to different apps according to the patterns define
-- in the config. If no pattern matches, `default_handler` is used.
local mod = {}
mod.config = {
patterns = {
-- evaluated in the order they are declared. Entry format: { "url pattern", "application bundle ID" }
-- e.g.
-- { "https?://gmail.com", "com.google.Chrome" },
-- { "https?://en.wikipedia.org", "org.epichrome.app.Wikipedia" },
},
-- Bundle ID for default URL handler
default_handler = "com.apple.Safari",
}
-- Attempt at getting an app ID from either an ID or the app name. Not fully debugged yet.
function getAppId(app, launch)
if launch == nil then
launch = false
end
-- Convert to app name if it's a bundleID
local name = hs.application.nameForBundleID(app)
local appid = nil
if name ~= nil then
-- app is a valid bundleID, so we return it
logger.df("Found an app with bundle ID %s: %s", app, name)
appid = app
else
-- assume it's an app name, first try to find it running
local appobj = hs.application.find(app)
if appobj ~= nil then
appid = appobj:bundleID()
logger.df("Found a running app that matches %s: %s (bundle ID %s)", app, appobj:name(), appid)
else
if launch then
-- as a last resort, try to launch it and then get its ID
logger.df("Trying to launch app %s", app)
if hs.application.launchOrFocus(app) then
appobj = hs.application.find(app)
logger.df("appobj = %s", hs.inspect(appobj))
if appobj ~= nil then
logger.df("Found a running app that matches %s: %s (bundle ID %s)", app, appobj:name(), appid)
appid = appobj:bundleID()
else
logger.df("%s launched successfully, but can't find it running", app)
end
else
logger.ef("Launching app %s failed", app)
end
else
logger.df("No running app matches '%s', launch=false so not trying to run one", app)
end
end
end
return appid
end
function mod.customHttpCallback(scheme, host, params, fullUrl)
logger.df("Handling URL %s", fullUrl)
for i,pair in ipairs(mod.config.patterns) do
local p = pair[1]
local app = pair[2]
logger.df("Matching %s against %s", fullUrl, p)
if string.match(fullUrl, p) then
logger.df(" Match! Opening with %s", app)
-- id = getAppId(app, true)
id = app
if id ~= nil then
hs.urlevent.openURLWithBundle(fullUrl, id)
return
else
logger.wf("I could not find an application that matches '%s', falling through to default handler", app)
end
end
end
hs.urlevent.openURLWithBundle(fullUrl, mod.config.default_handler)
end
function mod.init()
hs.urlevent.httpCallback = mod.customHttpCallback
hs.urlevent.setDefaultHandler('http')
end
return mod