I know the question is a bit vague. I'll try to explain better below:
My app (python/gtk) is mostly an indicator. Using this indicator you can choose to show/hide the main window.
When I try to open a new instance of the app I made it so it would check if the app is already running. If so, it would abort trying to run the app.
Now I want to adjust it, so before aborting I want it to bring the already active app's main window to the foreground. Even if this window isn't opened at the moment.
So I believe my question is: How can I get a (global?) variable/instance of my already active app from my new app? (so I can bring my main window to the foreground)
EDIT:
I just found this method in the api: GtkWindow - set_startup_id().
This says: Normally, startup identifier is managed automatically and you should only use this function in special cases like transferring focus from other processes.
So this means it should be possible to bring focus to a window from another process. But how would I get this id? And how would I use this id to bring it to the foreground?
Lets start by saying that there are lots and lots of ways. You normally setup a token/identifier when the program starts, so later instances can look for the existence of that token.
I will describe one way which uses dbus.
Overview:
When starting a program, it can register itself on the session dbus under a unique name (e.g. "org.nicklemaire.myprogram"). Further instances of the program can check if such an accesspoint is already registered, and if so, tell the program what to do via this dbus access point (e.g. get focus, open a website, play a song). The last part is probably necessary when you want behaviour similar to "firefox askubuntu.com", which opens this page in a new tab in an already running instance.
Code:
Test:
Open a terminal and run the program:
myprogram.py
. It will not terminate because we currently want to have it running and waiting for a second instance to start.Now do this: open another terminal and run the program again, this time with an additional argument
myprogram.py askubuntu.com
. It should print: "Another instance was running and notified." While in the first terminal, you should get an output similar to this: "got the following parameter from another instance: askubuntu.com"The other part of your question: raising a program is described here: https://stackoverflow.com/questions/9054462/how-do-i-raise-a-window-that-is-minimized-or-covered-with-pygobject
Basically, you have to call
mywindow.present()
in thestartup
method.Thanks to xubuntix's great answer, I've made a module which makes it simple:
Please refer the following gist for any further revisions in the code above:
Not sure if this is the ID your looking for:
Open
System Monitor
, either by going to the Applications, pressingCTRL+ALT+DEL
or by typing in terminalgnome-system-monitor
.Go to
View
tab, on the top bar. SelectAll Processes
and Dependencies. Go toEdit
tab, on the top bar, and openPreferences
.On the
Processes
tab, underInformation Fields
, selectID
.Then try to find you program in the list. Good luck!