You are not logged in.

#1 2012-08-24 02:40:01

Whinis
Member
Registered: 2012-05-26
Posts: 16

Prevent Multiple Instances of A Program and Switch Focus Instead

I am trying to figure out how todo this in my current custom DE.I Use
Compiz
SpaceFM
Adeskbar

And currently  whenever however many times I click an application launcher is how many windows I get of that application, I would prefer that after the first launch it would instead switch to the current instance.

Offline

#2 2012-08-24 03:15:26

Trilby
Inspector Parrot
Registered: 2011-11-29
Posts: 30,554
Website

Re: Prevent Multiple Instances of A Program and Switch Focus Instead

I suspect the only way to do this would be to edit the exec line of the .desktop files.  You could use pgrep and wmctrl to in the exec line to do this.


"UNIX is simple and coherent" - Dennis Ritchie; "GNU's Not Unix" - Richard Stallman

Offline

#3 2012-08-24 03:21:55

Whinis
Member
Registered: 2012-05-26
Posts: 16

Re: Prevent Multiple Instances of A Program and Switch Focus Instead

I feared only editing the .desktop files would fix this. How exactly would this work, I am not that well versed in complex bash statements.

Offline

#4 2012-08-24 13:44:46

Trilby
Inspector Parrot
Registered: 2011-11-29
Posts: 30,554
Website

Re: Prevent Multiple Instances of A Program and Switch Focus Instead

It would not be complex at all.  I'll give a suggestion below, but It'll be somewhat "pseudocode" or untested for two reasons:
1) I simply don't have an EWMH compliant window manager to test this on (which wmctrl requires, and I'm fairly sure compiz is).
2) This may be a bit of a "kludge" solution that will work a majority of the time.  In the minority of cases in which it doesn't work, you may need to troubleshoot and tweek it.  If you are unwilling/uninterested in learning a little bash scripting, then this is not the right solution for you.

To follow from #2 (but slightly off topic), I don't think one needs to be well versed in anything to use arch.  On the contrary, I think arch is a great distro for "newbies" if they want to learn about their computers: arch is the best way to learn.  I cam in to arch quite recently and knew very little bash and only a little C.  Now I've written many of my own tools that I use every day for AUR management, wifi connections, and a window manager.  They are all ugly hacks in the grand scheme of things, but they are my "children" in a sense.  It is by being willing to learn about and interact with my system that I figured out how to do most of this.  The point being, if you are not yet familiar with bash, completing the script below would be a great way to start learning - it should be pretty easy if you are willing to spend a few minutes reading man pages and learning about the commands.  If you are not willing/interested in learning about this stuff, but just want some tool that "just works", then feel free to disregard this post.

PROPOSED SOLUTION:
1) replace the exec line in the relevant desktop files with "scriptname appname".  Lets call the script "launch_once" for now.  So an example for a firefox.desktop file would be

Exec=launch_once firefox %u

In otherwords, simply prepend "launch_once" to each exec command.

2) write the "launch_once" script to be something like the following:

#!/bin/bash

if [[ `pgrep $1` ]]; then
    wmctrl <something here to raise $1 window>
else
    exec "$@"
fi

Obviously the wmctrl command is incomplete, but the man page will tell you how to pass parameters to identify a window for "$1" and issue a command like "raise" or "focus".

EDIT: note, that where this will currently fail, is if - following with the firefox example - you have firefox open already, then in some other app you click a url that tries to use the firefox.desktop file, it will only focus the current firefox window.  It will not open the new url.  I don't know if clicking a url in compiz uses the .desktop file, or if it uses some other mechanism.

Last edited by Trilby (2012-08-24 13:48:09)


"UNIX is simple and coherent" - Dennis Ritchie; "GNU's Not Unix" - Richard Stallman

Offline

#5 2012-08-24 20:05:36

Whinis
Member
Registered: 2012-05-26
Posts: 16

Re: Prevent Multiple Instances of A Program and Switch Focus Instead

I thought about using a script just wasn't sure how to write the code todo that. It makes sense and I will debug and try it tonight

Offline

#6 2012-09-14 02:58:21

Whinis
Member
Registered: 2012-05-26
Posts: 16

Re: Prevent Multiple Instances of A Program and Switch Focus Instead

Ok this may seem like a stupid question but bare with me. What is the best way to detect if the shift/ctrl key is pressed when the bash script is ran

Offline

#7 2012-09-14 03:09:42

ewaller
Administrator
From: Pasadena, CA
Registered: 2009-07-13
Posts: 20,727

Re: Prevent Multiple Instances of A Program and Switch Focus Instead

Whinis wrote:

Ok this may seem like a stupid question but bare with me. What is the best way to detect if the shift/ctrl key is pressed when the bash script is ran

<rant> /s/bare/bear/ s/is/are/  /s/ran/run/ </rant>

Last edited by ewaller (2012-09-14 03:10:29)


Nothing is too wonderful to be true, if it be consistent with the laws of nature -- Michael Faraday
The shortest way to ruin a country is to give power to demagogues.— Dionysius of Halicarnassus
---
How to Ask Questions the Smart Way

Offline

#8 2012-09-14 03:17:36

ConnorBehan
Package Maintainer (PM)
From: Long Island NY
Registered: 2007-07-05
Posts: 1,359
Website

Re: Prevent Multiple Instances of A Program and Switch Focus Instead

Trilby wrote:

EDIT: note, that where this will currently fail, is if - following with the firefox example - you have firefox open already, then in some other app you click a url that tries to use the firefox.desktop file, it will only focus the current firefox window.  It will not open the new url.  I don't know if clicking a url in compiz uses the .desktop file, or if it uses some other mechanism.

In that case the firefox.desktop file can probably be left alone and the application launcher can be set to use firefox-usercreated.desktop. Opening links without the launch_once is probably fine, because the browser's that I've tried detect this and spawn a new tab rather than a new window.


6EA3 F3F3 B908 2632 A9CB E931 D53A 0445 B47A 0DAB
Great things come in tar.xz packages.

Offline

#9 2012-09-14 13:06:47

Whinis
Member
Registered: 2012-05-26
Posts: 16

Re: Prevent Multiple Instances of A Program and Switch Focus Instead

I am currently thinking it will be easier to just modify my bar to launch applications correctly rather than fiddle a bash script that most likely won't do everything I want it too. currently using aDeskBar which is python and should be easily added I hope.

Offline

#10 2012-09-15 02:54:16

ximun
Member
From: Montreal, QC
Registered: 2012-08-14
Posts: 26

Re: Prevent Multiple Instances of A Program and Switch Focus Instead

Trilby wrote:

2) write the "launch_once" script to be something like the following:

This is the single_spacefm.sh script I use to prevent multiple SpaceFM instances:

if ! /bin/pidof /usr/bin/spacefm
then
    /usr/bin/spacefm
else
    wmctrl -a spacefm
fi

no #!/bin/bash at the top (apparently there's a good reason).

SpaceFM's window title is dynamic -- it changes based on the current location. For wmctrl -a to work I gave it a static title (e.g. spacefm). I also edited rc.xml so that hitting Win+f would run the script instead of the usual 'spacefm' command. An alias in ~/.bashrc would work too.

N.b. This is a simplistic version of Trilby's idea, but it should do fine until you find something better.

EDIT: Oops! Failed to notice you were using app launchers. Please ignore my comments about rc.xml and .bashrc.

Last edited by ximun (2012-09-15 20:59:35)


"The problem is, after a week of intense googling, we’ve started to burn out on knowing the answer to everything. God must feel that way all the time. I think people in the year 2020 are going to be nostalgic for the sensation of feeling clueless." Douglas Coupland - jPod

Offline

Board footer

Powered by FluxBB