From 768e9ad516125034fd2f9cee62da96323e12654f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E8=B0=AD=E9=B9=8F=28=E6=B7=B1=E5=9C=B3=E8=81=94=E5=8F=8B?= =?UTF-8?q?=E7=A7=91=E6=8A=80=E6=9C=89=E9=99=90=E5=85=AC=E5=8F=B8=E5=88=9B?= =?UTF-8?q?=E6=96=B0=E4=BA=A7=E5=93=81=E7=BE=A4=E6=95=B0=E5=AD=97=E8=90=A5?= =?UTF-8?q?=E9=94=80=E4=BA=A7=E5=93=81=E4=B8=AD=E5=BF=83=29?= Date: Sat, 25 Feb 2023 13:24:12 +0800 Subject: [PATCH] =?UTF-8?q?new:=20=E5=A4=A7=E9=87=8F=E4=BF=AE=E6=94=B9?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .gitignore | 3 +- init.lua | 17 ++++---- modules/hotkey.lua | 3 +- modules/launcher.lua | 78 ++++++++++++++++----------------- modules/windows.lua | 102 +++++++++++++++++++++++-------------------- timesync/timesync.sh | 0 6 files changed, 107 insertions(+), 96 deletions(-) mode change 100755 => 100644 timesync/timesync.sh diff --git a/.gitignore b/.gitignore index 59ece68..5b44810 100644 --- a/.gitignore +++ b/.gitignore @@ -1,4 +1,5 @@ /.idea/ /*.zip .idea/ -build/ \ No newline at end of file +build/ +.DS_Store \ No newline at end of file diff --git a/init.lua b/init.lua index 448be99..3948aa9 100644 --- a/init.lua +++ b/init.lua @@ -3,14 +3,15 @@ package.path = hs.configdir .. '/?.lua;' .. hs.configdir .. '/?/init.lua;' .. hs require "modules/reload" require "modules/hotkey" -require "modules/commons" -require "modules/system" +-- require "modules/commons" +-- require "modules/system" require "modules/windows" require "modules/launcher" -require "modules/systemInfo" -require "modules/snippet" -require "modules/timesync" -require "modules/caffeine" -require "modules/inputstat" +-- require "modules/systemInfo" +-- require "modules/snippet" +-- require "modules/timesync" +-- require "modules/caffeine" +-- require "modules/inputstat" require "modules/dict" -require "modules/jsonFormat" \ No newline at end of file +-- require "modules/jsonFormat" + diff --git a/modules/hotkey.lua b/modules/hotkey.lua index f550982..045e6fd 100644 --- a/modules/hotkey.lua +++ b/modules/hotkey.lua @@ -2,4 +2,5 @@ hyper = {"cmd", "ctrl", "alt"} hyperShift = {"alt", "shift"} hyperCtrl = {"alt", "ctrl"} hyperAlt = {"ctrl", "alt", "shift"} -hyperCmd = {"alt", "cmd"} \ No newline at end of file +hyperCmd = {"alt", "cmd"} +f19 = {'cmd','alt','shift','ctrl'} \ No newline at end of file diff --git a/modules/launcher.lua b/modules/launcher.lua index 0a1b393..33a01de 100644 --- a/modules/launcher.lua +++ b/modules/launcher.lua @@ -1,46 +1,46 @@ -local hotkey = require "hs.hotkey" -local grid = require "hs.grid" -local window = require "hs.window" -local application = require "hs.application" -local appfinder = require "hs.appfinder" -local fnutils = require "hs.fnutils" +-- http://qtdebug.com/mac-hammerspoon/ +-- 键和应用对 +-- 提示: 数字作为键,需要使用 [Number] 的格式 -grid.setMargins({0, 0}) - -applist = { - {shortcut = 'A',appname = 'Launchpad'}, - {shortcut = 'C',appname = 'calendar'}, - {shortcut = 'D',appname = 'Disk Utility'}, - {shortcut = 'E',appname = 'Finder'}, - {shortcut = 'G',appname = 'Google Chrome'}, - {shortcut = 'Q',appname = 'Activity Monitor'}, - {shortcut = 'T',appname = 'Terminal'}, - {shortcut = 'S',appname = 'System Preferences'}, - {shortcut = 'Z',appname = 'Calculator'}, +local KEY_APP_PAIRS = { + [5] = "QSpace Pro.app", + Q = "QQ.app",W = "WeChat.app",E = "Microsoft Edge.app",T = "iTerm.app",Y = "语雀.app",U = "Notion.app",I = "IINA.app",O = "Obsidian.app", P = "Clash Verge.app", + A = "ILink.app",S = "WebStorm.app",F = "Finder.app",G = "ChatGPT.app",H = "HBuilderX.app",K = "Spotify", L = "NeteaseMusic.app", + Z = "QSpace Pro.app",X = "Xcode.app",C = "Google Chrome.app",V = "Visual Studio Code.app",B = "wechatwebdevtools.app",N = "/System/Applications/Notes.app",M = "Telegram.app", + + -- [2] = "Notable.app", + -- [3] = "Typora.app", } +-- 修饰键 这里设置为 f19 +local MOFIFER_KEY = f19 +-- local MOFIFER_KEY = "alt" -fnutils.each(applist, function(entry) - hotkey.bind({'ctrl', 'shift'}, entry.shortcut, entry.appname, function() - application.launchOrFocus(entry.appname) - -- toggle_application(applist[i].appname) - end) +-- 显示 Finder: +-- hs.hotkey.bind(MOFIFER_KEY, "1", function() +-- hs.application.open("/System/Library/CoreServices/Finder.app") +-- hs.application.get("com.apple.finder"):setFrontmost(true) +-- end) + +-- 重新加载配置 +hs.hotkey.bind(f19, "R", function() + hs.reload() end) --- Toggle an application between being the frontmost app, and being hidden -function toggle_application(_app) - local app = appfinder.appFromName(_app) - if not app then - application.launchOrFocus(_app) - return - end - local mainwin = app:mainWindow() - if mainwin then - if mainwin == window.focusedWindow() then - mainwin:application():hide() - else - mainwin:application():activate(true) - mainwin:application():unhide() - mainwin:focus() - end +-------------------------------------------------------------------------------------- +-- 按下 "F19+键" 会打开或激活对应的应用,如果应用不是绝对路径,则指的是 /Applications 中的应用 -- +-------------------------------------------------------------------------------------- +function bindAppWithHotkey(keyAppPairs) + + + -- 另一种方式 + for key, app in pairs(keyAppPairs) do + hs.hotkey.bind(MOFIFER_KEY, key .. "", function() + -- hs.application.open(app) -- 这种方式必须要app 路径 + hs.alert.show(app) + hs.application.launchOrFocus(app) -- 这种可以只传入app name,也可以传入路径 + end) end end + +bindAppWithHotkey(KEY_APP_PAIRS) + diff --git a/modules/windows.lua b/modules/windows.lua index 7cba70c..7cc588b 100644 --- a/modules/windows.lua +++ b/modules/windows.lua @@ -11,11 +11,12 @@ local fnutils = require "hs.fnutils" local geometry = require "hs.geometry" local mouse = require "hs.mouse" +local MOFIFER_KEY_HALF = f19 -- default 0.2 -window.animationDuration = 0 +window.animationDuration = 0.1 -- left half -hotkey.bind(hyper, "Left", function() +hotkey.bind(MOFIFER_KEY_HALF, "Left", function() if window.focusedWindow() then window.focusedWindow():moveToUnit(layout.left50) else @@ -24,37 +25,37 @@ hotkey.bind(hyper, "Left", function() end) -- right half -hotkey.bind(hyper, "Right", function() +hotkey.bind(MOFIFER_KEY_HALF, "Right", function() window.focusedWindow():moveToUnit(layout.right50) end) -- top half -hotkey.bind(hyper, "Up", function() +hotkey.bind(MOFIFER_KEY_HALF, "Up", function() window.focusedWindow():moveToUnit'[0,0,100,50]' end) -- bottom half -hotkey.bind(hyper, "Down", function() +hotkey.bind(MOFIFER_KEY_HALF, "Down", function() window.focusedWindow():moveToUnit'[0,50,100,100]' end) -- left top quarter -hotkey.bind(hyperAlt, "Left", function() +hotkey.bind(MOFIFER_KEY_HALF, "[", function() window.focusedWindow():moveToUnit'[0,0,50,50]' end) -- right bottom quarter -hotkey.bind(hyperAlt, "Right", function() +hotkey.bind(MOFIFER_KEY_HALF, "'", function() window.focusedWindow():moveToUnit'[50,50,100,100]' end) -- right top quarter -hotkey.bind(hyperAlt, "Up", function() +hotkey.bind(MOFIFER_KEY_HALF, "]", function() window.focusedWindow():moveToUnit'[50,0,100,50]' end) -- left bottom quarter -hotkey.bind(hyperAlt, "Down", function() +hotkey.bind(MOFIFER_KEY_HALF, ";", function() window.focusedWindow():moveToUnit'[0,50,50,100]' end) @@ -69,7 +70,7 @@ hotkey.bind(hyper, 'C', function() end) -- maximize window -hotkey.bind(hyper, 'M', function() toggle_maximize() end) +hotkey.bind(MOFIFER_KEY_HALF, 'return', function() window.focusedWindow(): maximize() end) -- defines for window maximize toggler local frameCache = {} @@ -86,34 +87,23 @@ function toggle_maximize() end -- display a keyboard hint for switching focus to each window -hotkey.bind(hyperShift, '/', function() - hints.windowHints() - -- Display current application window - -- hints.windowHints(hs.window.focusedWindow():application():allWindows()) -end) +-- hotkey.bind(hyperShift, '/', function() +-- hints.windowHints() +-- -- Display current application window +-- -- hints.windowHints(hs.window.focusedWindow():application():allWindows()) +-- end) -- switch active window -hotkey.bind(hyperShift, "H", function() - window.switcher.nextWindow() -end) +-- hotkey.bind(hyperShift, "H", function() +-- window.switcher.nextWindow() +-- end) --- move active window to previous monitor -hotkey.bind(hyperShift, "Left", function() - window.focusedWindow():moveOneScreenWest() -end) --- move active window to next monitor -hotkey.bind(hyperShift, "Right", function() - window.focusedWindow():moveOneScreenEast() -end) - --- move cursor to previous monitor -hotkey.bind(hyperCtrl, "Right", function () - focusScreen(window.focusedWindow():screen():previous()) -end) - --- move cursor to next monitor -hotkey.bind(hyperCtrl, "Left", function () +-- move cursor to previous/next monitor +-- hotkey.bind(hyperCtrl, "Right", function () +-- focusScreen(window.focusedWindow():screen():previous()) +-- end) +hotkey.bind(f19, "`", function () focusScreen(window.focusedWindow():screen():next()) end) @@ -153,19 +143,37 @@ moveto = function(win, n) end end - --- move cursor to monitor 1 and maximize the window -hotkey.bind(hyperShift, "1", function() - local win = window.focusedWindow() - moveto(win, 1) +-- cycled move active window to another screen +hs.hotkey.bind( f19, 'tab', function() + -- get the focused window + local win = hs.window.focusedWindow() + -- get the screen where the focused window is displayed, a.k.a. current screen + local screen = win:screen() + -- compute the unitRect of the focused window relative to the current screen + -- and move the window to the next screen setting the same unitRect + win:move(win:frame():toUnitRect(screen:frame()), screen:next(), true, 0) end) -hotkey.bind(hyperShift, "2", function() - local win = window.focusedWindow() - moveto(win, 2) -end) +-- -- move active window to monitor 1 and maximize the window +-- hotkey.bind(f19, "1", function() +-- local win = window.focusedWindow() +-- moveto(win, 1) +-- end) -hotkey.bind(hyperShift, "3", function() - local win = window.focusedWindow() - moveto(win, 3) -end) \ No newline at end of file +-- hotkey.bind(f19, "2", function() +-- local win = window.focusedWindow() +-- moveto(win, 2) +-- end) + +-- hotkey.bind(f19, "3", function() +-- local win = window.focusedWindow() +-- moveto(win, 3) +-- end) + +-- -- move active window to previous/next monitor +-- hotkey.bind(MOFIFER_KEY_HALF, '-', function() +-- window.focusedWindow():moveOneScreenWest():maximize() +-- end) +-- hotkey.bind(MOFIFER_KEY_HALF, '=', function() +-- window.focusedWindow():moveOneScreenEast():maximize() +-- end) \ No newline at end of file diff --git a/timesync/timesync.sh b/timesync/timesync.sh old mode 100755 new mode 100644