Intro guide: Manipulate browsers using "Browser.Handle" and AHK

Window handles are, well, handy. :grinning:
We’re not just limited to Process IDs for manipulating instances.

In the script below we use AutoHotkey (the script assumes it’s installed) to:

  • Launch a Chrome browser.
  • Take a window handle passed by Robin to the AHK script;
  • Convert the handle to hex for use by AHK;
  • Minimize the browser for four seconds;
  • Show it again;
  • Wait two seconds; and
  • Exit.

(You can extend this principle to other modules, as will be shown in a bit.)

Screenshot (browser handle):

Code:

WebAutomation.LaunchChrome \
    Url:  "https://www.robin-language.org" \
    WindowState:WebAutomation.BrowserWindowState.Normal \
    ClearCache:False ClearCookies:False \
    BrowserInstance=> Browser
wait 3
# Console.Write Message: Browser.Handle # For debugging
set hwnd to Browser.Handle
# The above retrieves a decimal integer representation of the window handle.
# You may want to look at this:
# https://stackoverflow.com/questions/902967/what-is-a-windows-handle
# You can do lots of things with windows and controls if you have their 'hwnd'


# This is not very imaginative. Well minimize the browser for 4 seconds,
# Then show it again.
System.RunDOSCommand \
    DOSCommandOrApplication: "hwnd.ahk " + Browser.Handle\
    WorkingDirectory:'C:\work\RobinTests' \
    StandardOutput=> StandardOutput \
    StandardError=> StandardError \
    ExitCode=> ExitCode

# Okay, it worked. Now we'll close the browser after a couple of seconds.

wait 2

WebAutomation.CloseWebBrowser \
    BrowserInstance: Browser

Here’s a screenshot for basically the same procedure with Excel:

The code:

Excel.Launch \
    Visible:True \
    LoadAddInsAndMacros:False \
    Instance=> ExcelInstance
wait 3
# Console.Write Message: Excel.Handle # For debugging
set hwnd to ExcelInstance.Handle
# The above retrieves a decimal integer representation of the window handle.
# You may want to look at this:
# https://stackoverflow.com/questions/902967/what-is-a-windows-handle
# You can do lots of things with windows and controls if you have their 'hwnd'


# This is not very imaginative. Well minimize Excel for 4 seconds,
# Then show it again.
System.RunDOSCommand \
    DOSCommandOrApplication: "hwnd.ahk " + ExcelInstance.Handle\
    WorkingDirectory:'C:\work\RobinTests' \
    StandardOutput=> StandardOutput \
    StandardError=> StandardError \
    ExitCode=> ExitCode

# Okay, it worked. Now we'll close Excel after a couple of seconds.

wait 2

Excel.Close \
    Instance: ExcelInstance

And the AutoHotkey script needed to make this work:

ahk_code

And finally the AHK code.

; %1% here means the first argument
; (%0% contains the number of arguments)
handle = %1%
;msgbox %handle% ; for debugging

; Format the window handle from Robin as hexadecimal, with prefix
hwnd := Format("0x{:X}", handle)
;msgbox %hwnd% ; for debugging

; First, minimize the window
WinMinimize, ahk_id %hwnd%
; Wait 4 seconds
sleep 4000
; Activate and show the window
WinActivate, ahk_id %hwnd%

ExitApp

I hope this may of use to someone other than me. :grinning:

Regards,
burque505