sway kill windows with [app_id="something"]

I am looking for the following: $mod+n -> open cli in floating client (ikhal in particular) -> $mod+n -> close cli in floating client.

I was able to do this for nmtui with the following script:

#!/bin/bash
if pkill -0 nmtui; then
    pkill nmtui
else
    swaymsg exec 'foot -a floating_shell --window-size-pixel=1200x900 nmtui'
fi

Now I want to do the same for ikhal or any basically anything. My idea is, to assign a specific app_id, i.e. swaymsg ‘exec footclient -a ikhal ikhal’. And on keypress my scripts checks whether any app_id="ikhal" windows exists. If so, it kills them. Otherwise it starts a new one. However, the only triggers for criteria seem to be for_window and assign, which won’t help in this scenario?

Is there some way to do this anyways? Is there some other way to achieve this behavior?

Can you help me?

Asked By: grbll

||

For the specific thing I tried to do the following thing is the solution:

swaymsg [app_id="something"] kill

Explenation: swaymsg itself can take criteria. However, we need to escape the brackets. By this we can run commands on specific containers in sway.

However, as I wanted to use this to toggle a calender (or other apps) I found an even better solution: the sway scratchpad. So I ended up using the following script:

#!/bin/bash
swaymsg [app_id=$1] scratchpad show || swaymsg [app_id=$1] move scratchpad
if [ $? = 2 ]; then
  exec footclient -a $1 $1
fi

If you call this very simple script with sh toggle.sh ikhal it tries to get the window with app_id "ikahl" from the scratchpad, if this fails, it tries to move this window to the scratchpad, if both fail, it tries to open the app "ikhal" in a new foot session and assigns the app_id "ikhal".

I use this with any of my tuis, such as neomutt, ikhal, or nmtui.

Answered By: grbll
Categories: Answers Tags:
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.