Hi everyone, I would like to have my Ubersicht widgets be shown when the space I am on is empty, and hidden when there are one or more windows open. I would like to have window borders enabled but I don't like seeing my widgets in-between the gaps.
Here is what I tried to add as a rule to my .yabairc
yabai -m signal --add event=space_changed
index="$(yabai -m query --windows --space | jq '.[]') && \
if ["$[index"] =""]; then action="osascript -e 'tell application id "tracesOf.Uebersicht" to set hidden of widget id "Clock-Clock-index-coffee" to false'" && \
action="osascript -e 'tell application id "tracesOf.Uebersicht" to set hidden of widget id "Date-index-coffee" to false'" && \
action="osascript -e 'tell application id "tracesOf.Uebersicht" to set hidden of widget id "Time-index-coffee" to false'"; \
else action="osascript -e 'tell application id "tracesOf.Uebersicht" to set hidden of widget id "Clock-Clock-index-coffee" to true'" && \
action="osascript -e 'tell application id "tracesOf.Uebersicht" to set hidden of widget id "Date-index-coffee" to true'" && \
action="osascript -e 'tell application id "tracesOf.Uebersicht" to set hidden of widget id "Time-index-coffee" to true'"; fi
I then repeated this code for the application_launched/terminated, window_created/destroyed, and application_visible/hidden signals.
Here is the break down of what the code is trying to do:
yabai -m signal --add event=space_changed
This is just creating a rule that listens for the space_changed event
index="$(yabai -m query --windows --space | jq '.[]')
This _should_ be creating an index variable that stores a list of window objects in the current space
if ["$[index"] =""];
This should check if the index object is empty, i.e. there are no windows on the currently focused space.
then action="osascript -e 'tell application id "tracesOf.Uebersicht" to set hidden of widget id "Clock-Clock-index-coffee" to false'
This should set the widget of id "Clock-Clock-index-coffee" to be not hidden if the space is empty. Repeated for widgets "Date-index-coffee" and "Time-index-coffee".
else action="osascript -e 'tell application id "tracesOf.Uebersicht" to set hidden of widget id "Clock-Clock-index-coffee" to true'"
This should set the widget of id "Clock-Clock-index-coffee" to be hidden if the space is not empty. Repeated for widgets "Date-index-coffee" and "Time-index-coffee".
If I am doing anything wrong please let me know. Keep in mind this is the first time I'm actually messing around with shell scripting, so please take it easy on me. 馃槄
There's a very basic misunderstanding with shell scripts here. We've all started somewhere, don't worry. :-)
When you write this in your shell:
a b c "d e" f
Then the shell searches for an executable named a in its list of known executable paths (commonly stored in $PATH, separated with a colon), and sends 4 arguments to it: b, c, d e, and f.
The basic syntax for signals in yabai is like this:
yabai -m signal --add event=event_name action=shell_command
In order for yabai to receive the action=shell_command part as a single argument, it needs to be quoted, because otherwise the shell would split the argument at the wrong place. All yabai does with action=shell_command is to spawn a new shell and run the argument you've given it (except for the leading action=) as a new command.
Similarly, jq is a JSON processing tool, which comes with its own query language, which is very different from the language most shells use. osascript is an AppleScript processing tool, which allows you to execute AppleScript on the fly. So you're actually trying to mix three languages together here, and that needs to be done carefully.
(The following is untested, and assumes your basic AppleScript command is correct. To debug, look at the individual pieces.)
Step 1: Create an AppleScript that allows you to set your widgets visibility. Save the below file as hide-uebersicht-widget.scpt, and then run chmod +x hide-uebersicht-widget.scpt to make it executable.
#! /usr/bin/env osascript
on run argv
tell application id "tracesOf.Uebersicht"
set hidden of widget id (quoted form of (item 1 of argv)) to (item 2 of argv)
end tell
end run
Step 2: Create a script that tells you whether your desktop is empty. Save the below file as is-space-empty.sh, and then run chmod +x is-empty-space.sh to make it executable.
#! /usr/bin/env sh
{
yabai -m query --windows --space | jq -r 'length == 0'
} >/dev/null 2>&1
Step 3: Register the signals for yabai in your yabairc.
yabai -m signal --add event=window_created action='
if [ ./is-empty-space.sh ]; then
./hide-uebersicht-widget.scpt Clock-Clock-index-coffee true
fi'
yabai -m signal --add event=window_destroyed action='
if [ ! ./is-empty-space.sh ]; then
./hide-uebersicht-widget.scpt Clock-Clock-index-coffee false
fi'
# ...
Side note: When writing shell scripts, use shell check for linting them for correctness. You can learn an awful lot from shell check.
Just weighing in here, depending on which gaps you're seeing your widgets peeking through, you could just rearrange them so that they fall within where you typically see window gaps ( a lot of widgets support pretty straightforward config of their positioning )
Most helpful comment
There's a very basic misunderstanding with shell scripts here. We've all started somewhere, don't worry. :-)
When you write this in your shell:
Then the shell searches for an executable named
ain its list of known executable paths (commonly stored in$PATH, separated with a colon), and sends 4 arguments to it:b,c,d e, andf.The basic syntax for signals in yabai is like this:
In order for yabai to receive the
action=shell_commandpart as a single argument, it needs to be quoted, because otherwise the shell would split the argument at the wrong place. All yabai does withaction=shell_commandis to spawn a new shell and run the argument you've given it (except for the leadingaction=) as a new command.Similarly,
jqis a JSON processing tool, which comes with its own query language, which is very different from the language most shells use.osascriptis an AppleScript processing tool, which allows you to execute AppleScript on the fly. So you're actually trying to mix three languages together here, and that needs to be done carefully.Assembly
(The following is untested, and assumes your basic AppleScript command is correct. To debug, look at the individual pieces.)
Step 1: Create an AppleScript that allows you to set your widgets visibility. Save the below file as
hide-uebersicht-widget.scpt, and then runchmod +x hide-uebersicht-widget.scptto make it executable.Step 2: Create a script that tells you whether your desktop is empty. Save the below file as
is-space-empty.sh, and then runchmod +x is-empty-space.shto make it executable.Step 3: Register the signals for yabai in your yabairc.
Side note: When writing shell scripts, use shell check for linting them for correctness. You can learn an awful lot from shell check.