How can I prevent a program to open more than one window?

Get help with using AutoHotkey and its commands and hotkeys
User avatar
Almost_there
Posts: 404
Joined: 30 Sep 2014, 10:32

How can I prevent a program to open more than one window?

14 Jan 2015, 15:37

Hi.

I have two version of Firefox installed on a computer. One is a portable version and the other one is installed normally.

I want to use AHK to only allow one session of Firefox to exist. That is - if I first open the portable version og Firefox, the installed version should close emediately after trying to start. Or vice versa.

Now the big issue that make me wonder if this is impossible to achieve:
Both the portable version and the installed version gives the following information about their process (read from ActiveWindowInfoInclude.ahk):
<web site title> - Mozilla Firefox
ahk_class MozillaWindowClass
ahk_exe firefox.exe
As if that isn't enough, Firefox changes it's PID during a session.


Any ideas?

Thanks
User avatar
MilesAhead
Posts: 232
Joined: 03 Oct 2013, 09:44

Re: How can I prevent a program to open more than one window

14 Jan 2015, 16:10

Is this just for your own usage? If so and you set Firefox to open urls in new tabs then any time you see a new window MozillaWindowClass you could just close it. If for other's usage then it's bound to be a bit messier.
"My plan is to ghostwrite my biography. Then hire another writer to put his
name on it and take the blame."

- MilesAhead
User avatar
Almost_there
Posts: 404
Joined: 30 Sep 2014, 10:32

Re: How can I prevent a program to open more than one window

14 Jan 2015, 17:29

MilesAhead wrote:Is this just for your own usage?
Yes, for my own use only :)
MilesAhead wrote:If so and you set Firefox to open urls in new tabs then any time you see a new window MozillaWindowClass you could just close it.
No, I want to be able to have multiple tabs open inside that one session, and being able to switch between tabs as normal.
MilesAhead wrote:If for other's usage then it's bound to be a bit messier.
What do you mean with other usage?
User avatar
LinearSpoon
Posts: 156
Joined: 29 Sep 2013, 22:55

Re: How can I prevent a program to open more than one window

14 Jan 2015, 17:34

You might get away with just changing the exe filename on one version. I don't have firefox installed so I can't really say if that might break anything (such as updating).

Then all you have to do is keep track of which one is running, and watch for the other version to pop up.
User avatar
Nextron
Posts: 1379
Joined: 01 Oct 2013, 08:23
Location: Netherlands OS: Win10 AHK: Unicode x32

Re: How can I prevent a program to open more than one window

14 Jan 2015, 17:37

Does the portable version still work if you rename the exe?

I've got something the other way around, to prevent closing a window if multiple exist in order to avoid losing tabs from the session.
Try this (untested):

Code: Select all

ShellMessage(wParam, lParam:=""){
	static init:=DllCall("RegisterShellHookWindow", UInt,A_ScriptHwnd)
	,MsgNum:=OnMessage(DllCall("RegisterWindowMessage", Str,"SHELLHOOK"), "ShellMessage")
	,cleanup:={base:{__Delete: "ShellMessage"}}

	if !(cleanup)
		return DllCall("DeregisterShellHookWindow","Ptr",A_ScriptHwnd), OnMessage( MsgNum, "" )

	;hWnd:=lParam
	If (wParam=1) ;HSHELL_WINDOWCREATED:=1
		If WinExist("ahk_exe firefox.exe") && WinExist("ahk_exe firefoxportable.exe") && !WinExist("Confirm close"){ ;Both versions exist
			If WinExist("ahk_id " lParam " ahk_exe firefox.exe") ;Installed version executed last
				WinClose,ahk_exe firefoxportable.exe ;Close portable
			else WinExist("ahk_id " lParam " ahk_exe firefoxportable.exe") ;Portable executed last
				WinClose,ahk_exe firefox.exe ;Close installed
			WinWait,Confirm close,,1
			ControlSend,,{Enter},Confirm close
		}
}
If you can't rename the exe, you'll need to use something like WinGet,x,List,ahk_exe firefox.exe to retrieve a list of all firefox windows and WinGet,y,ProcessPath,parsed_x to distinguish them.
User avatar
Almost_there
Posts: 404
Joined: 30 Sep 2014, 10:32

Now it's obvious how to solve this

15 Jan 2015, 12:01

Thanks for guiding me into the full path trail.

I found that with AutoHotkey_L I can use Winget directly to get the full path of any window.

So in this case, I can use this code when Firefox is running.

Code: Select all

WinGet Path, ProcessPath, ahk_class MozillaWindowClass
MsgBox, 0, Firefox full path, %Path%
I now have an idea how to make the code work. Shouldn't be that hard, so I say it's solved.


Thanks :bravo:

Return to “Ask For Help”

Who is online

Users browsing this forum: DataLife, djuga, songdg and 29 guests