Thursday, January 16, 2014

Another Reason to love AutoHotKey: Internet Explorer Automation

I'm a big fan of AutoHotKey. I believe it's one of those Swiss Army Knife type tools that every developer (who uses Windows, anyway) should master. Heck, I think it's a great way to learn to program, too. But today I accomplished something with AutoHotKey that even I didn't think it could do: make Internet Explorer useful.

Today I learned that IE and AutoHotKey can be best of friends, making it possible to effectively script IE in just a few commands.

The magic that makes it happen is COM. COM is a technology I discover every few years that allows external programs to talk to the guts of Windows based apps. In theory, the same glue that lets AutoHotKey talk to IE, would allow it to talk to Word or Excel.

To get started, you need to have the latest version of AutoHotKey installed (named: AutoHotKey_L). And then you can check out these videos as well as this tutorial.

Here's a bit of code that was inspired by all that.

First, here's a utility function to find a current running instance of IE:

GetIeInstance()
{
  For pwb in ComObjCreate( "Shell.Application" ).Windows
  {
    If InStr(pwb.FullName, "iexplore.exe" )
    {
      Return pwb
    }
  }
  Return 0
}

Now, I can get that instance and operate on it:

 ie := GetIeInstance()
 MsgBox, % ie.readyState   ; Is the page loading?
 ie.Visible := false       ; Hide the browser
 ie.Visible := true        ; Show it again
 ie.FullScreen := true     ; Go Full screen
 ie.Navigate("http://www.blogbyben.com")   ; Go somewhere

Interesting, but not really anything to get too excited about, right? Now, watch this:

  firstInput := ie.document.getElementsByTagName("input")[0]            ; Access 'document' DOM element
  firstInput.focus()                                                    ; Grant the element focus
  MsgBox, % firstInput.name                                             ; Print the name

See what I did there? COM allows trivial access to the document section of the browser's DOM, and from there, I can traverse it like I would in JavaScript. This is huge, as it lets me interact with a web page in a remarkably clean and reliable way. If I set ie.Visible to false, I could do all this work without the user ever knowing about it.

And don't forget, along with access to the browser, AutoHotKey give you access to the rest of the OS. For example, the code below grabs the user's preferred home page from the registry, and opens up a browser full screen and points to it:

  RegRead, start, HKEY_CURRENT_USER, Software\Microsoft\Internet Explorer\Main, Start Page
  ie :=  ComObjCreate("InternetExplorer.Application")
  ie.Visible := True
  ie.Navigate(start)
  ie.FullScreen := true

You can find docs for the object model here.

I can't believe I'm excited to be working with IE. I suppose there's a first for everything.

1 comment:

  1. I want a script that allows me to launch an IE window, create 6 tabs to various websites I use daily, then launch a 2nd IE window with 3 more (different) tabs. I also need to throw some Sleep functions onto some of those tabs as well. Could you tell me how to do this using the functions above. When my company was using Windows XP, I could do this using "Pwb :+ ComObjCreate("InternetExplorer.Application")" followed by several "Pwb.Navigate" functions, & then repeat for the 2nd window. That still seems to work, but now it launches IE without any plugins (ie Java plugin), & my work uses Java for certain things so this doesn't work for me. I found another site that recommended using "Run, iexplore.exe Pwb := IEGet("pagename") pwb.navigate2("page",2048) etc etc" but then I get an "Error: Call to nonexistent function".on IEGet. I just want to find something that works, so please tell me you can help!!! BTW, my computer is running 64-bit Windows 7, & from what I understand there's a 32-bit IE & a 64-bit IE, one of which may not utilize the plugins, so I'm not sure if this is the problem I was having with my first variation of the script.

    ReplyDelete