-
Notifications
You must be signed in to change notification settings - Fork 141
/
init.lua
86 lines (75 loc) · 2.13 KB
/
init.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
--- === MusicAppMediaFix ===
---
--- Override macOS behaviour and send all media keys (play/prev/next) to Music.app
local obj = { __gc = true }
--obj.__index = obj
setmetatable(obj, obj)
obj.__gc = function(t)
t:stop()
end
-- Metadata
obj.name = "MusicAppMediaFix"
obj.version = "1.0"
obj.author = "Matheus Salmi <[email protected]>, Chris Jones <[email protected]>"
obj.homepage = "https://github.com/Hammerspoon/Spoons"
obj.license = "MIT - https://opensource.org/licenses/MIT"
obj.eventtap = nil
-- Internal function used to find our location, so we know where to load files from
local function script_path()
local str = debug.getinfo(2, "S").source:sub(2)
return str:match("(.*/)")
end
obj.spoonPath = script_path()
function obj:init()
self.eventtap = hs.eventtap.new({hs.eventtap.event.types.systemDefined}, self.mediaKeyCallback)
end
function obj.mediaKeyCallback(event)
local data = event:systemKey()
-- ignore everything but media keys
if data["key"] ~= "PLAY" and data["key"] ~= "FAST" and data["key"] ~= "REWIND" then
return false, nil
end
-- handle action
if data["down"] == false or data["repeat"] == true then
if data["key"] == "PLAY" then
hs.applescript('tell application "Music" to playpause')
elseif data["key"] == "FAST" then
hs.applescript('tell application "Music" to next track')
elseif data["key"] == "REWIND" then
hs.applescript('tell application "Music" to previous track')
end
end
-- consume event
return true, nil
end
--- MusicAppMediaFix:start()
--- Method
--- Starts the hs.eventtap that powers this Spoon
---
--- Parameters:
--- * None
---
--- Returns:
--- * The MusicAppMediaFix object
function obj:start()
if self.eventtap:isEnabled() ~= true then
self.eventtap:start()
end
return self
end
--- MusicAppMediaFix:stop()
--- Method
--- Stops the hs.eventtap that powers this Spoon
---
--- Parameters:
--- * None
---
--- Returns:
--- * The MusicAppMediaFix object
function obj:stop()
if self.eventtap:isEnabled() then
self.eventtap:stop()
end
return self
end
return obj