AutoHotkey Webinar- Working with Multiple Scripts & Sharing data / variables between them

In our first hour of today’s  AutoHotkey webinar we talked about working with multiple scripts in AutoHotkey.  How you can break out functions into separate files and leverage the library.

In the Second Hour we did Q&A and discussed other things we typically automate.

The two scripts we highlighted were: Dictionary by Fanatic Guru and iThesauras based of Dictionary by rommmecek.

Working with Multiple Files in AHK

  • Breaking out scripts into separate files / functions (Here is a page several videos on functions)
  • Using #include
  • Lib folder
  • Sharing data between scripts via:
  • Passing via command line parameters
  • OnMessage
  • COM object via Windows Temp Environment variables
  • COM object via custom object
  • Consolidating scripts from multiple files into one

Libraries and #Includes

Libraries

  • A script may call a function in an external file without having to use #Include. For this to work, a file of the same name as the function must exist in one of the following library directories:
  • Local library: %A_ScriptDir%\Lib\
  • User library: %A_MyDocuments%\AutoHotkey\Lib\
  • Standard library: path-to-the-currently-running-AutoHotkey.exe\Lib\

Order of search

  1. Original file
  2. Local Library
  3. User Library
  4. Standard Library
  • If a function is not found, but has an underscore in the name, it will search for a library with everything up to the underscore. (E.g. If your function is named “IE_Load” it will search for a file named IE.ahk and load it)

  Benefits of using Library: 1) Code maintenance, 2) Fewer files to keep track of, etc.  3) Easier to read code

  Negatives of using Library: Sharing code can be a bit more complex

Sharing data between scripts: Command line Parameters

AutoHotkey.exe [Switches] [Script Filename] [Script Parameters]

CompiledScript.exe [Switches] [Script Parameters]

Script Parameters:

  • The string(s) you want to pass into the script, with each separated from the next by a space.
  • Any parameter that contains spaces should be enclosed in quotation marks.
  • The script sees incoming parameters as the variables %1%, %2%, and so on.
  • In addition, %0% contains the number of parameters passed (0 if none). However, these variables cannot be referenced directly in an expression because they would be seen as numbers rather than variables.
  • In the receiver, use Param2 = %2% ;Note NO colon

Pros:  Simple, reliable

Cons: Only works when starting up, One-direction, Only passes strings/variables

Sharing data between scripts: OnMessage

Pass String between scripts with OnMessage

Pros: Simple to Use

Cons: Code is somewhat complex, A bit resource intensive, Pass single variable

Sharing data between scripts: Temp Environment Variable

Create a Temporary Environment Variable and stores it, until reboot, in Registery under: HKCU\VolatileEnvironment

Pros: Easy to use, Variable is Accessible after “sender” exits, Fast, Can use objects / dot-notation

Cons: Pass variables/strings, Character limitation

Sharing data between scripts: Custom COM Object

Registers a unique CLSID (Computer Licence Security ID) in registry  (A CLSID is a globally unique identifier that identifies a COM class object. If your server or container allows linking to its embedded objects, you need to register a CLSID for each supported class of objects.)

Pros:Can pass objects, Functions, Variables,Strings, Bi-directional, Super Fast

Cons: Code is more complex, Can’t use FOR loops to iterate over Objects, Can slow some when passing large data

Consolidating Multiple Scripts into One

  • AutoExec section – (Top of script until first: Return, Exit, hotkey/hotstring label)
  • Hotkeys – Conflicting Hotkeys prevents script from running (Make them context sensitive?)
  • Hotstrings – First Hotstring takes presedent
  • Re-using: Variables , Functions, Labels, Classes cause errors
  • Duplicate code – Often bring in things multiple times
  • Reasons why prefer one over the other
  • Multiple scripts-
    • Sharing scripts is easier
    • Troubleshooting can be easier
  • One Script-
    • One file to edit
    • One script in system tray (Can use #Notrayicon in multiple scritps to help negate this)
    • Ease sharing of code

Web Scraping with AutoHotkey 109a- Triggering an EventListener on a page


You ever plugging along on web scraping a page and have a problem with an element (drop down, edit field, radio button, etc) not updating?  Chances are the page has an EventListener watching that element for a specific Event type.  We used to be able to reliably “click” an element or send .fireEvent(“onchange”) / .fireEvent(“onclick”) howe

ver more and more pages are using this newer approach where they build an Event Listener and monitor for events on a given element.

If you’re a non-coder like me, this was very problematic to deal with as the EventListener is located in a different place in the DOM.  In the below video I walk through how to spot the problem and offer up a couple of solutions (like Visual Event) that should greatly help. The second video below demonstrates using my updated AutoHotkey syntax writer.

Triggering an EventListener on a page

Web Scraping with AutoHotkey 109a- Triggering an EventListener on a page

Updated AutoHotkey Syntax Writer

Here I demonstrate using my updated AutoHotkey Syntax writer to provide the needed information for the Events.

Web Scraping with AutoHotkey 109b- AHK syntax writer dealing with Events

AutoHotkey Webinar- Automating MS Outlook and COM

In our first hour of today’s  AutoHotkey webinar we talked about various ways you can automate MS Outlook.  We covered the below scripts:

  1. Creating an HTML email (Also see this video)
  2. Performing mail-merge on the above HTML email
  3. Integrating a mail-merge from an active Excel file  (Also see this video)
  4. Looping over emails in a folder
  5. Creating appointments in Outlook
  6. Creating an address book from looping over emails in Outlook

The second hour dove a bit deeper into using Outlook and Excel.  We also talked through a few user’s questions.

Below is the information that was reviewed in the PowerPoint deck we reviewed:

Script Highlight:

Sift by FanaticGuru uses Regular Expressions and nGrams* to fuzzy match strings

*n-gram is a contiguous sequence of n items

COMComponent Object Model

  • What is COM?
  • Some of the programs COM integrates with:

Creating an HTML email in Outlook with AutoHotkey

  1. Connect to Outlook or Create a new one (but may not deploy right then)
  2. Create a New Mail Item object
  3. Provide values for properties
    1. Specify Account Sending from
    2. Type of email (HTML, RTF, Text)  (Search here for Outlook Constants)
    3. Identify Recipients: To, CC, BBC
    4. Subject line
    5. HTML Body
    6. Attachments
    7. Delivery time (Fuse)
    8. Request Delivery Reciept
    9. Importance
    10. Display / Send
  4. Convert above email to mail-merge with names & email addresses

Resources

AHK Forum:

MSDN resources:

Outlook examples on the-Automator

Stack Overflow

AutoHotkey webinar on COM & Excel