You are not logged in.

#1 2015-12-07 12:00:11

Snap
Member
Registered: 2015-10-02
Posts: 465

The launch script

This is one of the goodies you can find in Vsido. It needs wmctrl.

launch

#!/bin/bash
# This script acts as a launcher for apps that observes the following rules:
#   1. If the app is not running, then start it up
#   2. If the app is running, don't start a second instance, instead:
#     2a. If the app does not have focus, give it focus
#     2b. If the app has focus, minimize it
# Reference link: http://forum.xfce.org/viewtopic.php?id=6168&p=1

# there has to be at least one parameter, the name of the file to execute
if [ $# -lt 1 ]
then
  echo "Usage: `basename $0` {executable_name parameters}"
  exit 1
fi

BNAME=`basename $1`

# test to see if program is already running
if [ "`wmctrl -lx | tr -s ' ' | cut -d' ' -f1-3 | grep -i $BNAME`" ]; then 
    # means it must already be running
    ACTIV_WIN=$(xdotool getactivewindow getwindowpid)
    LAUNCH_WIN=$(ps -ef | grep "$BNAME" | grep -v grep | tr -s ' ' | cut -d' ' -f2 | head -n 1)

    if [ "$ACTIV_WIN" == "$LAUNCH_WIN" ]; then
        # launched app is currently in focus, so minimize
        xdotool getactivewindow windowminimize
    else
        # launched app is not in focus, so raise and bring to focus
        for win in `wmctrl -lx | tr -s ' ' | cut -d' ' -f1-3 | grep -i $BNAME | cut -d' ' -f1`
        do
            wmctrl -i -a $win
        done
    fi
    exit

else
    # start it up
    $*&
fi

exit 0

IIRC by default in Vsido it's used everywhere in the Fluxbox menu and the keys config files, but I prefer using it for certain apps like web browsers, editors like medit or geany, file managers like spacefm, xfce4-terminal (they all have tabs so a single instance of each app keeps all tidier), also for Virtualbox, etc.... I have different keybindings to start certain apps with or without the launch script depending on the purpose/needs and I never use it for others. Be creative and you will love it.

Offline

#2 2015-12-07 12:38:03

brontosaurusrex
Middle Office
Registered: 2015-09-29
Posts: 1,889
Website

Re: The launch script

Thanks for sharing, I guess this would work great with wbar as well?

Online

#3 2015-12-07 13:15:00

Sector11
The Tpyo Knig Mod
From: 77345 ¡#
Registered: 2015-08-20
Posts: 5,632

Re: The launch script

It needs xdotool as well.  Nice looking app.


BunsenLabs Forum Rules ---== I'm a Conky 1.9'er ==---
System:    Host: d67 Kernel: 4.9.0-9-amd64 x86_64 (64 bit gcc: 6.3.0)
Desktop: Openbox 3.6.1 Distro: Debian GNU/Linux 9 (stretch)

Offline

#4 2015-12-07 17:21:42

Snap
Member
Registered: 2015-10-02
Posts: 465

Re: The launch script

Never used wbar. If it works as a window it should be treated the same by the WM.

It needs xdotool as well

Thanks for pointing out, S11. Since both wmctrl and xdotool always live inside my systems I wasn't aware of it.

Offline

#5 2015-12-07 17:38:32

damo
....moderator....
Registered: 2015-08-20
Posts: 5,157

Re: The launch script

Although AFAIK, `xdotool` and `wmctrl` should both be present on a default BL install.


Be Excellent to Each Other...

FORUM RULES and posting guidelines «» Help page for forum post formatting
Artwork on DeviantArt  «» BunsenLabs on DeviantArt

Offline

#6 2015-12-07 18:04:47

Sector11
The Tpyo Knig Mod
From: 77345 ¡#
Registered: 2015-08-20
Posts: 5,632

Re: The launch script

^I'm sure they do.  I mentioned it since the script came from VSIDO and Snap pointed out that "It needs wmctrl." and in the script it clearly uses xdotool as well.

So we LabRats are covered, but are all visitors?  Just trying to make sure Snap gets a home run.  wink

@Snap
xdotool has been on my system ever since I started playing with mrpeachy's interactive conkys since those require both xdtool and wmctrl.  I already had "wmctrl" as I use it to start conkys on various desktops.


BunsenLabs Forum Rules ---== I'm a Conky 1.9'er ==---
System:    Host: d67 Kernel: 4.9.0-9-amd64 x86_64 (64 bit gcc: 6.3.0)
Desktop: Openbox 3.6.1 Distro: Debian GNU/Linux 9 (stretch)

Offline

#7 2015-12-07 18:12:28

PackRat
jgmenu user Numero Uno
Registered: 2015-10-02
Posts: 1,110

Re: The launch script

^ Do you know if they were added? last I installed Debian proper neither was installed by default.

@b-rex - it should prevent a second instance of wbar from starting, but I don't know about the focus and minimizing aspects of it.

If you do not want to run a script, you can also use something like:

wmctrl -a firefox || firefox

as a key binding. It will prevent a second incidence of the app from starting, and bring the currently running instance into focus. No minimizing.

Last edited by PackRat (2015-12-07 18:12:58)


You must unlearn what you have learned.
    -- yoda

Offline

#8 2015-12-07 18:17:03

damo
....moderator....
Registered: 2015-08-20
Posts: 5,157

Re: The launch script

Sector11 wrote:

^I'm sure they do.  I mentioned it since the script came from VSIDO and Snap pointed out that "It needs wmctrl." and in the script it clearly uses xdotool as well.

So we LabRats are covered, but are all visitors?  Just trying to make sure Snap gets a home run.  wink
.....

I've started adding a little bit of bash to all my scripts to test if required tools are installed. I've posted it here:
Script snippet to test if required tools exist


Be Excellent to Each Other...

FORUM RULES and posting guidelines «» Help page for forum post formatting
Artwork on DeviantArt  «» BunsenLabs on DeviantArt

Offline

#9 2015-12-07 18:20:09

damo
....moderator....
Registered: 2015-08-20
Posts: 5,157

Re: The launch script

PackRat wrote:

^ Do you know if they were added? last I installed Debian proper neither was installed by default....

They are dependencies of some of the bl- scripts, so I expect the iso/packages boys have it organised (but don't quote me on that, since that stuff is above my pay-grade smile )


Be Excellent to Each Other...

FORUM RULES and posting guidelines «» Help page for forum post formatting
Artwork on DeviantArt  «» BunsenLabs on DeviantArt

Offline

#10 2015-12-07 20:41:46

Snap
Member
Registered: 2015-10-02
Posts: 465

Re: The launch script

I've started adding a little bit of bash to all my scripts to test if required tools are installed. I've posted it here:
Script snippet to test if required tools exist

Awesome! Thank you!

Offline

#11 2015-12-07 22:23:55

damo
....moderator....
Registered: 2015-08-20
Posts: 5,157

Re: The launch script

I hope you don't mind some comments on the bash wink

There are several unnecessary pipes which can be simplified (eg use pgrep, and parameter substitution), and use of frowned-upon command substitution.
Why backticks should generally be avoided is explained here

In general you really should only use the form $(), it's escaping-neutral, it's nestable, it's also POSIX.

After extensive tutoring by @johnraff and @xaos52 in recent months, I suggest....

#!/bin/bash
#.....
# there has to be at least one parameter, the name of the file to execute
if (( $# == 0 ));then
  echo "Usage: $(basename $0) executable_name [parameters]"
  exit 1
fi

BNAME=$(basename $1)

# test to see if program is already running
if [[ $(pgrep -a $BNAME) ]]; then 
    # means it must already be running
    ACTIV_WIN=$(xdotool getactivewindow getwindowpid)
    LAUNCH_WIN=$(pgrep $BNAME)

    if [[ $ACTIV_WIN = $LAUNCH_WIN ]]; then
        # launched app is currently in focus, so minimize
        xdotool getactivewindow windowminimize
    else
        # launched app is not in focus, so raise and bring to focus
        win=$(wmctrl -lx | grep $BNAME)
        wmctrl -i -a ${win%% *}
    fi
    exit

else
    # start it up
    $*&
fi

exit 0

Be Excellent to Each Other...

FORUM RULES and posting guidelines «» Help page for forum post formatting
Artwork on DeviantArt  «» BunsenLabs on DeviantArt

Offline

#12 2015-12-08 11:15:59

xaos52
The Good Doctor
From: Planet of the @pes
Registered: 2015-09-30
Posts: 695

Re: The launch script

Here is what I would make of this:

#!/bin/bash
#.....
# there has to be at least one parameter, the name of the file to execute
if (( $# == 0 ));then
  echo "Usage: $(basename $0) executable_name [parameters]"
  exit 1
fi

launchee=$(basename "$1")

# test to see if program is already running
if [[ $(pgrep -a "$launchee") ]]; then 
    # means at least one launchee process  must already be running
    active_window_pid=$(xdotool getactivewindow getwindowpid)
    launchee_pids=($(pgrep "$launchee"))

    # is acitve_window_pid one of the already running launchee?
    if [[ "${launchee_pids[@]}" =~ "*${active_window_pid}*" ]]; then
        # launched app is currently in focus, so minimize
        xdotool getactivewindow windowminimize
    else
        # launchee instance is not in focus, so raise and bring to focus
        # which one do you bring into focus if there are many?
        # First one is easiest
        declare -a wins
        while IFS= read -r; do
            wins+=("$REPLY")
        done < <(wmctrl -lx | grep "$launchee")
        # printf '%s\n' "${wins[@]}"
        wmctrl -i -a "${wins[0]%% *}"
    fi
else
    # start it up
    "$@"&
fi

exit 0

Remarks:
As a general rule ( I know there are exceptions ) quote your variables.
Your script breaks when the launchee contains embedded spaces.
I know it is not wise to call your application 'my very own application', but you can do it, and your script should not break on it.

Your script should handle the situation where some launchee are already running.

Reserve all uppercase variables for variables you read from your environment, such as EUID, USER, HOME.

Your script should handle launchee with arguments, f.e.

launcher xterm -geometry 100x70+10+10

Hope you don't mind smile
Any remarks gratefully accepted

Last edited by xaos52 (2015-12-08 11:21:14)

Offline

#13 2015-12-08 11:39:25

Snap
Member
Registered: 2015-10-02
Posts: 465

Re: The launch script

@damo & doc

I hope you don't mind [...]

Me? I'm not the author. It was apparently borrowed by VastOne from some forum. As a launch addict i can only applaud and welcome your stress tests and improvements. Thanks, guys!

EDIT: The script was not "borrowed" by VastOne from the xfce forum. He's one of the authors and the OP starter.

Last edited by Snap (2015-12-09 07:57:11)

Offline

#14 2015-12-08 15:02:54

damo
....moderator....
Registered: 2015-08-20
Posts: 5,157

Re: The launch script

xaos52 wrote:

Reserve all uppercase variables for variables you read from your environment, such as EUID, USER, HOME

What is the reasoning behind this? Is it just to prevent possible clashes with env vars?

The learning never stops!

When this script is polished it could be a valuable addition to the BL arsenal smile


Be Excellent to Each Other...

FORUM RULES and posting guidelines «» Help page for forum post formatting
Artwork on DeviantArt  «» BunsenLabs on DeviantArt

Offline

#15 2015-12-08 18:05:38

xaos52
The Good Doctor
From: Planet of the @pes
Registered: 2015-09-30
Posts: 695

Re: The launch script

Offline

#16 2015-12-08 20:29:53

brontosaurusrex
Middle Office
Registered: 2015-09-29
Posts: 1,889
Website

Re: The launch script

@damo
launchee iceweasel
Cannot convert argument to number.

@xaos52
launchee iceweasel
wmctrl: option requires an argument -- 'a'

Original version seems to work on all tested, but doesn't fully understand urxvt. Also makes my wbar look smarter.
launchee.th.png

Last edited by brontosaurusrex (2015-12-09 20:42:46)

Online

#17 2015-12-08 20:54:16

damo
....moderator....
Registered: 2015-08-20
Posts: 5,157

Re: The launch script

brontosaurusrex wrote:

@damo
launchee iceweasel
Cannot convert argument to number.

I can't say I did much testing - I was mainly concerned with improving some of the ugly bash code hmm


Be Excellent to Each Other...

FORUM RULES and posting guidelines «» Help page for forum post formatting
Artwork on DeviantArt  «» BunsenLabs on DeviantArt

Offline

#18 2015-12-09 03:17:45

johnraff
nullglob
From: Nagoya, Japan
Registered: 2015-09-09
Posts: 5,959
Website

Re: The launch script

xaos52 wrote:

Excellent! Saved.


John
--------------------
( a boring Japan blog , Japan Links, idle twitterings  and GitStuff )
In case you forget, the rules.

Offline

#19 2015-12-09 07:52:42

Snap
Member
Registered: 2015-10-02
Posts: 465

Re: The launch script

Original version seems to work on all tested, but doesn't fully understand urxvt.

Yeah, you want to start something in a new urxvt tab, don't you? It's not a launch fault. It happens with or without it. I'm also struggling to make it work. That's one of the reason why I keep xfce4-terminal on board. You can start anything into a new tab instead of a new instance easily but haven't found a way for urxvt yet.

Offline

#20 2015-12-09 07:58:09

Snap
Member
Registered: 2015-10-02
Posts: 465

Re: The launch script

some of the ugly bash code

It might be ugly, but works flawless.

Offline

#21 2015-12-09 09:26:03

brontosaurusrex
Middle Office
Registered: 2015-09-29
Posts: 1,889
Website

Re: The launch script

Snap wrote:

you want to start something in a new urxvt tab, don't you?

Not really, just the usual toggle behavior, actually no idea what I was doing there (probably typos) as it seems to work now.

p.s. I should probably change all this wbar statements to something like

(launchee iceweasel || iceweasel)

so wbar it will fall to its standard behavior if launchee script is not found.

Last edited by brontosaurusrex (2015-12-09 11:37:54)

Online

#22 2015-12-10 12:35:02

xaos52
The Good Doctor
From: Planet of the @pes
Registered: 2015-09-30
Posts: 695

Re: The launch script

Here is a new version of the launcher script.
Now using window id's exclusively in stead of pids.

#!/bin/bash
#.....
# there has to be at least one parameter, the name of the file to execute
if (( $# == 0 ));then
  echo "Usage: $(basename $0) executable_name [parameters]"
  exit 1
fi

has_element () {
  local e
  for e in "${@:2}"; do [[ "$e" == "$1" ]] && return 0; done
  return 1
}

launchee=$(basename "$1")

# test to see if program is already running
declare -a launchee_win_ids
launchee_win_ids=( $(wmctrl -lx | awk -v pattern="$launchee" 'BEGIN {IGNORECASE = 1} $3 ~ pattern {print $1}' ) )
if (( ${#launchee_win_ids[@]} ));
then 
    # means at least one launchee process  must already be running
    active_window_id=$(printf '0x%8x' $(xdotool getwindowfocus))
    active_window_id=${active_window_id// /0}

    # is acitve_window_pid one of the already running launchee?
    if has_element "$active_window_id" "${launchee_win_ids[@]}"; then
        # launched app is currently in focus, so do nothing
        # xdotool getactivewindow windowminimize
        :
    else
        # launchee instance is not in focus, so raise and bring to focus
        # which one do you bring into focus if there are many?
        # First one is easiest
        wmctrl -i -a "${launchee_win_ids[0]}"
    fi
else
    # start it up
    "$@"&
fi

exit 0

I have changed the behaviour slightly:
If you now launch an application from an already running instance of that application, it now does nothing.
The old version then minimised the current instance.

Try it out and let me know for which application if it does not work for you

BTW:
The script contains bash code to transform a decimal window_id to a hexadecimal one.

Last edited by xaos52 (2015-12-10 12:37:32)

Offline

#23 2015-12-10 13:12:02

brontosaurusrex
Middle Office
Registered: 2015-09-29
Posts: 1,889
Website

Re: The launch script

@xaos52, nice, slightly moded to enable minimize/maximize behavior and to shut-up the shellcheck.net
https://raw.githubusercontent.com/bront … n/launchee

Any purpose of the ":" line?

Last edited by brontosaurusrex (2015-12-10 13:22:42)

Online

#24 2015-12-10 15:12:27

damo
....moderator....
Registered: 2015-08-20
Posts: 5,157

Re: The launch script

brontosaurusrex wrote:

@xaos52, nice, slightly moded to enable minimize/maximize behavior and to shut-up the shellcheck.net
https://raw.githubusercontent.com/bront … n/launchee

Any purpose of the ":" line?

A quick web search with "bash colon" came up with:

Bash Reference Manual wrote:

: (a colon)

    : [arguments]

    Do nothing beyond expanding arguments and performing redirections. The return status is zero.

and

you can usually use either : or true. Both are specified by POSIX, and some find true easier to read. However there is one interesting difference: : is a so-called POSIX special built-in, whereas true is a regular built-in.


Be Excellent to Each Other...

FORUM RULES and posting guidelines «» Help page for forum post formatting
Artwork on DeviantArt  «» BunsenLabs on DeviantArt

Offline

#25 2015-12-10 22:05:18

brontosaurusrex
Middle Office
Registered: 2015-09-29
Posts: 1,889
Website

Re: The launch script

Right, sort of an empty event? So if
# xdotool getactivewindow windowminimize
is enabled it serves no purpose?

Online

Board footer

Powered by FluxBB