You are not logged in.

#1 2022-04-27 12:38:32

flump_cliff_kumquat_fox
Member
Registered: 2022-04-27
Posts: 11

[SOLVED] Script to only run application if it isn't already running

Like the title says, I'd like a surefire way of only executing GUI applications if they're not already running. Furthermore I'd like to focus the window of said application if it is running.

So far, I've made this:

#!/usr/bin/env bash

window_title="${0}"
process="${1}"

if [[ $(pgrep "$1") ]]; then
    wmctrl -a "$window_title"
else
    eval "$1"
fi

I'm trying to integrate this functionality into Xfce4's Application Finder which should be trivial as long as the script works. Suffice to say, the script above does not work. - Any pointers to what I'm doing wrong is greatly appreciated.

Bonus questions
Running this (run firefox but only if it isn't already running):

pgrep 'firefox' || firefox

... works in the CLI, but when adding the command to the Custom Actions in Application Finder it does not.
Why is that?

This snippet (force focus to window with 'Firefox' in the title):

wmctrl -a 'Firefox'

... only seems to work occasionally in the CLI with no indication as to why. I can run the command in one terminal window and get the expected result and then run it in another terminal window without anything happening at all.
Why is that?

Last edited by flump_cliff_kumquat_fox (2022-04-27 15:24:15)

Offline

#2 2022-04-27 15:10:53

flump_cliff_kumquat_fox
Member
Registered: 2022-04-27
Posts: 11

Re: [SOLVED] Script to only run application if it isn't already running

So, inspired by this thread, I decided to rewrite the script. It looks like this now:

#!/usr/bin/env bash

process="${1}"

if
    pgrep "$process" >/dev/null
then
    wmctrl -a $process
else
    "$process" &
fi

Adding this to /usr/local/bin works perfectly when used in conjunction with the Custom Actions in the Application Finder.

Last edited by flump_cliff_kumquat_fox (2022-04-27 15:11:50)

Offline

#3 2022-04-28 08:02:09

flump_cliff_kumquat_fox
Member
Registered: 2022-04-27
Posts: 11

Re: [SOLVED] Script to only run application if it isn't already running

Yeah, earlier version was a mess. It only worked by invoking wmctrl, it didn't execute the process if it wasn't running.

Here's a version that so far works like I want it to:

#!/usr/bin/env bash

process="${1}"
run="${2}"

if [[ -n $(pgrep "$process") ]];then
    wmctrl -a $process
elif [[ -n "$run" ]];then
    eval $run
else
    eval $process
fi

This also enables you to add a second parameter to run processes with options attached.

Offline

#4 2022-05-20 07:53:22

flump_cliff_kumquat_fox
Member
Registered: 2022-04-27
Posts: 11

Re: [SOLVED] Script to only run application if it isn't already running

#!/usr/bin/env bash
#focorun 2.0

focus="${1}" #regex for the title of the window you want to focus if it exists
launch="${2}" #the command you want to run if the above window does not exist - this is optional: if the parameter is not set, we simply execute parameter 1
ignore="${3}" #optional parameter that let's you define a regex for windows (their titles) to exclude in parameter 1

if [[ "$ignore" ]]; then
    windows=$(wmctrl -l | grep -vG "$ignore")
else
    windows=$(wmctrl -l)
fi

window=$(echo "$windows" | grep -G "$focus")

if [[ "$window" ]]; then
    wmctrl -ia "$window"
elif [[ "$launch" ]]; then
    eval "$launch"
else
    eval "$focus"
fi

Last edited by flump_cliff_kumquat_fox (2022-05-20 09:46:20)

Offline

Board footer

Powered by FluxBB