Page 1 of 1

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

Posted: 14 Jan 2015, 15:37
by Almost_there
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

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

Posted: 14 Jan 2015, 16:10
by MilesAhead
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.

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

Posted: 14 Jan 2015, 17:29
by Almost_there
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?

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

Posted: 14 Jan 2015, 17:34
by LinearSpoon
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.

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

Posted: 14 Jan 2015, 17:37
by Nextron
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.

Now it's obvious how to solve this

Posted: 15 Jan 2015, 12:01
by Almost_there
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: