hammerspoon icon indicating copy to clipboard operation
hammerspoon copied to clipboard

hs.spaces.moveWindowToSpace not working

Open akarsai opened this issue 1 year ago • 10 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)

akarsai avatar May 13 '24 08:05 akarsai

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

sidkang avatar May 15 '24 03:05 sidkang

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: https://github.com/koekeishiya/yabai/issues/2240#issuecomment-2112484610

mattfloat avatar May 16 '24 12:05 mattfloat

Looks like they were able to achieve the same functionality in another way: https://github.com/koekeishiya/yabai/commit/7bacdd59bdf39b53012e024b442d61913095794e

cmsj avatar May 16 '24 14:05 cmsj

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

SilverEzhik avatar May 20 '24 15:05 SilverEzhik

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

asmagill avatar May 20 '24 19:05 asmagill

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 😬

cmsj avatar May 20 '24 19:05 cmsj

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(): https://github.com/mogenson/PaperWM.spoon/commit/3b48e6b69be14101de6f824fe9f3a3d005015acf

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.

mogenson avatar May 21 '24 00:05 mogenson

See this commit for a SIP friendly (enabled) solution https://github.com/koekeishiya/yabai/commit/98bbdbd1363f27d35f09338cded0de1ec010d830

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

asmvik avatar May 21 '24 00:05 asmvik

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!

asmagill avatar May 21 '24 10:05 asmagill

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

asmvik avatar May 21 '24 10:05 asmvik

Closing since the PR is merged.

cmsj avatar Aug 05 '24 15:08 cmsj

very sorry to annoy again: unfortunately, yesterday i updated to MacOS Sequoia 15.0 Beta (24A5298h). the new fix does not work for me and the issue remains (hs.spaces.moveWindowToSpace returns true even though the window is not moved)

akarsai avatar Aug 06 '24 08:08 akarsai

very sorry to annoy again: unfortunately, yesterday i updated to MacOS Sequoia 15.0 Beta (24A5298h). the new fix does not work for me and the issue remains (hs.spaces.moveWindowToSpace returns true even though the window is not moved)

Does Yabai work in 15beta?

cmsj avatar Aug 06 '24 09:08 cmsj

According to https://github.com/koekeishiya/yabai/blob/master/CHANGELOG.md

• Moving windows to other spaces requires SIP to be disabled on macOS Sequoia https://github.com/koekeishiya/yabai/issues/2324 https://github.com/koekeishiya/yabai/issues/2331
• Updated scripting-addition mach loader/injection and payload to work for macOS Sequoia Beta 1, 2, 3, and 4 https://github.com/koekeishiya/yabai/issues/2324 https://github.com/koekeishiya/yabai/issues/2331

So, yes, but not in a way we can easily mimic.

I'll keep on eye on it; IIRC, when Sonoma first came out, injection was the only fix as well, so it might change. Or it might be time to admit defeat and move spaces to more of a yabai wrapper.

asmagill avatar Aug 06 '24 10:08 asmagill

There does not appear to be other ways.

All 3 functions I used in my Sonoma 14.5/6 workaround got NOP'd by Apple in Sequoia-- they nuked the implementation in the WindowServer.

The remaining functions all have rights checking on the WindowServer connection. You either need to be the owner of the window, or the system universal owner (Dock.app).

asmvik avatar Aug 06 '24 14:08 asmvik

Is moveWindowToSpace working for people in the v 1.0.0 release on Sonoma 14.5? I find it still does not (aside from the HS console window).

jdtsmith avatar Sep 11 '24 21:09 jdtsmith

Is moveWindowToSpace working for people in the v 1.0.0 release on Sonoma 14.5? I find it still does not (aside from the HS console window).

´moveWindowToSpace´ works as expected in the latest version of macOS Sonoma; however, it does not in macOS Sequoia.

franzbu avatar Sep 17 '24 13:09 franzbu

@franzbu can confirm is does not work, is not working for me either

jcarlos7121 avatar Sep 19 '24 14:09 jcarlos7121

moveWindowToSpace works for me on Sonoma, but not Sequoia. Both v1.0.0. It still returns true, which means it didn't hit any errors/edge-cases as mentioned in https://github.com/Hammerspoon/hammerspoon/blob/08cb81357dae2e9ecce33cba1a627b9dbc09af04/extensions/spaces/libspaces.m#L189

purajit avatar Sep 22 '24 17:09 purajit

https://github.com/ianyh/Amethyst/discussions/1682

This seems relevant to Sequoia :/

purajit avatar Sep 30 '24 18:09 purajit

For me moveWindowToSpace is not even working in Sonoma, so I switched back to my classic technique of "click on window's title bar, Control-arrow, release". You can see the code here. Would appreciate if anyone could test in Sequoia.

jdtsmith avatar Sep 30 '24 21:09 jdtsmith

For me moveWindowToSpace is not even working in Sonoma, so I switched back to my classic technique of "click on window's title bar, Control-arrow, release". You can see the code here. Would appreciate if anyone could test in Sequoia.

@jdtsmith Hi, I can confirm this hack works on macOS Sequoia 15.0.1, thank you so much!

RichDom2185 avatar Oct 16 '24 07:10 RichDom2185

Hello,

is this intentionally closed? Is there a fix for this?

Moving window to another screen worked for me before upgrading it to Sequoia.

BraveDistribution avatar Nov 04 '24 14:11 BraveDistribution

For others coming for the same reason, this issue was resolved with Sonoma (14.5.x) after Apple changed the API.

Then Apple disabled that API in Sequoia (15.x). You can follow the discussion in #3698.

Hello,

is this intentionally closed? Is there a fix for this?

Moving window to another screen worked for me before upgrading it to Sequoia.

steady137 avatar Nov 05 '24 22:11 steady137