Skip to content

Instantly share code, notes, and snippets.

@atdt
Created September 6, 2020 19:48
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save atdt/b723f0eb9e3254533b316d363ee74eb0 to your computer and use it in GitHub Desktop.
Save atdt/b723f0eb9e3254533b316d363ee74eb0 to your computer and use it in GitHub Desktop.
hs.loadSpoon("Lunette")
spoon.Lunette:bindHotkeys()
--[[
- menubar items
- print(hs.inspect(...))
hs.alert.show(string.format("mode: %s x %s", mode.w, mode.h))
--]]
local function sendSystemKey(key)
hs.eventtap.event.newSystemKeyEvent(key, true):post()
hs.eventtap.event.newSystemKeyEvent(key, false):post()
end
local volume = {
up = function() sendSystemKey("SOUND_UP") end,
down = function() sendSystemKey("SOUND_DOWN") end,
mute = function() sendSystemKey("MUTE") end
}
hs.hotkey.bind({}, "f10", volume.mute)
hs.hotkey.bind({}, "f11", volume.down, nil, volume.down)
hs.hotkey.bind({}, "f12", volume.up, nil, volume.up)
function setDockAutohide(enabled)
ok = hs.osascript.applescript(
[[tell application "System Events" to set the autohide of the dock]] ..
[[ preferences to ]] .. tostring(enabled))
end
function autoSetDockAutohide()
-- Disable dock auto-hide when a wide screen is attached.
local maxWidth = 0
for _, screen in ipairs(hs.screen.allScreens()) do
local mode = screen:currentMode()
maxWidth = math.max(maxWidth, mode.w)
end
setDockAutohide(maxWidth <= 1440)
end
autoSetDockAutohide()
local screenWatcher = hs.screen.watcher.newWithActiveScreen(autoSetDockAutohide)
screenWatcher:start()
-- Run every 60 seconds because we sometimes miss events.
hs.timer.new(60, autoSetDockAutohide):start()
--[[ Automatically set USB Audio Device as default audio device on init and on hot plug ]] --
USB_AUDIO_DEVICE = {
productID = 20,
productName = "USB Audio Device",
vendorID = 3468,
vendorName = "C-Media Electronics Inc."
}
function isUSBAudioDevice(device)
return device.productID == USB_AUDIO_DEVICE.productID and device.vendorID ==
USB_AUDIO_DEVICE.vendorID
end
function setDefaultAudioDevice(deviceName)
local device = nil;
device = hs.audiodevice.findInputByName(deviceName)
if device ~= nil then device:setDefaultInputDevice() end
device = hs.audiodevice.findOutputByName(deviceName)
if device ~= nil then device:setDefaultOutputDevice() end
end
for _, device in pairs(hs.usb.attachedDevices()) do
if isUSBAudioDevice(device) then setDefaultAudioDevice("USB Audio Device") end
end
local usbWatcher = hs.usb.watcher.new(function(data)
if data.eventType == "added" and isUSBAudioDevice(data) then
setDefaultAudioDevice("USB Audio Device")
end
end)
usbWatcher:start()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment