Closed fiedl closed 6 years ago
Have you tried Space.all()
? According to documentation:
Returns all spaces, the first space in this array corresponds to the primary space (macOS 10.11+, returns an empty list otherwise).
I've never used spaces myself so can't be of much help here (you could also try to figure out if space.hash()
always produces the same hash, and map it to N).
If I remember correctly the array should be ordered. Also the hash value for each space is their identifier.
Thanks! Space.all()
did the trick. I just needed to filter for normal spaces and the same screen.
Solution:
# ~/.phoenix.js
#!/usr/bin/env coffee -p
for i in [1..9]
((i)-> # needed for scoping. otherwise i will always be n+1 when the callback is called.
Key.on i, ['ctrl', 'alt'], -> move_active_window_to_space i
)(i)
spaces_of_current_screen = ->
_.filter(Space.all(), (space)->
_.includes(space.screens(), active_window().screen()) && space.isNormal()
)
space = (i)->
spaces_of_current_screen()[i - 1]
active_window = ->
Window.focused()
move_active_window_to_space = (i)->
Space.active().removeWindows [active_window()]
space(i).addWindows [active_window()]
@fiedl Nice! There’s also Screen#spaces()
.
@fiedl This is the best i3wm
alternative setup I could find for Mac OS! I was very frustrated that yabai
requires disabling SIP in order to move windows around. Finally, I've discovered a solution to throw windows to numbered spaces. Thanks so much. For anyone who'd rather use regular JS:
const ALT_SHIFT = ["alt", "shift"];
const ALT_CTRL = ["alt", "control"];
let _i;
/**
* Move to Numbered Space
*
* Adapted from fiedl's coffeescript phoenix.js dotfile:
* https://github.com/fiedl/dotfiles/
*
* Add Option/Alt Key + [1-9] to System Preferences > Keyboard > Shortcuts
* to quickly switch between numbered spaces.
* Use "WhichSpace" to see active space in menu bar--- looking for better solution.
*
*/
const _fn = (i) => {
Key.on(i, ALT_SHIFT, () => {
return move_active_window_to_space(i);
});
return Key.on(i, ALT_CTRL, () => {
return move_active_window_and_go_to_space(i);
});
};
const assignment = (() => {
for (let i=_i=1;_i<=9;i=++_i) {
_fn(i);
}
})();
const spaces_of_current_screen = () => {
return _.filter(Space.all(), function(space) {
return _.includes(space.screens(), active_window().screen()) && space.isNormal();
});
};
const space = (i) => {
return spaces_of_current_screen()[i - 1];
};
const active_window = () => {
return Window.focused();
};
const move_active_window_to_space = (i) => {
Space.active().removeWindows([active_window()]);
return space(i).moveWindows([active_window()]);
};
const move_active_window_and_go_to_space = (i) => {
move_active_window_to_space(i);
return active_window().focus();
};
@kasper Could you elaborate on Screen#spaces()
?
I was very frustrated that yabai requires disabling SIP in order to move windows around
yabai
does not need SIP disabled to move windows around. It needs SIP disabled to modify spaces
.
Hi @koekeishiya, thanks for seeking some clarity. I'm not super familiar with the terminology, and I think I used the term window when I meant space. With yabai running and SIP enabled, it does not have the ability to send windows to space n, AFAIK. I've found that this phoenix configuration is a good solution to use (even in tandem with the SIP-enabled yabai) in order to mimic i3wm-like window movements across numbered spaces.
With yabai running and SIP enabled, it does not have the ability to send windows to space n
You can do this with SIP enabled: yabai -m window --space n
. It uses the following API which is not protected in any way SLSMoveWindowsToManagedSpace
.
Well hot Daniel, that works. Thanks for pointing that out @koekeishiya. Still glad that phoenix can be scripted to do this too. Sorry for bloating this issue.
@l00sed Oh, right, I was just referring that you could use Screen#spaces()
to replace spaces_of_current_screen
.
Oh, awesome. Thanks for that.
macOS does provide keyboard shortcuts for switching to space n, for example ctrl+3 to switch to space 3.
I'm trying to implement a shortcut with phoenix in order to send the active window (
Window.focused()
) to then
th space in the above sense.Is there a way to get an array of spaces in the order that macOS uses for ctrl+n.