JonathanDavidArndt
JonathanDavidArndt

Reputation: 2732

How to activate Firefox using AutoIt CLASS matching

Using AutoIt v3, the following script is supposed to activate the Firefox web browser window:

Opt("WinTitleMatchMode", 4)
$winMatchFirefox = "[CLASS:MozillaWindowClass]"

If WinExists($winMatchFirefox) Then
   Local $hWnd = WinActivate($winMatchFirefox)
   If 0 = $hWnd Then
      ToolTip("Firefox could not be activated", 100, 100, "Notice", 1)
   Else
      ToolTip("Firefox activated", 100, 100, "Notice", 1)
   EndIf
Else
   ToolTip("Firefox is not running", 100, 100, "Notice", 1)
EndIf
Sleep(3000)

The above script appears to work. When Firefox is running, the output even reads "Firefox activated", but Firefox is not actually activated.

Swapping in these as the first two lines, everything suddenly works as expected:

Opt("WinTitleMatchMode", 2)
$winMatchFirefox = " - Mozilla Firefox"

Using the AutoIt Window Info tool, it seems that the Basic Window Info is populated, but the Basic Control Info is empty:

window info

Is there a reason the CLASS attribute does not work? The following snippet even appears in the FireFox AutoIt library - FF.au3 (v0.6.0.1b-15):

Local $WINTITLE_MATCH_MODE = AutoItSetOption("WinTitleMatchMode", 4)
WinWaitActive("[CLASS:MozillaWindowClass]")
Sleep(500)
WinSetState("[CLASS:MozillaWindowClass]", "", @SW_MINIMIZE)
BlockInput(0)
AutoItSetOption("WinTitleMatchMode", $WINTITLE_MATCH_MODE)

What is going on here? Usually would prefer to control windows using the CLASS attribute instead of the Window title. Is that not possible here?

Using AutoIt v3.3.10.2, and Firefox 52.0 (32-bit).

Upvotes: 2

Views: 7291

Answers (1)

matrix
matrix

Reputation: 513

Here is an example how to do it using class through the window list:

#include <WinAPI.au3>
#include <Constants.au3>
#include <WindowsConstants.au3>
Local $hFireFoxWin=0,$aWinList=WinList("[REGEXPCLASS:Mozilla(UI)?WindowClass]")
For $i=1 To $aWinList[0][0]
    If BitAND(_WinAPI_GetWindowLong($aWinList[$i][1],$GWL_STYLE),$WS_POPUP)=0 Then
        $hFireFoxWin=$aWinList[$i][1]
        ExitLoop
    EndIf
Next
If $hFireFoxWin Then WinActivate($hFireFoxWin)

You can also read about Advanced Window Descriptions

Your attempt fails because one instance of Mozilla can have several processes mozilla_process and basic WinActivate([CLASS:MozillaWindowClass]) request affects hidden (default) window of Mozilla.

You can check it:

Opt("WinTitleMatchMode", 4)
$winMatchFirefox = "[CLASS:MozillaWindowClass]"
Local $hWnd = WinActivate($winMatchFirefox)
MsgBox(0,"", WinGetProcess("[ACTIVE]", ""))

In my case it will be the process with PID 10128.

Upvotes: 4

Related Questions