This bash script is part of a macOS window management system that leverages Yabai (a tiling window manager for macOS) to programmatically move windows between spaces. Specifically, this script moves the currently focused window to the next space to the right, automatically creating a new space if the window is already on the rightmost space.
The script automates window organization by providing a keyboard-shortcuttable way to shift windows rightward through macOS spaces (virtual desktops), enhancing productivity for users who work with multiple spaces.
- Smart Space Detection: Automatically detects if the current window is on the rightmost space
- Dynamic Space Creation: Creates a new space when moving from the rightmost position
- Window Focus Preservation: Ensures the moved window retains focus after the move
- Comprehensive Logging: Detailed logging for debugging and monitoring
- Error Handling: Graceful handling of edge cases and errors
- Yabai: macOS tiling window manager (
/opt/homebrew/bin/yabai
) - jq: Command-line JSON processor (
/opt/homebrew/bin/jq
) - log_helper.sh: Custom logging utility script (must be in same directory)
- Bash: Shell script environment
- Initial State Capture: Records the current window, space, and display configuration
- Window Identification: Gets the ID of the currently focused window
- Space Detection: Determines the next available space to the right
- Movement Logic:
- If a space exists to the right: moves window to that space
- If no space exists (rightmost position): creates new space, then moves
- Focus Management: Switches to the target space and refocuses the moved window
- State Verification: Logs the final state for verification
┌─────────────────┐
│ Get Window ID │
└────────┬────────┘
│
▼
┌─────────────────┐
│ Query Spaces │
└────────┬────────┘
│
▼
┌────────┐
│ Next │
│ Space? │
└───┬────┘
│
┌────┴────┐
│ │
▼ ▼
┌──────┐ ┌──────────┐
│ Move │ │ Create │
│ to │ │ Space │
│ Next │ │ + │
└──────┘ │ Move │
└──────────┘
# Execute directly
~/.config/scripts/move_window_right.sh
# Or make executable and run
chmod +x move_window_right.sh
./move_window_right.sh
This script is designed to be triggered via keyboard shortcuts using tools like:
- Karabiner-Elements: Complex keyboard modifications
- skhd: Simple hotkey daemon for macOS
- System Preferences: Native macOS keyboard shortcuts
Example Karabiner configuration snippet:
{
"description": "Move window right",
"manipulators": [{
"type": "basic",
"from": {"key_code": "right_arrow", "modifiers": {"mandatory": ["shift", "command"]}},
"to": [{"shell_command": "~/.config/scripts/move_window_right.sh"}]
}]
}
log_state()
: Captures and logs current window/space/display statefocus_window()
: Refocuses a specific window by ID with verification- Main Logic: Sequential execution of window movement operations
The script uses structured logging with the following levels:
- START/END: Script lifecycle events
- INFO: Informational messages
- ACTION: Yabai commands being executed
- DEBUG: Detailed debugging information
- ERROR: Error conditions
Logs are written via log_helper.sh
to a centralized location (typically ~/.config/logs/
).
- Validates window ID retrieval
- Handles edge cases for rightmost space
- Uses explicit string comparison for jq fallback values
- Includes sleep delays for Yabai operation completion
Solution: The script includes a 0.1 second delay and explicit refocus command to handle Yabai's asynchronous operations.
Solution: Ensure a window is focused before running the script. Some system windows may not be queryable.
Solution: Check Yabai permissions and ensure SIP is configured correctly for space management.
This script is part of a larger window management system. Related scripts include:
move_window_left.sh
: Move window to previous spacespace_right.sh
: Switch focus to next space (without moving window)remove_empty_spaces.sh
: Clean up unused spaceslog_helper.sh
: Centralized logging utility
- The script uses absolute paths for binaries to ensure consistent execution
- String comparison for jq's empty fallback uses literal
'""'
to avoid bash interpretation issues - Window IDs are preserved throughout the operation for reliable refocusing
- State logging before and after operations aids in debugging space management issues
Part of personal dotfiles configuration - adapt as needed for your setup.