使用 hammerspoon 和 spaces 模块将 window 移动到新的 space
Using hammerspoon and the spaces module to move window to new space
我已经从 https://github.com/asmagill/hs._asm.undocumented.spaces 安装了 "undocumented spaces" 模块。特别是,它提供了一种方法 moveWindowToSpace
,我试图使用该方法绑定 cmd+1
以使用以下方法将当前 window 移动到 space 1:
local spaces = require("hs._asm.undocumented.spaces")
function MoveWindowToSpace(sp)
local spaceID = spaces.query()[sp]
spaces.moveWindowToSpace(hs.window.focusedWindow():id(), spaceID)
spaces.changeToSpace(spaceID)
end
hs.hotkey.bind({"cmd"}, "1",function() MoveWindowToSpace(1) end)
这在将 window 移动到新的 space 的意义上起作用,但是,space 似乎处于伪随机顺序。
有谁知道如何正确地将 spaces.query()
返回的 spaceID
s 映射到实际的 spaces?
在 spaces 模块的作者的一些提示之后,我想到了以下内容,这似乎可以解决问题。
local spaces = require("hs._asm.undocumented.spaces")
-- move current window to the space sp
function MoveWindowToSpace(sp)
local win = hs.window.focusedWindow() -- current window
local uuid = win:screen():spacesUUID() -- uuid for current screen
local spaceID = spaces.layout()[uuid][sp] -- internal index for sp
spaces.moveWindowToSpace(win:id(), spaceID) -- move window to new space
spaces.changeToSpace(spaceID) -- follow window to new space
end
hs.hotkey.bind(hyper, '1', function() MoveWindowToSpace(1) end)
之前我使用的是 https://github.com/Hammerspoon/hammerspoon/issues/235 代码的一个变体,它连接到 osx 定义的用于切换空格的热键,但上面的代码要快得多!
对于那些在 2020 年寻找更简单的工作解决方案的人,您可以使用苹果脚本:
function moveWindowOneSpace(direction)
local keyCode = direction == "left" and 123 or 124
return hs.osascript.applescript([[
tell application "System Events"
keystroke (key code ]] .. keyCode .. [[ using control down)
end tell
]])
end
我尝试使用 this issue 的解决方案,但没有用。另一方面,apple scripts 很有魅力。
由于未记录的空间已移至spaces,新代码如下(部分行可以合并,但我喜欢拆分操作的清晰度):
spaces = require("hs.spaces")
-- move current window to the space sp
function MoveWindowToSpace(sp)
local win = hs.window.focusedWindow() -- current window
local cur_screen = hs.screen.mainScreen()
local cur_screen_id = cur_screen:getUUID()
local all_spaces=spaces.allSpaces()
local spaceID = all_spaces[cur_screen_id][sp]
spaces.moveWindowToSpace(win:id(), spaceID)
spaces.gotoSpace(spaceID) -- follow window to new space
end
hs.hotkey.bind(hyper, '1', function() MoveWindowToSpace(1) end)
hs.hotkey.bind(hyper, '2', function() MoveWindowToSpace(2) end)
hs.hotkey.bind(hyper, '3', function() MoveWindowToSpace(3) end)
我已经从 https://github.com/asmagill/hs._asm.undocumented.spaces 安装了 "undocumented spaces" 模块。特别是,它提供了一种方法 moveWindowToSpace
,我试图使用该方法绑定 cmd+1
以使用以下方法将当前 window 移动到 space 1:
local spaces = require("hs._asm.undocumented.spaces")
function MoveWindowToSpace(sp)
local spaceID = spaces.query()[sp]
spaces.moveWindowToSpace(hs.window.focusedWindow():id(), spaceID)
spaces.changeToSpace(spaceID)
end
hs.hotkey.bind({"cmd"}, "1",function() MoveWindowToSpace(1) end)
这在将 window 移动到新的 space 的意义上起作用,但是,space 似乎处于伪随机顺序。
有谁知道如何正确地将 spaces.query()
返回的 spaceID
s 映射到实际的 spaces?
在 spaces 模块的作者的一些提示之后,我想到了以下内容,这似乎可以解决问题。
local spaces = require("hs._asm.undocumented.spaces")
-- move current window to the space sp
function MoveWindowToSpace(sp)
local win = hs.window.focusedWindow() -- current window
local uuid = win:screen():spacesUUID() -- uuid for current screen
local spaceID = spaces.layout()[uuid][sp] -- internal index for sp
spaces.moveWindowToSpace(win:id(), spaceID) -- move window to new space
spaces.changeToSpace(spaceID) -- follow window to new space
end
hs.hotkey.bind(hyper, '1', function() MoveWindowToSpace(1) end)
之前我使用的是 https://github.com/Hammerspoon/hammerspoon/issues/235 代码的一个变体,它连接到 osx 定义的用于切换空格的热键,但上面的代码要快得多!
对于那些在 2020 年寻找更简单的工作解决方案的人,您可以使用苹果脚本:
function moveWindowOneSpace(direction)
local keyCode = direction == "left" and 123 or 124
return hs.osascript.applescript([[
tell application "System Events"
keystroke (key code ]] .. keyCode .. [[ using control down)
end tell
]])
end
我尝试使用 this issue 的解决方案,但没有用。另一方面,apple scripts 很有魅力。
由于未记录的空间已移至spaces,新代码如下(部分行可以合并,但我喜欢拆分操作的清晰度):
spaces = require("hs.spaces")
-- move current window to the space sp
function MoveWindowToSpace(sp)
local win = hs.window.focusedWindow() -- current window
local cur_screen = hs.screen.mainScreen()
local cur_screen_id = cur_screen:getUUID()
local all_spaces=spaces.allSpaces()
local spaceID = all_spaces[cur_screen_id][sp]
spaces.moveWindowToSpace(win:id(), spaceID)
spaces.gotoSpace(spaceID) -- follow window to new space
end
hs.hotkey.bind(hyper, '1', function() MoveWindowToSpace(1) end)
hs.hotkey.bind(hyper, '2', function() MoveWindowToSpace(2) end)
hs.hotkey.bind(hyper, '3', function() MoveWindowToSpace(3) end)