[openbox] exit script

D.T. ohnonot-github at posteo.de
Sat May 21 11:16:39 EDT 2022


Answering myself:
The script includes quite a lot of extra code because I wanted to
execute it in its own terminal window. This is not required.

On Sat, 2022-05-21 at 14:33 +0000, D.T. wrote:
> On Wed, 2022-05-18 at 13:03 -0600, Dave Serls wrote:
> > I'm wondering if there is any approved method of executing a
> > command
> > or 
> > script when "exit" is the selected action from the main menu.
> > I'd like to scan (xlsclients -a) for running tasks and either
> > cleanup
> > or
> > abort the "exit".
> > 
> 
> Thanks you for the hint with xlsclients! I wonder why I never knew
> about it.
> 
> I have been using a script to gracefully close all windows before
> exiting or shutting down.
> It simply sends wmctrl -c to close all windows gracefully; if the app
> objects (like e.g. an editor with unsaved changes would) I get
> prompted.
> 
> It works well enough, but I will definitely take a look at
> xlsclients.
> 
> The menu.xml part:
> 
>     <item label="Exit"><action
> name="Execute"><command><![CDATA[alacritty --title 'Graceful Exit' -e
> ~/.config/scripts/close-all-windows 'openbox --
> exit']]></command></action></item>
>     <item label="Reboot"><action
> name="Execute"><command><![CDATA[alacritty --title 'Graceful Reboot'
> -e
> ~/.config/scripts/close-all-windows systemctl
> reboot]]></command></action></item>
>     <item label="Shutdown"><action
> name="Execute"><command><![CDATA[alacritty --title 'Graceful
> Shutdown'
> -e ~/.config/scripts/close-all-windows systemctl
> poweroff]]></command></action></item>
> 
> The script:
> 
> ##################################################################
> sep="############################"
> excludeapps=( tint2 conky xfce4-panel glava )
> 
> # simple dependency check
> for dep in grep xwininfo wmctrl; do
>         type -f $dep >/dev/null || exit 1
> done
> 
> echo "Gracefully closing all windows."
> 
> excludeapps_winid=()
> for (( i=0,j=0 ; i<${#excludeapps[@]} ; i++ ))
> do
>     while read excludeapps_winid[j]
>     do
>         if [[ "x${excludeapps_winid[j]}" != "x" ]]
>         then
>             excludeapps_winid[j]="${excludeapps_winid[j]%% *}"
>             j=$((j + 1))
>         fi
>     done <<< "$(xwininfo -root -tree -int | grep -i
> "(.*${excludeapps[i]}.*)")"
>     # dump all window info, with IDs in decimal, and grep for
> classes/names,
>     # in brackets like: ("urxvt" "URxvt")
> done
> 
> # Add this window to the array of excluded apps
> # WINDOWID uses the same decimal format, just append it to the array
> excludeapps_winid=( ${excludeapps_winid[@]} $WINDOWID )
> 
> echo "$sep
> List of classes/names to exclude: \"${excludeapps[@]}\" and self.
> $sep
> Window IDs to exclude: ${excludeapps_winid[@]}"
> 
> while read -r line
> do
>     for (( i=0 ; i<${#excludeapps_winid[@]} ; i++ ))
>     do
>         excludeapps_winid[i]="0x$(printf '%08x'
> ${excludeapps_winid[i]})"
>         if [[ "${line%% *}" == "${excludeapps_winid[i]}" ]]
>         then
>             line=''
>             break
>         fi
>     done
>     if [[ "x$line" != "x" ]]
>     then
>         echo -e "$sep\nClosing: ${line/$HOSTNAME/}"
>         # go to the desktop in question...
>         wmctrl -s "${line:10:3}"
>         # ...and close:
>         wmctrl -ic "${line%% *}"
>         # checking if it's really closed now:
>         if wmctrl -l | grep -E "^${line%% *}" >/dev/null 2>&1
>         then
>             # wait, presumably for user action:
>             echo -n "Waiting . "
>             while wmctrl -l | grep -E "^${line%% *}" >/dev/null 2>&1
>             do
>                 sleep 0.1; echo -n '.'
>             done
>             echo
>         fi
>     fi
> done <<< "$(wmctrl -l)"
> 
> wmctrl -ia "0x$(printf '%08x' $WINDOWID)" >/dev/null 2>&1
> 
> echo "$sep
> Press <Enter> for \"$@\" or anything else to continue."
> oldifs="$IFS"
> IFS=
> read -s -n 1 key
> # -s: do not echo input character. -n 1: read only 1 character
> if [ -z "$key" ]; then
>     IFS="$oldifs"
>     while :
>     do
>         $@
>         sleep 2
>     done
> else
>     IFS="$oldifs"
> fi
> ##################################################################
> 
> I cut out some irrelevant parts, I hope it works as-is.
> 
> 
> ___
> 
> Vaccines for everyone!
> Donate to the COVAX alliance:
> https://gogiveone.org/ (WHO)
> https://www.gavi.org/donate (Gavi)
> _______________________________________________
> openbox mailing list
> openbox at icculus.org
> http://icculus.org/mailman/listinfo/openbox

___

Vaccines for everyone!
Donate to the COVAX alliance:
https://gogiveone.org/ (WHO)
https://www.gavi.org/donate (Gavi)
-------------- next part --------------
A non-text attachment was scrubbed...
Name: signature.asc
Type: application/pgp-signature
Size: 659 bytes
Desc: This is a digitally signed message part
URL: <http://icculus.org/pipermail/openbox/attachments/20220521/4a51fa5f/attachment.sig>


More information about the openbox mailing list