Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Q] How do I move the focus to the next/previous space? #3583

Open
NightMachinery opened this issue Jan 10, 2024 · 1 comment
Open

[Q] How do I move the focus to the next/previous space? #3583

NightMachinery opened this issue Jan 10, 2024 · 1 comment

Comments

@NightMachinery
Copy link

image

I want to be able to do the above using Hammerspoon. I took a look at Hammerspoon docs: hs.spaces, but I couldn't find a way to get ID of the "left"/"right" spaces.

@avegetablechicken
Copy link

local function getUserSpaces()
  local user_spaces = {}
  for _, screen in ipairs(hs.screen.allScreens()) do
    local spaces = hs.spaces.spacesForScreen(screen) or {}
    for _, space in ipairs(spaces) do
      if hs.spaces.spaceType(space) == "user" then
        table.insert(user_spaces, space)
      end
    end
  end
  return user_spaces
end

local function checkAndMoveWindowToSpace(space)
  local user_spaces = getUserSpaces()
  local nspaces = #user_spaces
  if nspaces > 1 then
    local curSpaceID = hs.spaces.focusedSpace()
    local index = hs.fnutils.indexOf(user_spaces, curSpaceID)
    local targetIdx = space == "r" and index + 1 or index - 1
    if 1 <= targetIdx and targetIdx <= nspaces then
      local win = hs.window.focusedWindow()
      hs.spaces.moveWindowToSpace(win, user_spaces[targetIdx])
      hs.spaces.gotoSpace(user_spaces[targetIdx])
      local screenUUID = hs.spaces.spaceDisplay(user_spaces[targetIdx])
      if win:screen():getUUID() ~= screenUUID then
        focusScreen(hs.screen.find(screenUUID))
      end
      win:focus()
    end
  else
    hs.alert.show("Only ONE User Space")
  end
end

My configuration to move focused window to each adjacent space. I get ID of the "left"/"right" spaces by travesing.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants