Page 1 of 1

shell, prevent to open multiple box

Posted: Thu 21 Sep 2017, 17:39
by fabrice_035
hey,

It's simple tips i know , but rarely applied :shock:

Try with yassm-select (version 2.7), add this at top

Code: Select all

#!/bin/bash
# (...)

present=`wmctrl -l | grep "YASSM v2.7"`
if [ "$present" != "" ] ; then
wmctrl -R "YASSM V2.7" 
exit
fi

# (...)

Hidden windows showing in front.

Purpose another way ?

Think about that ? :roll:
Regard

Posted: Thu 21 Sep 2017, 18:26
by Flash
Could you give us more of a description of what this does, what it is for?

Posted: Thu 21 Sep 2017, 19:03
by fabrice_035
In my example, it's for place a always running program at front and take focus.

Before that, same program running several times.

Posted: Thu 21 Sep 2017, 23:04
by Flash
If I understand you correctly, this will prevent more than one instance of the program from running?

Posted: Fri 22 Sep 2017, 00:12
by musher0
Hello Flash (and fabrice)

See the wmctrl man page at https://linux.die.net/man/1/wmctrl

Code: Select all

wmctrl -R [window]
brings that window in focus.

I never thought of using it, actually, but it can be quite useful for any
window hidden behind other windows.

If I can comment fabrice's code:

Code: Select all

#!/bin/bash
# (...) # Probably the (c), GPL3 notice, maybe some strings, etc., here.

present=`wmctrl -l | grep "YASSM v2.7"`
# We check with wmctrl -i if we have a window called YASSM.
if [ "$present" != "" ] ; then # If we do
wmctrl -R "YASSM V2.7"  # we bring it in front
exit # and we exit the script
fi
# (...) # Otherwise we execute what's following here.
This bit of code is actually a "one-instance only" check.

If I can suggest an improvement, fabrice could replace the < exit > with an
< else > and push back the < fi > to the end, after the rest of the code. If
the first condition is satisfied, the part after the else will be skipped, and
the script will exit normally. There is no need to force the exit.

Code: Select all

#!/bin/bash
# (...) # Probably the (c), GPL3 notice, maybe some strings, etc., here.

present=`wmctrl -l | grep "YASSM v2.7"`
# We check with wmctrl -i if we have a window called YASSM.
if [ "$present" != "" ] ; then # If we do
wmctrl -R "YASSM V2.7"  # we bring it in front
else # <<=- Change
(...) # The rest of the code
fi
IHTH

Re: shell, prevent to open multiple box

Posted: Fri 22 Sep 2017, 03:48
by MochiMoppel
fabrice_035 wrote:

Code: Select all

present=`wmctrl -l | grep "YASSM v2.7"`
if [ "$present" != "" ] ; then
wmctrl -R "YASSM V2.7" 
exit
fi
Keep it simple:

Code: Select all

wmctrl -R "YASSM V2.7" && exit
Flash wrote:If I understand you correctly, this will prevent more than one instance of the program from running?
Hmmm...that's what the thread title implies but it's not what it does. If multiple instances are already running, the code grabs the first window with "YASSM V2.7" in the title (actually the oldest of the bunch) and moves it to the current desktop.

To prevent multiple instances I use wmctrl when starting programs from tray buttons. First wmctrl looks for any already existing program window and when it finds one it moves it to my present desktop, if it fails, i.e. if the program is not already running, the code starts the program.
Example for galculator:

Code: Select all

wmctrl -R galculator || /usr/bin/galculator
It's also possible to put this code directly into a desktop icon:

Posted: Fri 22 Sep 2017, 08:09
by fabrice_035
@MochiMoppe , It's also possible to put this code directly into a desktop icon
Fabulous :shock: But lose default icon, need to restore after.

[Edit] But no good idea, because redefine icon with Set Icon menu option is for `/bin/bash` , so all shortcuts have same icon :cry: