Created
December 4, 2024 17:40
-
-
Save baptistemesta/88503e743b2ca32bf9805e50a3ec85e9 to your computer and use it in GitHub Desktop.
Hammerspoon config for moving windows
This file contains hidden or 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
-- Set up the logger | |
local log = hs.logger.new('WindowMover', 'info') | |
-- Function to get spaceId by space name | |
function getSpaceIdByName(spaceName) | |
local spaceNames = hs.spaces.missionControlSpaceNames() | |
for uuid, desktops in pairs(spaceNames) do | |
log.i("UUID: " .. uuid) -- Log the UUID | |
for index, name in pairs(desktops) do | |
log.i("Index: " .. index .. ", Name: " .. tostring(name)) -- Log the index and name | |
if string.find(name, spaceName) then | |
log.i("Found spaceId for " .. spaceName .. ": " .. index) | |
return index | |
end | |
end | |
end | |
log.w("Space not found: " .. spaceName) | |
return nil | |
end | |
-- Function to move focused window to a specific space | |
function moveFocusedWindowToSpace(spaceNumber) | |
local spaceName = "Desktop " .. spaceNumber | |
log.i("Attempting to move window to " .. spaceName) | |
local spaceId = getSpaceIdByName(spaceName) | |
if spaceId then | |
local focusedWindow = hs.window.focusedWindow() | |
if focusedWindow then | |
log.i("Moving window " .. focusedWindow:title() .. " to spaceId " .. spaceId) | |
hs.spaces.moveWindowToSpace(focusedWindow:id(), spaceId) | |
else | |
log.w("No focused window") | |
hs.alert.show("No focused window") | |
end | |
else | |
log.w("Space not found: " .. spaceName) | |
hs.alert.show("Space not found: " .. spaceName) | |
end | |
end | |
-- Bind keys alt + shift + 1-4 | |
for i = 1, 4 do | |
hs.hotkey.bind({"alt", "shift"}, tostring(i), function() | |
log.i("Hotkey pressed: alt + shift + " .. i) | |
moveFocusedWindowToSpace(i) | |
end) | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment