How to close all windows of a given program at once

How to do things, solutions, recipes, tutorials
Message
Author
User avatar
MochiMoppel
Posts: 2084
Joined: Wed 26 Jan 2011, 09:06
Location: Japan

How to close all windows of a given program at once

#1 Post by MochiMoppel »

Sorry for a trivial suggestion. This closes all ROX windows on all desktops at once:

Code: Select all

rox -D /
My problem is that I keep opening ROX-Filer windows and never bother to close them. Pretending that I'll reuse them later they get buried and abandoned, and when I run out of desktop space, I move on to the next clean desktop to open new windows until this desktop is messed up....well, I guess I'm not organized. Above code helps me to get back to a clean slate to start over again.

EDIT 1
On special request and in all its ugliness, the code to close all ROX windows on the current desktop at once:

Code: Select all

D=$(xprop -root _NET_CURRENT_DESKTOP);D=${D##* };R=$(echo -n "`wmctrl -lx`" | awk -F"[ .]+" '{if ($2=='$D' && $3=="ROX-Filer") print $1}');for i in $R;do wmctrl -ic $i;done
Requires wmctrl (included in every decent repository).

EDIT 2
This closes <any program> windows on all desktops at once.
The active window determines the windows to close. E.g if the currently active window belongs to the program mtPaint, this window and all other mtPaint windows will be closed.

Code: Select all

#!/bin/sh
ACTIVE_WID="$(xprop -root | sed -n 's/^_NET_ACTIVE_WINDOW(WINDOW): window id # //p')"
ACTIVE_WNAME="$(xprop -id $ACTIVE_WID | sed -n 's/^.*WM_CLASS(STRING) = "//p' | sed 's/".*$//')"
Xdialog --yesno "Close all $ACTIVE_WNAME windows on ALL desktops?" 0 0 || exit
R=$(echo -n "`wmctrl -lx`" | awk -F"[ .]+" '{if ($3=="'"$ACTIVE_WNAME"'") print $1}')
for i in $R;do wmctrl -ic $i;done
Last EDIT
Edit 2 needed some code cleaning, so I made a version that might even be useful. This will let the user close all windows of a given application, either those on the a) current desktop b) all desktops excluding the current or c) all desktops:

Code: Select all

#!/bin/bash
AI=$(xprop -root _NET_ACTIVE_WINDOW)     ;AI=${AI##* }                      # Active Window ID
AN=$(xprop -id $AI WM_CLASS);AN=${AN#*\"};AN=${AN%%\"*}                     # Active Window Name
AD=$(xprop -root _NET_CURRENT_DESKTOP)   ;AD=${AD##* }                      # Active Desktop No. (base 0)
RC=$(wmctrl -lx|awk -F"[ .]+" '{if ($2=='$AD' && $3=="'"$AN"'") print $1}') # Window IDs CURRENT
RO=$(wmctrl -lx|awk -F"[ .]+" '{if ($2!='$AD' && $3=="'"$AN"'") print $1}') # Window IDs OTHER
RA=$(wmctrl -lx|awk -F"[ .]+" '{if (             $3=="'"$AN"'") print $1}') # Window IDs ALL
CC=${RC//[^x]};CC=${#CC}                                                    # Count CURRENT
CO=${RO//[^x]};CO=${#CO}                                                    # Count OTHER
CA=${RA//[^x]};CA=${#CA}                                                    # Count ALL
gxmessage -c -fn 'bold 11' -bg '#06F' -fg '#FFF' \
-buttons "CURRENT ($CC)":101,"OTHER ($CO)":102,"ALL ($CA)":103,Cancel:1 \
$'\n\tClose '"\"$AN\" windows on "$'\n\tCURRENT, OTHER or ALL desktops'
case $? in 1) exit ;; 101) R=$RC;; 102) R=$RO;; 103) R=$RA;; esac
for   i in $R;do wmctrl -ic $i;done
Attachments
Screenshot.png
(9.39 KiB) Downloaded 461 times
Last edited by MochiMoppel on Thu 17 Mar 2016, 06:43, edited 5 times in total.

User avatar
Puppus Dogfellow
Posts: 1667
Joined: Tue 08 Jan 2013, 01:39
Location: nyc

#2 Post by Puppus Dogfellow »

good to know. thanks, Mochi.

...what's the command to close only those on the current desktop? (i tried lowercase d on a hunch but no dice.)

User avatar
MochiMoppel
Posts: 2084
Joined: Wed 26 Jan 2011, 09:06
Location: Japan

#3 Post by MochiMoppel »

Puppus Dogfellow wrote:...what's the command to close only those on the current desktop?
Not possible. Type rox -h for a list of all command line options.

User avatar
Puppus Dogfellow
Posts: 1667
Joined: Tue 08 Jan 2013, 01:39
Location: nyc

#4 Post by Puppus Dogfellow »

MochiMoppel wrote:
Puppus Dogfellow wrote:...what's the command to close only those on the current desktop?
Not possible. Type rox -h for a list of all command line options.
thanks for the tip--easier to do panels with that info.

User avatar
Puppus Dogfellow
Posts: 1667
Joined: Tue 08 Jan 2013, 01:39
Location: nyc

#5 Post by Puppus Dogfellow »

MochiMoppel wrote:
Puppus Dogfellow wrote:...what's the command to close only those on the current desktop?
Not possible. Type rox -h for a list of all command line options.
if that wmctrl program can call/identify each window of a program running on each desktop, and i think this is a fraction of what it can do, it can then close/minimize/maximize those windows. probably tile or cascade them as well. i see you know how to make the gtk stuff--wouldn't surprise me to find you've disproven your assertion within two weeks. :wink:

you just write the script and list it as a dependency is all (although much of this is contingent upon the idea that i did not misread what wmctrl is all about).


of course, that's a bit more involved than discovering that command...
:(
:)

(hope that doesn't make you feel put upon. many thanks for the many improvements you've made to my favorite OS.)

User avatar
MochiMoppel
Posts: 2084
Joined: Wed 26 Jan 2011, 09:06
Location: Japan

#6 Post by MochiMoppel »

Puppus Dogfellow wrote:wouldn't surprise me to find you've disproven your assertion within two weeks. :wink:
Would surprise me to be proven wrong that early. :) I stand by my claim that it's impossible with a (rox) or any other single command. But of course if you bring in heavy artillery like wmctrl you can shoot almost any window. See top post.

User avatar
Puppus Dogfellow
Posts: 1667
Joined: Tue 08 Jan 2013, 01:39
Location: nyc

#7 Post by Puppus Dogfellow »

MochiMoppel wrote:
Puppus Dogfellow wrote:wouldn't surprise me to find you've disproven your assertion within two weeks. :wink:
Would surprise me to be proven wrong that early. :) I stand by my claim that it's impossible with a (rox) or any other single command. But of course if you bring in heavy artillery like wmctrl you can shoot almost any window. See top post.
very nice.
:lol:

...

Mochi, would swapping out "ROX-Filer" in

Code: Select all

D=$(echo "`wmctrl -d`" | grep "\*" | grep -o "^[0-9]*");R=$(echo -n "`wmctrl -lx`" | awk -F"[ .]+" '{if ($2=='$D' && $3=="ROX-Filer") print $1}');for i in $R;do wmctrl -ic $i;done
for "[application of choice]" make this code do the same for all correctly named programs?

anyway, tested and confirmed to work well with Rox.

Thanks, Mochi.

edit: to test the theory, i swapped out "Rox-Filer" for "mtpaint" and clicking the script closed all the open mtpaint windows on the active desktop.

this could prove helpful for me, especially with regards to xpad; those things really tend to pile up.

:D

update: i have now dragged symlinks to four versions of the script (swapping out "rox-filer" for leafpad, xpad, sakura, and mtpaint) which, when added to the original cr (close all rox windows on active desktop), gives me five new convenient terminal commands: cl, cx, cr, cm, cs (which closes itself).

great stuff.

....

here's wmctrl-1.07-6_i386.pet for anyone who wants it--i think it was originally put together by jemimah.

edit: wmctrl-1.07-6_i386.pet was made by lithpr
Last edited by Puppus Dogfellow on Thu 26 Jun 2014, 07:04, edited 2 times in total.

User avatar
MochiMoppel
Posts: 2084
Joined: Wed 26 Jan 2011, 09:06
Location: Japan

#8 Post by MochiMoppel »

Puppus Dogfellow wrote:would swapping out "ROX-Filer" in ...
for "[application of choice]" make this code do the same for all correctly named programs?
You figured it out already, but for the record: You can find the correct name when you run xprop. Click on a window of the program of your interest and check the output in the terminal. E.g. when you click on a mtPaint window, check for the line that reads WM_CLASS(STRING) = "mtpaint", "Mtpaint". The first of the 2 names is the one used by the script.
cs (which closes itself).
??? Sakura opens only one instance, why would you need a script to close it?

User avatar
Puppus Dogfellow
Posts: 1667
Joined: Tue 08 Jan 2013, 01:39
Location: nyc

#9 Post by Puppus Dogfellow »

MochiMoppel wrote:
Puppus Dogfellow wrote:would swapping out "ROX-Filer" in ...
for "[application of choice]" make this code do the same for all correctly named programs?
You figured it out already, but for the record: You can find the correct name when you run xprop. Click on a window of the program of your interest and check the output in the terminal. E.g. when you click on a mtPaint window, check for the line that reads WM_CLASS(STRING) = "mtpaint", "Mtpaint". The first of the 2 names is the one used by the script.
cs (which closes itself).
??? Sakura opens only one instance, why would you need a script to close it?
sakura's a button on my taskbar on one set up and a menu item on another--the terminals pile up at times as each click's a window and not a tab.
i think i was over using it (relative to rarely using the terminal at all) to test it out and play around with the customization features...and launch my panels and shortcuts... anyway, xprop seems like it will come in as handy as xev, which i just recently came across, so--thanks.


i seem to have run into some sort of snag on my precise 5.5. install--none of those little shortcuts work at the moment:

Code: Select all

# cs
Cannot get current desktop properties. (_NET_CURRENT_DESKTOP or _WIN_WORKSPACE property)
awk: {if ($2== && $3=="sakura") print $1}
awk:           ^ syntax error
awk: {if ($2== && $3=="sakura") print $1}
awk:                          ^ syntax error
# cr
Cannot get current desktop properties. (_NET_CURRENT_DESKTOP or _WIN_WORKSPACE property)
awk: {if ($2== && $3=="ROX-Filer") print $1}
awk:           ^ syntax error
awk: {if ($2== && $3=="ROX-Filer") print $1}
awk:                             ^ syntax error
# 


thankfully, the other one (rox -D /) still does--i've come to rely on it quite a bit...

User avatar
MochiMoppel
Posts: 2084
Joined: Wed 26 Jan 2011, 09:06
Location: Japan

#10 Post by MochiMoppel »

Puppus Dogfellow wrote:i seem to have run into some sort of snag on my precise 5.5. install-
Did it work before? What awk version (command awk --version )? I tested with 3.1.8

User avatar
Puppus Dogfellow
Posts: 1667
Joined: Tue 08 Jan 2013, 01:39
Location: nyc

#11 Post by Puppus Dogfellow »

MochiMoppel wrote:
Puppus Dogfellow wrote:i seem to have run into some sort of snag on my precise 5.5. install-
Did it work before? What awk version (command awk --version )? I tested with 3.1.8

Code: Select all

# awk --version
GNU Awk 3.1.8
worked in precise 5.5 up until an x-server restart. works fine on precise 561 and 571.

...

any other commands that can be substituted in and out of the script? minimize, maximize, restore...what else can you do with all windows of a type?...

it'd be nice to get that working again, but this installation's picked up a problem or two over the months...the problem could be related to whatever caused me to have to restart the xserver in the first place--rox hangs on me if i click the Open With menu. the built in open withs are fine and dragging to applications works fine, and none of the default or set run actions have been affected, but at some point that stopped working correctly....

other than that, i have no idea why it stopped working or what else out of the ordinary took place around the time it quit.

:?

User avatar
MochiMoppel
Posts: 2084
Joined: Wed 26 Jan 2011, 09:06
Location: Japan

#12 Post by MochiMoppel »

Puppus Dogfellow wrote:any other commands that can be substituted in and out of the script?
Type wmctrl -h

User avatar
MochiMoppel
Posts: 2084
Joined: Wed 26 Jan 2011, 09:06
Location: Japan

#13 Post by MochiMoppel »

I added 2 options to the original post, which should provide sufficient flexibility. The scripts determine the window ID of the currently active window, then based on the window ID retrieve the proper program name and lastly use wmctrl to build a list all windows belonging to this program, the basis for the following "close" action.

User avatar
Puppus Dogfellow
Posts: 1667
Joined: Tue 08 Jan 2013, 01:39
Location: nyc

#14 Post by Puppus Dogfellow »

MochiMoppel wrote:I added 2 options to the original post, which should provide sufficient flexibility. The scripts determine the window ID of the currently active window, then based on the window ID retrieve the proper program name and lastly use wmctrl to build a list all windows belonging to this program, the basis for the following "close" action.
cool. gave each version a shortcut key; both are working very well, even on the installation that was having problems with the other version of the current desktop script. thanks for this, MochiMoppel.

...also work well with panel and jwm menu...and as desktop icons...

how do you activate them from the terminal? they close their filer or terminal window when activated with those methods.

User avatar
MochiMoppel
Posts: 2084
Joined: Wed 26 Jan 2011, 09:06
Location: Japan

#15 Post by MochiMoppel »

Puppus Dogfellow wrote:how do you activate them from the terminal?
Why do you want to do that? That's not the way the script is meant to be used - unless you want the terminal windows to commit suicide.

User avatar
Puppus Dogfellow
Posts: 1667
Joined: Tue 08 Jan 2013, 01:39
Location: nyc

#16 Post by Puppus Dogfellow »

MochiMoppel wrote:
Puppus Dogfellow wrote:how do you activate them from the terminal?
Why do you want to do that? That's not the way the script is meant to be used - unless you want the terminal windows to commit suicide.
i was just curious and in a way hoping it would answer another question: i tried to get wmctrl -l (and -h, with the same comical results) to launch with a click and it just kind of popped in and out in a millisecond--i couldn't get it or the terminal running it to stick around. i was hoping the code to get the terminal to take itself out of the equation (or something along those lines) would reveal itself.

anyway, that's a really cool bit of code you came up with. i suppose i could combine them with that semicolon trick you showed me--get a choice of which...

you've been churning out quite a bit of excellence lately, Mochi. at least four of the programs i use repeatedly every day are recent scripts of yours.

User avatar
MochiMoppel
Posts: 2084
Joined: Wed 26 Jan 2011, 09:06
Location: Japan

#17 Post by MochiMoppel »

Well, to tell the truth I never thought that the code would be useful. Usable - yes, but useful? I did it out of curiosity and to complete the collection of killer codes. My reasoning: If someone needs a script to mass slaughter windows, he may be better off with a tool to prevent the accumulation of unneeded windows.

I've seen the screenshot of your desktop and I have a couple of questions:
- You use 3 (?) virtual desktops. Did you ever consider to increase the number? This could reduce the number of windows in each desktop.
- How many windows in total do you normally keep open?
- Do you normally minimize them or did you minimize them all only for the screenshot?
- How do you find a specific window? If you decide to kill all, you would need to know how many there are and where they are.

User avatar
Puppus Dogfellow
Posts: 1667
Joined: Tue 08 Jan 2013, 01:39
Location: nyc

#18 Post by Puppus Dogfellow »

MochiMoppel wrote:Well, to tell the truth I never thought that the code would be useful. Usable - yes, but useful? I did it out of curiosity and to complete the collection of killer codes. My reasoning: If someone needs a script to mass slaughter windows, he may be better off with a tool to prevent the accumulation of unneeded windows.

I've seen the screenshot of your desktop and I have a couple of questions:
- You use 3 (?) virtual desktops. Did you ever consider to increase the number? This could reduce the number of windows in each desktop.
- How many windows in total do you normally keep open?
- Do you normally minimize them or did you minimize them all only for the screenshot?
- How do you find a specific window? If you decide to kill all, you would need to know how many there are and where they are.
most of the iconified windows in that screenshot are xpad windows--i can normally identify them by where i placed them, the title/first line snippet that sticks out, etc.

the number of desktops i use varies from 3 to 10. the mass of purple (xpad) and blue (libreoffice) plus the geany are usually stuck to all desktops. unsticking an xpad window and sending it elsewhere allows me to close all the others if i so choose--all my machines autostart with at least four xpad windows...

i just found something on the xdotool webpage (which i can't now access due to impatience and its incredibly slow rate of crawl) which would allow you to resize and grab and position types of windows...there's a template for sets of four, so i can see teds or leafpads or (etc) piling up as well... right now, until i add jwm configuration boxes and photosnap and paint programs for uploading screenshot purposes, i can pretty much identify all the minimized windows by reading what's visible. grabbing them and being able to move/resize/activate them from a list would be pretty handy, though.


as to what's minimized, maximized, how they're layered--it depends on what i'm doing/what i'm looking at.

User avatar
MochiMoppel
Posts: 2084
Joined: Wed 26 Jan 2011, 09:06
Location: Japan

#19 Post by MochiMoppel »

Puppus Dogfellow wrote:i can pretty much identify all the minimized windows by reading what's visible. grabbing them and being able to move/resize/activate them from a list would be pretty handy, though.
You must have damn good eyesight :lol: Window captions are barely visible in your tray. Anyway, your problems are different from mine. Since I don't use stickies, I never knew on which desktop to find applications which I know I opened. An eternal hunt for the browser window. I solved this problem now with a menu that shows all windows of all desktops in a flat list. It allows to close single or multiple windows (ahh, you see we are still on topic), but moving/resizing...no, and I have no idea how this should work. If you think you could use it, drop me a line by PM and I can send you a copy. It's functional and I use it daily, but it still needs some polishing. You would be the ideal person to put it through a stress test :lol:

The screenshot shows 15 windows, sorted by desktops, with desktop 5 being the current one. No distinction is made between minimized and normal windows.
Image

User avatar
Puppus Dogfellow
Posts: 1667
Joined: Tue 08 Jan 2013, 01:39
Location: nyc

#20 Post by Puppus Dogfellow »

MochiMoppel wrote:
Puppus Dogfellow wrote:i can pretty much identify all the minimized windows by reading what's visible. grabbing them and being able to move/resize/activate them from a list would be pretty handy, though.
You must have damn good eyesight :lol: Window captions are barely visible in your tray. Anyway, your problems are different from mine. Since I don't use stickies, I never knew on which desktop to find applications which I know I opened. An eternal hunt for the browser window. I solved this problem now with a menu that shows all windows of all desktops in a flat list. It allows to close single or multiple windows (ahh, you see we are still on topic), but moving/resizing...no, and I have no idea how this should work. If you think you could use it, drop me a line by PM and I can send you a copy. It's functional and I use it daily, but it still needs some polishing. You would be the ideal person to put it through a stress test :lol:

The screenshot shows 15 windows, sorted by desktops, with desktop 5 being the current one. No distinction is made between minimized and normal windows.
Image
that looks great, Mochi. (link is invisible by design or is my choice of gtk theme coming back to haunt me again?...). if moving or resizing is important, the user can just do it with assigned keys through jwm customizations or the menu bar right click pop up. having a list of open windows would allow me to not have to scroll through the stuff...much less mouse work in the end, so sure, expect that pm. :D

speaking of stress tests...broke something and had to reboot my machine messing around with xdotool---making quadrants of five leafpads can only be pushed so far so fast it seems...thing appears at least as powerful as wmctrl...seems a bit less cryptic, too.

Are there other hidden away rox commands like the close all windows on all desktops one? is there one for simply cycling back through the directories you just visited?

and my eyesight's eroding. once upon a time it was pretty good. (feel like i ruined your eyesight/screenshot joke. how did you do that? color=transparent?)
Last edited by Puppus Dogfellow on Thu 19 Jun 2014, 01:18, edited 1 time in total.

Post Reply