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

hs.spaces.moveWindowToSpace not working #3636

Open
akarsai opened this issue May 13, 2024 · 10 comments
Open

hs.spaces.moveWindowToSpace not working #3636

akarsai opened this issue May 13, 2024 · 10 comments

Comments

@akarsai
Copy link

akarsai commented May 13, 2024

the following code snippet used to work for me, but stopped working a few weeks ago. the snippet is responsible for finding the current terminal window, moving it to the active space, and activating it. unfortunately, the window is not moved by hammerspoon. the linehs.spaces.moveWindowToSpace(app:mainWindow(), currentSpace) is not working as it should, and returns true even though the window was not succesfully moved.

can anyone reproduce the issue and/or does anyone have any ideas on how to fix this? unfortunately, i do not exactly know what i have changed, it could be also be a macOS update that is at fault here. i use macOS 14.5 Beta (23F5064f).

-- quickly open kitty (terminal)
hs.hotkey.bind({"cmd"}, "g", function()
    local windowPosition = '0.2,0.4,0.8,0.9'
    
    -- Get current space 
    local currentSpace = hs.spaces.focusedSpace()
    
    -- Get kitty app
    local app = hs.application.get("kitty")
    
    -- If app already open:
    if app then
        
        -- If no main window, then open a new window
        if not app:mainWindow() then
            app:selectMenuItem("New OS Window", true)
            
            hs.timer.doAfter(.05,function ()
                app:mainWindow():raise()
                app:mainWindow():moveToUnit(windowPosition)
                end)
        
        -- If app is already in front, then hide it
        elseif app:isFrontmost() then
            app:hide()
        
        -- If there is a main window somewhere, bring it to current space and to front
        else
            -- First move the main window to the current space
            hs.spaces.moveWindowToSpace(app:mainWindow(), currentSpace)
            
            -- Activate the app
            app:activate()
            
            --Raise the main window and position correctly
            app:mainWindow():raise()
            app:mainWindow():moveToUnit(windowPosition)
        end
    
    -- If app not open, open it
    else
        hs.application.launchOrFocus("kitty")
        
        hs.timer.doAfter(.2,function () 
            app = hs.application.get("kitty") 
            app:mainWindow():raise()
            app:mainWindow():moveToUnit(windowPosition)
            end)
    end

end)
@akarsai akarsai changed the title hs.spaces.moveWindowToSpace not working for me hs.spaces.moveWindowToSpace not working May 13, 2024
@sidkang
Copy link

sidkang commented May 15, 2024

Facing the same issue, 14.4.1 worked, but after 14.5, not any more.

@mattfloat
Copy link

It appears macOS 14.5 changed some functions to protected which could be the reason for our symptoms.
Yabai is facing the same issue. More details there: koekeishiya/yabai#2240 (comment)

@cmsj
Copy link
Member

cmsj commented May 16, 2024

Looks like they were able to achieve the same functionality in another way: koekeishiya/yabai@7bacdd5

@SilverEzhik
Copy link

I've observed that it still works for me with Hammerspoon's own windows, but not with any other app's windows.

@asmagill
Copy link
Member

@cmsj, it looks to me like yabai added the function to their script injection payload; without doing something similiar (requiring SIP changes and an elevated permissions helper app) I'm not sure we'll be able to accomplish the same.

It might be better to see how closely we can work with yabai and come up with a module/spoon/process for utilizing it as our "helper" or "companion" app.

@cmsj
Copy link
Member

cmsj commented May 20, 2024

Aha. Yeah closer integration with yabai is an interesting option. We could also just get out of the business of pretending that Apple wants us interacting with Spaces in the first place 😬

@mogenson
Copy link

If hammerspoon can't directly invoke an Apple method to move a window to a space, it would still be nice to provide a utility function to accomplish this same task manually. Similar to how hs.spaces.gotoSpace() opens Mission Control and programatically clicks the desired space.

Here's a work-around replacement for hs.spaces.moveWindowToSpace(): mogenson/PaperWM.spoon@3b48e6b

It involves programmatically clicking a window's title bar, switching to a space, and dragging the window to the new space. This has the limitation that you can't move a window to or from a non-visible space, but it's better than nothing.

Note: I tried this with hs.window:move(), but I couldn't get this method to pick up and hold a window while the space transitions like a mouse click does. Still, it would be nice to accomplish this without having to hijack the user's mouse cursor.

@koekeishiya
Copy link

koekeishiya commented May 21, 2024

See this commit for a SIP friendly (enabled) solution koekeishiya/yabai@98bbdbd

A little note/mention/credit would be appreciated if my solution is used.

@asmagill
Copy link
Member

I can confirm @koekeishiya's suggestions seems to work (at least for now, curse you Apple!). I need to do some more testing and it will probably be a couple of days before I can put together a formal pull-request (though I'm fine if someone else wants to have a go at it sooner).

@koekeishiya, what type of notification would you consider appropriate? Normally, I will add comments in the source as to where specific changes/additions are from and add a line or two with links in the documentation (both internal to Hammerspoon and what is ultimately published in our pages) for the relevant module. If you'd like something more, or if there are other licensing concerns, let us know!

@koekeishiya
Copy link

@koekeishiya, what type of notification would you consider appropriate? Normally, I will add comments in the source as to where specific changes/additions are from and add a line or two with links in the documentation (both internal to Hammerspoon and what is ultimately published in our pages) for the relevant module. If you'd like something more, or if there are other licensing concerns, let us know!

That will do just fine.

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

8 participants