Restore minimized firefox window

Get help with using AutoHotkey (v1.1 and older) and its commands and hotkeys
gazzat5
Posts: 12
Joined: 16 Aug 2022, 11:30

Restore minimized firefox window

Post by gazzat5 » 16 Aug 2022, 11:33

Hi there,

I'm trying to restore a minimized firefox window, but nothing i'm trying seems to work.
My current code is as following and absolutely nothing happens when I run it!
I'm a noob at this so i appreaciate any and all help!

Code: Select all

#NoEnv  ; Recommended for performance and compatibility with future AutoHotkey releases.
#Warn  ; Enable warnings to assist with detecting common errors.
SendMode Input  ; Recommended for new scripts due to its superior speed and reliability.

;Attempting to Restore Minimized Firefox window.
;PID=18064
;class=MozillaWindowClass
;title=*Mozilla Firefox

f1::
hWindow = WinGet, hWindow, ID, Mozilla Firefox	;that should get active windows handle
return

f2::
;attempt to raise the window
WinActivate — *Mozilla Firefox   ; Activate FF
;WinWaitActive — Mozilla Firefox ; Wait 'till is active

;attempt to raise the window by class
WinActivate, ahk_class MozillaWindowClass

;attempt to raise the window by ID
WinActivate, ahk_id %hWindow%

;attempt to raise the window by PID
WinActivate, ahk_pid 18064

;attempt to raise the window by title regex
SetTitleMatchMode, RegEx
WinActivate, .* - Mozilla Firefox   ;window title must end in "- Mozilla Firefox"

ControlFocus, OK, "*Mozilla Firefox"

;change tabs
Send ^1                         ; Activate tab #2 (ie Youtube)

;change tabs
ControlSend,, {Control Down}1{Control Up}, ahk_id %hWindow%        ; sending directly to window handle ensures reliablity

return

f12::
ExitApp
TIA

User avatar
mikeyww
Posts: 26851
Joined: 09 Sep 2014, 18:38

Re: Restore minimized firefox window

Post by mikeyww » 16 Aug 2022, 12:56

Code: Select all

#IfWinExist ahk_exe firefox.exe
^F3::
WinActivate
SoundBeep, 1500
Return
#IfWinExist
hWindow = WinGet, hWindow, ID, Mozilla Firefox
AHK commands cannot be used as expressions. := can be used with expressions; see documentation.

:arrow: Example

Careful with long hyphens. RegEx is below.

Code: Select all

WinActivate, .* — Mozilla Firefox
How did I find the long hyphen? Run Window Spy => Copy text.

gazzat5
Posts: 12
Joined: 16 Aug 2022, 11:30

Re: Restore minimized firefox window

Post by gazzat5 » 17 Aug 2022, 03:52

Hi Mikey,

i tried both of your suggested code snippets and neither worked!
Firefox stayed resolutely minimized throughout. :(
Any other suggestions?

Cheers,
Garreth

User avatar
mikeyww
Posts: 26851
Joined: 09 Sep 2014, 18:38

Re: Restore minimized firefox window

Post by mikeyww » 17 Aug 2022, 05:59

The second one requires the match mode that you provided earlier.

How do you know that the window is minimized? Just because you can't see it?

You can check a window's state: https://www.autohotkey.com/docs/commands/WinGet.htm#MinMax

Did you hear the beep with the first script?

Below, post the script that you are testing.

gazzat5
Posts: 12
Joined: 16 Aug 2022, 11:30

Re: Restore minimized firefox window

Post by gazzat5 » 17 Aug 2022, 09:19

No beep heard although firefox.exe is open.

Code tested as below:

Code: Select all

#NoEnv  ; Recommended for performance and compatibility with future AutoHotkey releases.
#Warn  ; Enable warnings to assist with detecting common errors.
SendMode Input  ; Recommended for new scripts due to its superior speed and reliability.
SetWorkingDir %A_ScriptDir%  ; Ensures a consistent starting directory.


SetTitleMatchMode, 2   ;window title can contain WinTitle anywhere inside it to be a match
;#WinActivateForce
;WinActivate, - Mozilla Firefox   ;as it always has the "- Mozilla Firefox" part in it

Winget, FFState, List, .* - Mozilla Firefox
Msgbox, %FFState%
; returns 0

Winget, FFState, MinMax, .* - Mozilla Firefox
Msgbox, %FFState%
;returns nothing

WinActivate, .* — Mozilla Firefox

#IfWinExist ahk_exe firefox.exe
^F3::
WinActivate
SoundBeep, 1500
exit
#IfWinExist

User avatar
mikeyww
Posts: 26851
Joined: 09 Sep 2014, 18:38

Re: Restore minimized firefox window

Post by mikeyww » 17 Aug 2022, 09:42

Run alone with no other code. If there is no beep when you press the hotkey, then it could mean:

1. Script not running. Check for green AHK icon in system tray.
2. You did not trigger the hotkey, Ctrl+F3, or that hotkey is used by another program.
3. Window running firefox.exe is not found. Check Windows Task Manager -> Details

Code: Select all

#IfWinExist ahk_exe firefox.exe
^F3::
WinActivate
SoundBeep, 1500
Return
#IfWinExist

gazzat5
Posts: 12
Joined: 16 Aug 2022, 11:30

Re: Restore minimized firefox window

Post by gazzat5 » 17 Aug 2022, 10:07

Aha fab, that works.

I actually have two firefox windows open, is there a way to select between them?

Thanks for you help!

User avatar
mikeyww
Posts: 26851
Joined: 09 Sep 2014, 18:38

Re: Restore minimized firefox window

Post by mikeyww » 17 Aug 2022, 10:41

See :arrow: WinTitle to learn more about how to specify a WinTitle. WinGet can get a list of windows if you need them.

Related: WinGetTitle

gazzat5
Posts: 12
Joined: 16 Aug 2022, 11:30

Re: Restore minimized firefox window

Post by gazzat5 » 17 Aug 2022, 12:01

Okay, i'm on the right track.
I can get the Window ID for a window and use that to activate it.

How would i go about getting the Window ID of each firefox window, compare the Hex IDs and activate just the lowest number.

I'm not sure how to get the id of both windows as maybe an array or something to compare them... :crazy:

Any help appreciated!

User avatar
mikeyww
Posts: 26851
Joined: 09 Sep 2014, 18:38

Re: Restore minimized firefox window

Post by mikeyww » 17 Aug 2022, 14:39

You could read the documentation for WinGet.

Code: Select all

WinGet, ff, List, ahk_exe firefox.exe
Loop, %ff%
 (!lowest || ff%A_Index% < lowest) && lowest := ff%A_Index%
WinActivate, ahk_id %lowest%

gazzat5
Posts: 12
Joined: 16 Aug 2022, 11:30

Re: Restore minimized firefox window

Post by gazzat5 » 18 Aug 2022, 08:45

🤦‍♂️ my bad!
:thumbup:

One more question then. I've modified the code to send Control+1 after raising the window, but despite waiting for the window to load, and trying two different methods of indicating I want Control+1 sent, Firefox does not respond (by switching to the first tab).

Any suggestions?

My full code is now as follows (not hotkey as i intend to run the AHK from another app...

Code: Select all

SetTitleMatchMode, 2
WinGet, ff, List, ahk_exe firefox.exe
Loop, %ff%
 (!highest || ff%A_Index% > highest) && highest := ff%A_Index%
WinActivate, ahk_id %highest%
WinWaitActive — Mozilla Firefox
Sleep 3000
ControlSend,, {Control down}1{Control up}, ahk_id %highest%
ControlSend,, ^1, ahk_id %highest%
exit
🙏

User avatar
mikeyww
Posts: 26851
Joined: 09 Sep 2014, 18:38

Re: Restore minimized firefox window

Post by mikeyww » 18 Aug 2022, 09:08

Code: Select all

WinGet, ff, List, ahk_exe firefox.exe
Loop, %ff%
 (ff%A_Index% > highest) && highest := ff%A_Index%
WinActivate, % winTitle := "ahk_id" highest
If WinActive(winTitle)
 Send ^1

gazzat5
Posts: 12
Joined: 16 Aug 2022, 11:30

Re: Restore minimized firefox window

Post by gazzat5 » 18 Aug 2022, 12:17

Fantastic. I'm tantilizingly close to completing this script...

I now want to check if the current firefox window contains the correct browser tab, and if not, loop through the other firefox windows and select the first tab until it does.

I'm not sure how to increment through an array in this language. My code is as below. Any suggestions?
Cheers, i really appreaciate your time and knowledge!

Code: Select all

; PSEUDOCODE: If (WinActive WinTitle not contain " - TMetric — Mozilla Firefox"), loop through firefox windows until it is
WinGet, ff, List, ahk_exe firefox.exe
Loop {
; Increment though window ID list, activating the window until we find the one with the tab we want
	; HOW TO INCREMENT THROUGH ARRAY/LIST?
	WinActivate, % WinTitle := "ahk_id" ff%A_Index%
	WinWaitActive — Mozilla Firefox
	Send ^1 ;select the first browser tab
	Sleep 1500 ;give it a little time to settle
	; [TODO] add a counter that breaks the loop if we have iterated more times than there are firefox windows...
} Until (WinActive(- TMetric — Mozilla Firefox))
; loop through list and try other firefox windows until we find it...

User avatar
mikeyww
Posts: 26851
Joined: 09 Sep 2014, 18:38

Re: Restore minimized firefox window

Post by mikeyww » 18 Aug 2022, 16:18

Would give it a go and see what happens. You may need to set the title match mode.

Line 7 is usually not needed.

You could consider some form of error trapping-- a way to handle the situation that your target WinTitle never becomes active in the loop. A simple approach to that is limiting the number of iterations to the number of windows, which you already know (is %ff%).

If you address potential unexpected situations like that, you will discover that some of them eventually occur!

Post Reply

Return to “Ask for Help (v1)”