While AutoHotkey Directly Supports Most Windows Features, the Flexibility of the ComObjCreate() Function Adds More Useful Capabilities—Especially for Capturing Web Data, Sending E-mail, and Reading Text Out-Loud
A number of my scripts use the ComObjCreate() function in various forms. Most of them I copied from the AutoHotkey Forums and modified for my own purposes. In this blog, I highlight the ComObjCreate() applications I use most, then offer a list of other forms of the function you may find useful.
How I Use ComObjCreate()
While AutoHotkey supports many of these features in one form or another, directly accessing the COM (Component Object Model) might provide a solution you can get by no other method. I use the ComObjCreate() function in three ways:
- Collect data from Web pages (ComObjCreate(“WinHttp.WinHttpRequest.5.1”)).
- Send e-mail directly from an AutoHotkey script (ComObjCreate(“CDO.Message”))—no mail program required.
- Use the computer voice to read text (ComObjCreate(“SAPI.SpVoice”)).
While I haven’t found much additional information about the ComObjCreate() function posted on the new AutoHotkey forum, the old forum contains a useful COM Object reference list. You don’t need to know how they work—just how to use them.
The ComObjCreate(“WinHttp.WinHttpRequest.5.1”) Function for Downloading Web Page Code
AutoHotkey includes the UrlDownloadToFile command for downloading the Web page HTML code to a file. In the same documentation, you’ll find both ComObjCreate(“WinHttp.WinHttpRequest.5.1”) for downloading directly to a variable and ComObjCreate(“Msxml2.XMLHTTP”) for downloading in the background without forcing the script to wait (asynchronous). I’ve used the first form extensively employing Regular Expressions (RegEx) to scrape data from Web pages for specific purposes.
See the following examples for a few of the applications:
Using Regular Expressions (RegEx) the IPFind.ahk script parses IP addresses found in any selected block of text then looks them up one by one on a Web page—again using another RegEx to extract and display the details of the locations. Find more information on IPFind.ahk in Chapter Eight of the book A Beginner’s Guide to Using Regular Expressions (RegEx) in AutoHotkey.
- The SynonymLookup.ahk script accesses a thesaurus Web page and downloads replacement synonyms for the selected word for insertion into an AutoHotkey menu. Find more information in the blog, “Build Your Own Dream Thesaurus Word Replacement Tool.”
- This e-mail sending technique bypasses any e-mail program. As a demonstration, the EmailSend.ahk script offers a format for sending a daily horoscope to yourself (or anyone else). It uses Regular Expressions (RegEx) to parse the horoscope from the downloaded Web page code. See “Quick and Dirty Web Data Extraction Script.” The script also uses the e-mail ComObjCreate() function discussed in the next section.
If you have any data that you would like to regularly extract from the Web, then this is the AutoHotkey trick for you.
Sending E-mail Directly from an AutoHotkey Script (ComObjCreate(“CDO.Message”))
The EmailSend.ahk script acts as a template for sending an e-mail directly from an AutoHotkey script. Since you must use your own e-mail provider and credentials, you need to modify the sample script before it will work for you. It uses the ComObjCreate(“CDO.Message”) function to connect to the e-mail server and send a formatted message.
I found this function on the old forum (“CDO Com Email Delivery“) and reposted this script on the current AutoHotkey forum (“Send Email Directly from AutoHotkey“). The comments from other users may offer more insight.
Make Your Computer Talk with ComObjCreate(“SAPI.SpVoice”)
I’ve employed ComObjCreate(“SAPI.SpVoice”) to activate the Windows speaking voice for reading text—primarily in multimedia applications.
ComObjCreate("SAPI.SpVoice") .Speak("U IS FOR UNICORN . u , , n , , i , , c , , o , , r , , n , , unicorn")
I’ve discussed this technique in a number of places such as “Searching Data Files and Other Scripting Ploys with Emojis.” Plus, you can find more information in:
- Chapter Six, “A Multimedia Greeting Card (Progress/Splash Image, ComObjCreate())” and Chapter Thirty-two, “An Easier Way to Get Your Computer to Talk to You (ComObjectCreate)” of AutoHotkey Applications.
- Chapter Thirteen, “AutoHotkey Scan Codes, Speech, Sound, and Splash Images in Children’s Apps” in the AutoHotkey Hotkey Techniques book discusses similar techniques.
- Chapter 6.1.1 “Entertaining and Educating Toddlers with a Talking Keyboard” and Chapter 6.1.2 “More AutoHotkey Entertainment and Education” in my book Jack’s Motley Assortment of AutoHotkey Tips offer more insights.
The three examples of ComObjCreate() highlighted in this blog show how I’ve used the function in a number of my scripts but I’ve found a list of even more techniques (so far, unexplored by me).
Jethrow’s COM Object List
I copied this list from the post in the archived AutoHotkey forum. I’ve only tested the three discussed above. Some people might find that explanations of these forms of the function offer too much information. I tend to agree. When I get this far into the weeds, I tend to look for simpler solutions. Although…I’m tempted to explore the possibilities—even if I don’t know exactly how they work.
HTML File – Represents an HTML document. It can be used to read, write and interact with HTML.
InternetExplorer Application – Explore Web pages.
ScriptControl – Dynamically execute VBScript or JScript.
Scripting Dictionary – Object that stores data key, item pairs.
Scripting FileSystemObject – Access Files & Folders
Shell Explorer – Embed an Explorer/Browser Control in a Gui (Internet Explorer – Trident Browser)
Speech API: SpVoice – Text-to-Speech (TTS) (*Discussed above).
VBScript RegExp – VBS Regular Expressions (including global match)
Windows Media Player – Play Media Files; Embed WMPlayer Control in a GUI.
This media player function (ComObjCreate(“WMPlayer.OCX”)) may offer a little more flexibility than the parallel AutoHotkey SoundPlay command. I’ve posted a short SoundPlay script called QuikPlay.ahk to demonstrate how easily you can build a quick-and-dirty media player. (Discussed in detail in Chapter Five “A Quick AutoHotkey App for Playing Music (SoundPlay and FileSelectFile)” of the book AutoHotkey Applications.)
WinHttpRequest – Provides simple HTTP client functionality, allowing much more control than UrlDownloadToFile. (*Discussed above.)
WScript Shell – Various Administration Tasks (many native AHK tasks)
A few more ComObjCreate() functions found a little further down the list:
CDO COM – Email Delivery BAHK_L (*Discussed above.)
Note: AHK_L (AutoHotkey_L) is the current version of AutoHotkey (v1.1)
While I haven’t explored the majority of these special objects, they have engaged my curiosity. If you have a favorite, I would like to hear about it.
Click the Follow button at the top of the sidebar on the right of this page for e-mail notification of new blogs. (If you’re reading this on a tablet or your phone, then you must scroll all the way to the end of the blog—pass any comments—to find the Follow button.)
This post was proofread by Grammarly
(Any other mistakes are all mine.)
(Full disclosure: If you sign up for a free Grammarly account, I get 20¢. I use the spelling/grammar checking service all the time, but, then again, I write a lot more than most people. I recommend Grammarly because it works and it’s free.)