Hammerspoon / hammerspoon

Staggeringly powerful macOS desktop automation with Lua
http://www.hammerspoon.org
MIT License
12.15k stars 587 forks source link

Reordering Spaces #3669

Closed jcarlos7121 closed 3 months ago

jcarlos7121 commented 3 months ago

Looked through the documentation but I don't find a way to be able to reorder Mac Desktop Spaces, other than create or remove spaces.

Tried also creating an applescript to simulate the drag n drop.

Is there a consistent way to achieve this?

dmgerman commented 3 months ago

I don't think Hammerspoon has a function to do it. Did Applescript work for you?

jcarlos7121 commented 3 months ago

Yeah, I didn't actually used Applescript, I just made the windows swap from one space to the other and changed focus.

Gives me the same results as what I want. Thanks anyway!

I leave the code I used in case someone wants to replicate a sort of "Reorder Spaces" kind of functionality.

-- Function to move all windows from one space to another
function moveWindowsBetweenSpaces(space1, space2)
    -- Get all windows in the first space
    local windows1 = hs.spaces.windowsForSpace(space1)
    -- Get all windows in the second space
    local windows2 = hs.spaces.windowsForSpace(space2)
    -- Move each window from the first space to the second space
    for _, win in ipairs(windows1) do
        hs.spaces.moveWindowToSpace(win, space2)
    end
    -- Move each window from the second space to the first space
    for _, win in ipairs(windows2) do
        hs.spaces.moveWindowToSpace(win, space1)
    end
    -- Move to the second space
    hs.spaces.gotoSpace(space2)
end
-- Function to get the current space
function getCurrentSpace()
    local currentScreen = hs.screen.mainScreen()
    local currentSpace = hs.spaces.activeSpaceOnScreen(currentScreen)
    return currentSpace
end
-- Function to get all spaces
function getAllSpaces()
    local currentScreen = hs.screen.mainScreen()
    local allSpaces = hs.spaces.spacesForScreen(currentScreen)
    local nonFullScreenSpaces = {}
    for _, space in ipairs(allSpaces) do
        if hs.spaces.spaceType(space) == "user" then
            table.insert(nonFullScreenSpaces, space)
        end
    end
    return nonFullScreenSpaces
end
-- Move all windows to the previous space
hs.hotkey.bind({"ctrl", "shift"}, "U", function()
  local allSpaces = getAllSpaces()
  local currentSpace = getCurrentSpace()
  local currentIndex = hs.fnutils.indexOf(allSpaces, currentSpace)
  if currentIndex and currentIndex < #allSpaces then
    local nextSpace = allSpaces[currentIndex + 1]
    moveWindowsBetweenSpaces(currentSpace, nextSpace)
  else
    hs.alert.show("No next space available")
  end
end)
-- Move all windows to the previous space
hs.hotkey.bind({"ctrl", "shift"}, "Y", function()
  local allSpaces = getAllSpaces()
  local currentSpace = getCurrentSpace()
  local currentIndex = hs.fnutils.indexOf(allSpaces, currentSpace)
  if currentIndex and currentIndex > 1 then
    local previousSpace = allSpaces[currentIndex - 1]
    moveWindowsBetweenSpaces(currentSpace, previousSpace)
  else
    hs.alert.show("No previous space available")
  end
end)