AutoHotkey V2 - Using HTML in GUIs
Table of Contents
Introduction
This tutorial will guide you through using HTML in AutoHotkey V2 to create a GUI (Graphical User Interface) that displays web content. By the end, you'll be able to fetch a webpage and present it in a custom window using AutoHotkey, enhancing your automation scripts with web integration.
Step 1: Set Up Your AutoHotkey Script
To start using AutoHotkey V2, ensure you have it installed on your machine. Create a new script file with the .ahk
extension.
Code Setup
Begin by writing the following code in your script:
#Requires AutoHotkey v2+
f1:: {
url := 'https://jakob.kaivo.net/'
url := (http('Post', url))
http(verb, url) {
web := ComObject('WinHttp.WinHttpRequest.5.1')
web.Open(verb, url)
web.Send()
web.WaitForResponse()
return web.ResponseText
}
}
Practical Advice
- Use
f1
as a hotkey to trigger the script which fetches the HTML content from the specified URL. - The function
http
handles the HTTP request and returns the response text, making it reusable for different requests.
Step 2: Create the GUI
Next, you’ll create a GUI that will display the fetched web content.
GUI Code
Add the following lines to your script:
myGui := Gui()
myGui.MarginX := "0", myGui.MarginY := "0"
ogcActiveXWB := myGui.Add("ActiveX", "x0 y0 w1024 h600 vWB", "VScroll, shell explorer")
WB := ogcActiveXWB.Value
wb.Navigate("about:blank")
wb.document.write(url)
myGui.Title := "A website"
myGui.Show()
return
Practical Advice
- The GUI is initialized with the margins set to zero for a full window experience.
- An ActiveX control is added to render the HTML content. Adjust the width (
w1024
) and height (h600
) as needed for your application. - The
wb.Navigate("about:blank")
line prepares the web browser control, andwb.document.write(url)
writes the fetched HTML content into the GUI.
Step 3: Run Your Script
After setting up your script, save the changes and run it.
Practical Advice
- Press the
F1
key to execute the script. This will fetch the HTML content from the specified URL and display it in the GUI. - Ensure you have the necessary permissions for running scripts and that your AutoHotkey version is up to date.
Conclusion
In this tutorial, you learned how to create a simple GUI in AutoHotkey V2 that displays HTML content from a specified URL. You can extend this basic setup by modifying the URL or enhancing the GUI with more controls. For further learning, consider exploring more complex web automation tasks with AutoHotkey, such as form submissions or data extraction. Happy scripting!