Last active
January 28, 2019 15:09
-
-
Save fanzeyi/cf89b0429c4f695cb385746b594e62bf to your computer and use it in GitHub Desktop.
Hammerspoon Configuration
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
local last_alert = nil | |
function displaySong(player) | |
artist, name = player.getCurrentArtist(), player.getCurrentTrack() | |
if (last_alert ~= nil) then | |
hs.alert.closeSpecific(last_alert) | |
end | |
last_alert = hs.alert.show(artist .. " - " .. name) | |
end | |
function PlayNext() | |
if (hs.itunes.isPlaying()) then | |
hs.itunes.next() | |
displaySong(hs.itunes) | |
elseif (hs.spotify.isPlaying()) then | |
hs.spotify.next() | |
displaySong(hs.spotify) | |
end | |
end | |
function PlayPrevious() | |
if (hs.itunes.isPlaying()) then | |
hs.itunes.previous() | |
displaySong(hs.itunes) | |
elseif (hs.spotify.isPlaying()) then | |
hs.spotify.previous() | |
displaySong(hs.spotify) | |
end | |
end | |
local last_paused = "" | |
function PlayPause() | |
if (last_paused == "") then | |
if (hs.spotify.isPlaying()) then | |
last_paused = "spotify" | |
hs.spotify.playpause() | |
elseif (hs.itunes.isPlaying()) then | |
last_paused = "itunes" | |
hs.itunes.playpause() | |
elseif (hs.spotify.isRunning()) then | |
last_paused = "spotify" | |
hs.spotify.playpause() | |
elseif (hs.itunes.isRunning()) then | |
last_paused = "itunes" | |
hs.itunes.playpause() | |
end | |
elseif (last_paused == "itunes") then | |
last_paused = "" | |
hs.itunes.play() | |
elseif (last_paused == "spotify") then | |
last_paused = "" | |
hs.spotify.play() | |
end | |
end | |
local hhkbHotkeys = { | |
hs.hotkey.bind({}, "f11", function() | |
PlayPrevious() | |
end), | |
hs.hotkey.bind({}, "f12", function() | |
PlayNext() | |
end), | |
hs.hotkey.bind({}, "help", function() | |
PlayPause() | |
end), | |
} | |
local hhkbWatcher = nil | |
function enableHotkeys(hk) | |
for i, v in ipairs(hk) do | |
v:enable() | |
end | |
end | |
function disableHotkeys(hk) | |
for i, v in ipairs(hk) do | |
v:disable() | |
end | |
end | |
function usbDeviceCallback(data) | |
if (data["productName"] == "HHKB Professional") then | |
if (data["eventType"] == "added") then | |
enableHotkeys(hhkbHotkeys) | |
elseif (data["eventType"] == "removed") then | |
disableHotkeys(hhkbHotkeys) | |
end | |
end | |
end | |
hhkbWatcher = hs.usb.watcher.new(usbDeviceCallback) | |
hhkbWatcher:start() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment