shell, prevent to open multiple box

How to do things, solutions, recipes, tutorials
Post Reply
Message
Author
User avatar
fabrice_035
Posts: 765
Joined: Mon 28 Apr 2014, 17:54
Location: Bretagne / France

shell, prevent to open multiple box

#1 Post 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

User avatar
Flash
Official Dog Handler
Posts: 13071
Joined: Wed 04 May 2005, 16:04
Location: Arizona USA

#2 Post by Flash »

Could you give us more of a description of what this does, what it is for?

User avatar
fabrice_035
Posts: 765
Joined: Mon 28 Apr 2014, 17:54
Location: Bretagne / France

#3 Post 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.

User avatar
Flash
Official Dog Handler
Posts: 13071
Joined: Wed 04 May 2005, 16:04
Location: Arizona USA

#4 Post by Flash »

If I understand you correctly, this will prevent more than one instance of the program from running?

musher0
Posts: 14629
Joined: Mon 05 Jan 2009, 00:54
Location: Gatineau (Qc), Canada

#5 Post 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
musher0
~~~~~~~~~~
"You want it darker? We kill the flame." (L. Cohen)

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

Re: shell, prevent to open multiple box

#6 Post 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:
Attachments
Move_here_else_open.png
(15.11 KiB) Downloaded 141 times

User avatar
fabrice_035
Posts: 765
Joined: Mon 28 Apr 2014, 17:54
Location: Bretagne / France

#7 Post 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:

Post Reply