Last active
January 2, 2025 15:23
-
-
Save withakay/25e7fd0a27545778c9ec8e098fea3e8f to your computer and use it in GitHub Desktop.
Cycle layouts in Rectangle Pro
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env zsh | |
# Rectangle Pro Layout Cycler | |
# This script cycles through predefined Rectangle Pro window manager layouts. | |
# It supports multiple layout groups and maintains state between invocations. | |
# It uses `open -g "rectangle-pro://execute-layout?name=<<layout>>` to activate Layouts. | |
# | |
# Edit the layout_groups variable to suit your needs and then use a 3rd party tool map | |
# a key command to invoke the script. (I am using Alfred, but there are loads of options | |
# like skhd, hammerspoon, Keyboard Maestro and more I am sure | |
# | |
# Usage: | |
# ./cycle-layouts.sh [--group <group_name>] | |
# | |
# Features: | |
# - Cycles through layouts within a specified group | |
# - Maintains state in XDG-compatible location | |
# - Creates debug-friendly state files | |
# | |
# State file format: | |
# line 1: current_index | |
# line 2: layout=<current_layout_name> | |
# line 3: group=<current_group_name> | |
# Enable associative array support | |
typeset -A layout_groups | |
# Define layout groups with arrays. | |
# Group names are arbitrary and can be anything | |
# the array elements are the layout names, these must match a layout name in Rectangle Pro. | |
layout_groups=(group1 "group1-layout1 group1-layout2 group1-layout3") | |
# if you want more groups just add them: | |
# layout_groups=(group2 "group2-layout1 group2-layout2") | |
echo $layout_groups | |
# Default group | |
group="group1" | |
# Parse arguments | |
while [[ $# -gt 0 ]]; do | |
case $1 in | |
--group) | |
if [[ -n $layout_groups[$2] ]]; then | |
group="$2" | |
else | |
echo "Error: Group '$2' not found" | |
exit 1 | |
fi | |
shift 2 | |
;; | |
*) | |
echo "Unknown argument: $1" | |
echo "Usage: $0 [--group <group_name>]" | |
exit 1 | |
;; | |
esac | |
done | |
# split groups on space | |
layouts=(${=layout_groups[$group]}) | |
# XDG compatible state file location | |
STATE_DIR="${XDG_STATE_HOME:-$HOME/.local/state}/rectangle-layouts" | |
STATE_FILE="$STATE_DIR/current_layout_${group}" | |
# Create state directory if it doesn't exist | |
mkdir -p "$STATE_DIR" | |
# default index to zero | |
current_index=0 | |
# attempt to read the state file | |
if [[ -f "$STATE_FILE" ]]; then | |
# index should be on the first line, all other lines are for debug purposes | |
current_index=$(head -n 1 "$STATE_FILE") | |
fi | |
next_index=$(( (current_index + 1) % $#layouts )) | |
# echo $next_index | |
next_layout=$layouts[(next_index + 1)] # zsh arrays are 1-based | |
# Activate the next layout | |
open -g "rectangle-pro://execute-layout?name=$next_layout" | |
# Save the state with all information | |
cat > "$STATE_FILE" << EOF | |
$next_index | |
layout=$next_layout | |
group=$group | |
EOF | |
echo "Activated $next_layout" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
In Rectangle Pro configure some App Layouts to cycle through