Last active
July 13, 2019 17:24
-
-
Save Jackliu91/c2a3976f713224166c78bc3d2ff38ce9 to your computer and use it in GitHub Desktop.
[Hammerspoon Config] my hammerspoon config file #lua
This file contains 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
--varibles | |
ctrl_alt = {"control", "alt"} | |
ctrl_cmd = {"control", "cmd"} | |
alt_cmd = {"alt", "cmd"} | |
--reload | |
hs.hotkey.bind(ctrl_cmd, "R", function() | |
hs.reload() | |
hs.alert.show("Config loaded") | |
end) | |
function reloadConfig(files) | |
doReload = false | |
for _, file in pairs(files) do | |
if (file:sub(-4) == ".lua") then | |
doReload = true | |
end | |
end | |
if (doReload) then | |
hs.reload() | |
end | |
end | |
myWatcher = hs.pathwatcher.new(os.getenv("HOME") .. "/.hammerspoon/", reloadConfig):start() | |
hs.alert.show("Config loaded") | |
--show console | |
hs.hotkey.bind(ctrl_cmd, "S", function() | |
local win = hs.console.hswindow() | |
if win then | |
win:focus() | |
end | |
end) | |
--Window Position | |
screen = hs.screen.mainScreen() | |
max = screen:frame() | |
origin_frame = {} | |
left50 = {max.x, max.y, max.w / 2, max.h} | |
right50 = {max.x + (max.w / 2), max.y, max.w / 2, max.h} | |
top50 = {max.x, max.y, max.w, max.h /2} | |
botom50 = {max.x, max.y + (max.h / 2), max.w, max.h / 2} | |
function moveWindow(layout) | |
return function() | |
local win = hs.window.focusedWindow() | |
local f = win:frame() | |
local id = win:id() | |
if (origin_frame[id] == nil) then | |
origin_frame[id] = hs.geometry.copy(f) | |
end | |
if (layout) then | |
win:setFrame(layout) | |
else | |
win:centerOnScreen() | |
end | |
end | |
end | |
function toggleMax() | |
local win = hs.window.focusedWindow() | |
local f = win:frame() | |
local id = win:id() | |
if (origin_frame[id]) then | |
win:setFrame(origin_frame[id]) | |
origin_frame[id] = nil | |
else | |
origin_frame[id] = hs.geometry.copy(f) | |
win:maximize() | |
end | |
end | |
hs.hotkey.bind(ctrl_alt, "Left", moveWindow(left50)) | |
hs.hotkey.bind(ctrl_alt, "Right", moveWindow(right50)) | |
hs.hotkey.bind(ctrl_alt, "Up", moveWindow(top50)) | |
hs.hotkey.bind(ctrl_alt, "Down", moveWindow(botom50)) | |
hs.hotkey.bind(ctrl_alt, "C", moveWindow(nil)) | |
hs.hotkey.bind(ctrl_alt, "Return", toggleMax) | |
hs.hotkey.bind(ctrl_alt, "F", function() hs.window.focusedWindow():toggleFullScreen() end) | |
--window size | |
hs.hotkey.bind(ctrl_alt, "J", function() local win = hs.window.focusedWindow() local size = win:size() win:setSize(hs.geometry.size(size.w, size.h + 30)) end) | |
hs.hotkey.bind(ctrl_alt, "K", function() local win = hs.window.focusedWindow() local size = win:size() win:setSize(hs.geometry.size(size.w, size.h - 30)) end) | |
hs.hotkey.bind(ctrl_alt, "H", function() local win = hs.window.focusedWindow() local size = win:size() win:setSize(hs.geometry.size(size.w - 30, size.h)) end) | |
hs.hotkey.bind(ctrl_alt, "L", function() local win = hs.window.focusedWindow() local size = win:size() win:setSize(hs.geometry.size(size.w + 30, size.h)) end) | |
hs.hotkey.bind(ctrl_alt, "U", function() local win = hs.window.focusedWindow() local size = win:size() win:setSize(hs.geometry.size(size.w - 30, size.h - (30 * size.h / size.w))) end) | |
hs.hotkey.bind(ctrl_alt, "N", function() local win = hs.window.focusedWindow() local size = win:size() win:setSize(hs.geometry.size(size.w + 30, size.h + (30 * size.h / size.w))) end) | |
--screen | |
hs.hotkey.bind(ctrl_cmd, "Left", function() hs.window.focusedWindow():moveOneScreenWest() end) | |
hs.hotkey.bind(ctrl_cmd, "Right", function() hs.window.focusedWindow():moveOneScreenEast() end) | |
--switch | |
switch_ui = {fontName = 'Helvetica Neue Light', showTitles = false, showSelectedThumbnail = true, selectedThumbnailSize = 256, showSelectedTitle = false, backgroundColor = {0.93, 0.93, 0.93, 0.80}, highlightColor = {0.80, 0.50, 0.00, 0.50}} | |
switcher = hs.window.switcher.new(nil, switch_ui) -- default windowfilter: only visible windows, all Spaces | |
hs.hotkey.bind('alt','tab',function()switcher:next()end) | |
hs.hotkey.bind('alt-shift','tab',function()switcher:previous()end) | |
--expose | |
all_ui = {fontName = 'Helvetica Neue Light', textSize = 28, showTitles = false, showThumbnails = false, maxHintLetters = 1, fitWindowsMaxIterations = 10, fitWindowsInBackground = false, otherSpacesStripWidth = 0.1, nonVisibleStripWidth = 0.1, textColor = {0.99, 0.16, 0.21, 0.90}, backgroundColor = {0.64, 1.00, 0.71, 0.60}, highlightColor = {0.73, 0.73, 0.73, 0.80}, closeModeBackgroundColor = {0.89, 0.30, 0.44, 0.60}, minimizeModeBackgroundColor = {0.36, 0.79, 0.96, 0.60}, nonVisibleStripBackgroundColor = {0.64, 1.00, 0.71, 0.60}, otherSpacesStripBackgroundColor = {0.64, 1.00, 0.71, 0.60}} | |
less_ui = hs.fnutils.copy(all_ui) | |
less_ui.textSize = 40 | |
less_ui.includeOtherSpaces = false | |
less_ui.showThumbnails = true | |
less_ui.nonVisibleStripWidth = 0.4 | |
less_ui.fitWindowsInBackground = true | |
all_expose = hs.expose.new(nil, all_ui) | |
less_expose = hs.expose.new(nil, less_ui) | |
hs.hotkey.bind(ctrl_cmd, 'W', function() all_expose:toggleShow() end) | |
hs.hotkey.bind(ctrl_cmd, 'E', function() less_expose:toggleShow() end) | |
--input method | |
local app_Chinese = {"QQ", "WeChat", "MWeb", "2Do", "Microsoft Word", "Microsoft PowerPoint", "Pages", "Keynote", "AliWangwang", "Day One"} | |
function appLayoutWacher(appName, eventType, appObject) | |
if (eventType == hs.application.watcher.activated) then | |
if (hs.fnutils.contains(app_Chinese, appName)) then | |
hs.keycodes.setMethod("Squirrel") | |
else | |
hs.keycodes.setLayout("U.S.") | |
end | |
end | |
end | |
appLayout = hs.application.watcher.new(appLayoutWacher) | |
appLayout:start() | |
--FullScreen when app is launched. | |
local app_fullscreen = {"QQLive", "MWeb", "Reeder", "PDF Expert", "2Do", "Pocket", "App Store", "iTunes", "Photos", "Calendar", "Mail", "Safari", "Google Chrome", "Finder", "Maps", "iBooks", "iTerm2"} | |
function appLaunchWacher(appName, eventType, appObject) | |
if(eventType == hs.application.watcher.launched) then | |
if (hs.fnutils.contains(app_fullscreen, appName)) then | |
local win = appObject:mainWindow() | |
if (win) then | |
win:maximize() | |
end | |
end | |
end | |
end | |
appLaunched = hs.application.watcher.new(appLaunchWacher) | |
appLaunched:start() | |
--audio | |
output = hs.audiodevice.findOutputByName("Built-in Output") | |
function headphoneUnpluged(uid, eventType, eventScope, eventElement) | |
if (eventType == "jack") then | |
-- unpluged headphone | |
if (output:jackConnected() == false) then | |
output:setOutputMuted(true) | |
-- pluged headphone | |
else | |
if (output:outputVolume() > 20) then | |
output:setOutputVolume(10) | |
end | |
end | |
end | |
end | |
output:watcherCallback(headphoneUnpluged) | |
output:watcherStart() | |
--iTunes | |
function iTunesControl(state) | |
return function() | |
if (state == "next") then | |
hs.itunes.next() | |
elseif (state == "previous") then | |
hs.itunes.previous() | |
elseif (state == "play") then | |
hs.itunes.playpause() | |
end | |
end | |
end | |
hs.hotkey.bind(alt_cmd, "I", iTunesControl("play")) | |
hs.hotkey.bind(alt_cmd, "J", iTunesControl("next")) | |
hs.hotkey.bind(alt_cmd, "K", iTunesControl("previous")) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment