This guide is not intended to replace the great work that other (more advanced) members have put into other guides. It is designed to help non-coders (n00bz) grasp the basic concepts of AutoHotkey v1.1+ COM.
This guide will focus on giving the reader a baseline understanding of the use of the Component Object Model(COM) which can be used to manipulate an application with designed with Document Object Model or DOM such as Internet Explorer.
Q: What is COM?
The Component Object Model is a collection of automation objects that allows a user to interface with various methods and properties of an application.
Q: How do I use it in my script?
There is no easy answer to this question. Why? Because there are different commands to every type of COM object. For instance the methods for Internet Explorer are completely different from MS Office.
In this tutorial I will focus on using COM to script simple commands that will be used to automate IE. Before you can do anything with the IE DOM you have to create a handle to the application.
wb := ComObjCreate("InternetExplorer.Application")
"wb" or Web Browser is the common name for your handle to IE. Your script doesn't care what you name your handle you could name it "WhatAboutBob" if you really wanted to.
Now that you have a handle you need to do something with it. Think of it like the steering wheel of a car. Just because you have the wheel in your hands doesn't mean your driving the car.
wb.Visible := True
This is the next line of code you will HAVE to have. Your code can work without it but you won't be able to see anything going on. By default IE starts off in invisible mode. I'll go ahead and point out that ".Visible" is NOT AHK code. It is a built in method in the DOM. You can access it through any COM compatible language (Ruby,C++,ect).
wb.Navigate("Google.com")
At this point we have actually done something. That is open up an instance of IE and navigated to google.com. You could also store an address in a variable.
URL = Google.com wb.Navigate(URL)
What about accessing an already open web browser? Good Q! There is no simple command for this. Instead you will have to rely on a function (I did not write) to do this for you.
IEGet(Name="") ;Retrieve pointer to existing IE window/tab { IfEqual, Name,, WinGetTitle, Name, ahk_class IEFrame Name := ( Name="New Tab - Windows Internet Explorer" ) ? "about:Tabs" : RegExReplace( Name, " - (Windows|Microsoft) Internet Explorer" ) For wb in ComObjCreate( "Shell.Application" ).Windows If ( wb.LocationName = Name ) && InStr( wb.FullName, "iexplore.exe" ) Return wb } ;written by Jethrow
If your thinking "Holy Heart Attack Batman!" fear not you don't need to understand how this function works to implement it into your script. What this function does is looks through the windows on your computer for IE and then for the tab name of the page you sent it OR default to the last activated tab.
wb := IEGet() ;Last active window/tab ;OR wb := IEGet("Google") ;Tab name you define can also be a variable
Now that we can open IE and navigate to pages we can manipulate things now right? Wrong!
notice the loading bar at the bottom of the page or wherever your browser has it? That is your first adversary. You are going to have to tell your script to wait till your page is done loading.
IELoad(wb) ;You must send the function your Handle for it to work
This is a function that is based of the iWeb function Tank built. If you don't care to understand how it works then just know that you must have this (or another similer) function on any script that deals with IE.
IELoad(wb) ;You need to send the IE handle to the function unless you define it as global. { If !wb ;If wb is not a valid pointer then quit Return False Loop ;Otherwise sleep for .1 seconds untill the page starts loading Sleep,100 Until (wb.busy) Loop ;Once it starts loading wait until completes Sleep,100 Until (!wb.busy) Loop ;optional check to wait for the page to completely load Sleep,100 Until (wb.Document.Readystate = "Complete") Return True }
Now that you can access an existing page or launch a new one we can actually do something useful. Let's fill the search field. We are going to use the "Name" element to do this.
wb.Document.All.q.Value := "site:autohotkey.com tutorial"
You can also set a form to a variable:
Website = site:autohotkey.com tutorial wb.Document.All.q.Value := Website
You've just manipulated your first DOM object! Let's go over what we did just now.
This line of code probably doesn't mean anything to you at the moment so let's break it down to something that we all can understand.
Think of "wb" in our code as the top floor or roof of the building you happen to be on. You however want to do something in the basement since it's too wet and rainy to do anything on the roof. However to get to the basement you must travel through all the other floors between.
"Document" refers the the room directly below you in this example. The Document object holds EVERYTHING on any web page you see. But you must specify where your looking on the Document.
"All" specifies all the items including forms, tables, buttons, ect that you can control.
What is "q"??? Q happens to be the name of the search form. To figure out what the name of anything on a web page you should use a tool such as "Ahk Web Recorder." It's a handy tool that shows the information of any IE window you hover the mouse cursor over.
Now that we have the address of the element we want to work with "Value" is a method that alows you to change the content within.
Everything to the right side of the ":=" is what you want placed within the value you have selected. In this case we are going to search through the website "autohotkey.com" using the "site:" tool that is built into the Google search engine. That way Google will only search through a single website instead of browsing the entire WWW.
Now let's click the "Search" button.
wb.Document.All.btnG.Click()
Here we use the "Click()" method. "Value" gives us access to data held within a form whereas "Click" activates a button.
So far we have covered: how to get a(n) existing or created IE handle, sleep till a page has loaded, set a form, and click a button. Here's what your code SHOULD look like. Minus the functions:
wb := IEGet("Google") ;IE instance already open and tab named google exists wb.Document.All.q.Value := "site:autohotkey.com tutorial" wb.Document.All.btnG.Click() IELoad(wb) ;or wb := ComObjCreate("InternetExplorer.Application") ;create a IE instance wb.Visible := True wb.Navigate("Google.com") IELoad(wb) wb.Document.All.q.Value := "site:autohotkey.com tutorial" wb.Document.All.btnG.Click() IELoad(wb)
This guide is a work in progress I will continue to add more material as soon as I get more time and after editing the work I have done so far.
Please keep posts on this page to either requests for explanations or pointing out errors I have made here.
Thanks for reading.
Other Resources:
For a more advanced tutorial on this subject please see jethrow's tutorial at: Basic Webpage Controls with JavaScript / COM This is what I first started out with.
EDITS by jethrow:
- changed Pwb to wb
- changed _L to v1.1+