Hammerspoon / hammerspoon

Staggeringly powerful macOS desktop automation with Lua

Home Page:http://www.hammerspoon.org

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

hs.spaces.moveWindowToSpace not working

akarsai opened this issue · comments

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)

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

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)

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

commented

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

commented

@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.

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 😬

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.

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.

commented

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, 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.