当前位置: 首页 > 工具软件 > hammerspoon > 使用案例 >

MacOs 安装 HammerSpoon 窗口全屏、2分屏、4分屏

廉宇
2023-12-01

1,安装HammerSpoon

  • Hammerspoon-0.9.73.zip 解压,把Hammerspoon移动到应用程序

2,配置HammerSpoon

$ cd ~/.hammerspoon/
$ vim init.lua 
  • 目录结构
├── init.lua
└── modules
    ├── hotkey.lua
    └── windows.lua
  • init.lua配置
require "modules/hotkey"
require "modules/windows"
  • hotkey.lua配置
hyper = {"ctrl","cmd"}
  • windows.lua配置
require "hs.application"
local hotkey = require "hs.hotkey"
local window = require "hs.window"

-- 屏幕显示Hello world
hotkey.bind(hyper, "h", function()
  hs.alert("Hello World")
end)

-- 通知中心弹出Hello Hammerspoon!
hotkey.bind(hyper, "t", function()
  hs.notify.new({title="Hammerspoon", informativeText="Hello Hammerspoon!"}):send()
end)


-- 当前窗口全屏
hs.hotkey.bind(hyper, 'up', function()
    hs.grid.maximizeWindow()
end)

-- 当前窗口 2分屏 宽度1/2 左分屏
hs.hotkey.bind(hyper, "Left", function()
  local win = hs.window.focusedWindow()
    local f = win:frame()
    local screen = win:screen()
    local max = screen:frame()

    f.x = max.x
    f.y = max.y
    f.w = max.w / 2
    f.h = max.h
    win:setFrame(f)
end)

-- 当前窗口 2分屏 宽度1/2 右分屏
hs.hotkey.bind(hyper, "Right", function()
  local win = hs.window.focusedWindow()
    local f = win:frame()
    local screen = win:screen()
    local max = screen:frame()

    f.x = max.x + max.w/2
    f.y = max.y
    f.w = max.w / 2
    f.h = max.h
    win:setFrame(f)
end)


-- 当前窗口 4分屏 宽度1/2 高度1/2 左上分屏
hs.hotkey.bind(hyper,"1", function()
  local win = hs.window.focusedWindow()
    local f = win:frame()
    local screen = win:screen()
    local max = screen:frame()

    f.x = max.x 
    f.y = max.y
    f.w = max.w / 2
    f.h = max.h / 2
    win:setFrame(f)
end)

-- 当前窗口 4分屏 宽度1/2 高度1/2 左下分屏
hs.hotkey.bind(hyper,"2", function()
  local win = hs.window.focusedWindow()
    local f = win:frame()
    local screen = win:screen()
    local max = screen:frame()

    f.x = max.x
    f.y = max.y + max.h/2
    f.w = max.w / 2
    f.h = max.h / 2
    win:setFrame(f)
end)


-- 当前窗口 4分屏 宽度1/2 高度1/2 右上分屏
hs.hotkey.bind(hyper,"3", function()
  local win = hs.window.focusedWindow()
    local f = win:frame()
    local screen = win:screen()
    local max = screen:frame()

    f.x = max.x + max.w/2
    f.y = max.y 
    f.w = max.w / 2
    f.h = max.h / 2
    win:setFrame(f)
end)


-- 当前窗口 4分屏 宽度1/2 高度1/2 右下分屏
hs.hotkey.bind(hyper,"4", function()
  local win = hs.window.focusedWindow()
    local f = win:frame()
    local screen = win:screen()
    local max = screen:frame()

    f.x = max.x + max.w/2
    f.y = max.y + max.h/2
    f.w = max.w / 2
    f.h = max.h / 2
    win:setFrame(f)
end)

2,窗口分屏快捷键

快捷键窗口分屏
Ctrl + Cmd + ↑全屏
Ctrl + Cmd + ←2分屏 左分屏
Ctrl + Cmd + →2分屏 右分屏
Ctrl + Cmd + 14分屏 左上分屏
Ctrl + Cmd + 24分屏 左下分屏
Ctrl + Cmd + 34分屏 右上分屏
Ctrl + Cmd + 44分屏 右下分屏

参考:

  1. Hammerspoon
  2. hammerspoon下载
  3. Mac神器hammerspoon
  4. Hammerspoon API文档
  5. Hammerspoon-高效操作窗口和鼠标
 类似资料: