users/ryan/modules/simple-bar/simple-bar-source/lib/scripts/init-yabai.sh

8c57a511a35702ddf354f3cb52e04228a36bee82 · 5.4 KB · 102 lines raw

1 # Assign input arguments to variables
2 yabai_path=$1
3 display_skhd_mode=$2
4 disable_signals=$3
5 enable_window_title_changed_signal=$4
6
7 # Check if yabai is running
8 pgrep -x yabai > /dev/null
9
10 # If yabai is not running, output an error message and exit
11 if [ $? -eq 1 ]; then
12 echo "yabaiError"
13 exit 0
14 fi
15
16 # Get the System Integrity Protection (SIP) status
17 SIP=$(csrutil status)
18 # Get the current window shadow configuration from yabai
19 shadow_enabled=$($yabai_path -m config window_shadow)
20
21 # Query yabai for spaces, windows, and displays information
22 spaces=$($yabai_path -m query --spaces)
23 windows=$($yabai_path -m query --windows)
24 displays=$($yabai_path -m query --displays)
25
26 # Retry querying spaces if the initial query was empty
27 if [ -z "$spaces" ]; then
28 spaces=$($yabai_path -m query --spaces)
29 fi
30
31 # Retry querying windows if the initial query was empty and remove backslashes
32 if [ -z "$windows" ]; then
33 windows=$($yabai_path -m query --windows | sed 's/\\.//g;')
34 fi
35
36 # Retry querying displays if the initial query was empty
37 if [ -z "$displays" ]; then
38 displays=$($yabai_path -m query --displays)
39 fi
40
41 # Add or remove signals based on the disable_signals flag
42 if [ $disable_signals = "false" ]; then
43 # Add signals to refresh the simple-bar widget on various yabai events
44 $yabai_path -m signal --add event=window_focused action="osascript -e 'tell application id \"tracesOf.Uebersicht\" to refresh widget id \"simple-bar-index-jsx\"'" label="Refresh simple-bar when focused application changes"
45 $yabai_path -m signal --add event=window_minimized action="osascript -e 'tell application id \"tracesOf.Uebersicht\" to refresh widget id \"simple-bar-index-jsx\"'" label="Refresh simple-bar when a window is minimized"
46 $yabai_path -m signal --add event=window_resized action="osascript -e 'tell application id \"tracesOf.Uebersicht\" to refresh widget id \"simple-bar-index-jsx\"'" label="Refresh simple-bar when a window is resized"
47 $yabai_path -m signal --add event=window_destroyed action="osascript -e 'tell application id \"tracesOf.Uebersicht\" to refresh widget id \"simple-bar-index-jsx\"'" label="Refresh simple-bar when an application window is closed"
48 $yabai_path -m signal --add event=space_changed action="osascript -e 'tell application id \"tracesOf.Uebersicht\" to refresh widget id \"simple-bar-index-jsx\"'" label="Refresh simple-bar on space change"
49 $yabai_path -m signal --add event=display_changed action="osascript -e 'tell application id \"tracesOf.Uebersicht\" to refresh widget id \"simple-bar-index-jsx\"'" label="Refresh simple-bar on display focus change"
50
51 # Add signal for window title change if enabled
52 if [ $enable_window_title_changed_signal = "true" ]; then
53 $yabai_path -m signal --add event=window_title_changed action="osascript -e 'tell application id \"tracesOf.Uebersicht\" to refresh widget id \"simple-bar-index-jsx\"'" label="Refresh simple-bar when current window title changes"
54 fi
55
56 # Add signals for space creation and destruction if yabai version is 6 or higher
57 yabai_major_version=$($yabai_path -v | awk -F '.' '{print $1}' | sed 's/yabai-v//')
58 if [ $yabai_major_version -ge 6 ]; then
59 $yabai_path -m signal --add event=space_destroyed action="osascript -e 'tell application id \"tracesOf.Uebersicht\" to refresh widget id \"simple-bar-index-jsx\"'" label="Refresh simple-bar on space removal"
60 $yabai_path -m signal --add event=space_created action="osascript -e 'tell application id \"tracesOf.Uebersicht\" to refresh widget id \"simple-bar-index-jsx\"'" label="Refresh simple-bar on space creation"
61 fi
62 else
63 # Remove signals if disable_signals is true
64 $yabai_path -m signal --remove label="Refresh simple-bar when focused application changes" >/dev/null 2>&1 || true
65 $yabai_path -m signal --remove label="Refresh simple-bar when a window is minimized" >/dev/null 2>&1 || true
66 $yabai_path -m signal --remove label="Refresh simple-bar when a window is resized" >/dev/null 2>&1 || true
67 $yabai_path -m signal --remove label="Refresh simple-bar when an application window is closed" >/dev/null 2>&1 || true
68 $yabai_path -m signal --remove label="Refresh simple-bar on space change" >/dev/null 2>&1 || true
69 $yabai_path -m signal --remove label="Refresh simple-bar on display focus change" >/dev/null 2>&1 || true
70 $yabai_path -m signal --remove label="Refresh simple-bar when current window title changes" >/dev/null 2>&1 || true
71 $yabai_path -m signal --remove label="Refresh simple-bar on space removal" >/dev/null 2>&1 || true
72 $yabai_path -m signal --remove label="Refresh simple-bar on space creation" >/dev/null 2>&1 || true
73 fi
74
75 # Determine skhd mode based on the display_skhd_mode flag and disable_signals flag
76 if [ $display_skhd_mode = "true" ]; then
77 SCRIPT_DIR=$(cd $(dirname "${BASH_SOURCE[0]}") && pwd)
78 if [ $disable_signals = "false" ]; then
79 skhd_mode=$(cat "$("${SCRIPT_DIR}"/yabai-set-mode.sh --query)")
80 else
81 skhd_mode=$(cat "$("${SCRIPT_DIR}"/yabai-set-mode-server.sh --query)")
82 fi
83 else
84 skhd_mode="{}"
85 fi
86
87 # Output the collected information as a JSON object
88 echo $(cat <<-EOF
89 {
90 "spaces": $spaces,
91 "windows": $windows,
92 "displays": $displays,
93 "SIP": "$SIP",
94 "shadow": "$shadow_enabled",
95 "skhdMode": $skhd_mode
96 }
97 EOF
98 ) | \
99 # Remove invisible U+200E Left-To-Right Mark character
100 sed "s/\xe2\x80\x8e//g" | \
101 # Remove newlines from output (handling Google Chrome JSON parse error caused by "search in page")
102 tr -d '\n'