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

Find and click image / Send text to it

I recently played with this great AutoHotkey function FindText().  The program is similar to AutoHotkey’s built-in ImageSearch functionality however it works more reliably and does not need a local file/copy of the image.  The name of the function is a bit misleading in that it really just helps you find and click images.

My first video below demonstrates most of the basic functionality of the tool.  The second video shows the usage of the functions that I wrote to help clicking, sending text, etc.

Here is my version of the FindText function which includes the functions I demonstrate in the second video.  If you’d like the hotstrings I used they are below also (mine are in my main AutoHotkey.ahk file)

Find and click image with FindText()

Finding text / Image on a page

Demonstration of custom AutoHotkey functions to find image and click it.

Functions to click /send text after finding image

HotStrings used for typing-aide for FindText

AutoHotkey Webinar- Parsing plain text with AutoHotkey

Video Hour 1: High-level overview:AutoHotkey Webinar

In the first hour of our AutoHotkey webinar we demonstrated Quick Access Popup from our guest speaker Jean Lalonde.  It is an amazingly customizable tool that allows you to have what you want at your fingertips!

Here is an abbreviated list of some of the great features:

  1. Create shortcuts to folders, programs, files, links, Snippets, special folders, etc.
  2. Easily create Hotkeys for above shortcuts
  3. Works with most Open/Save dialog boxes
  4. Integrated with most file managers (Windows Explorer, Directory Opus, Total Commander, etc.)
  5. The tool itself is highly customizable
  6. Have “shared” sections as well as individual ones
  7. Organize your menu into folders & sub-folders
  8. Customize Menu/GUI shortcut with an icon (that makes sense to you)
  9. Available in 10 languages

You can watch a short overview video here or get a deeper dive here.  Jean was kind enough to walk me through his tool in this video.

We then talked about what a “text file” is and frequent sources of them (Export from programs, sever logs, emails, etc).  We also briefly discussed what is File / Character Encoding & why are there frequent problems with it (this is a very deep topic which we could have spent days discussing!)

Also discussed  what a “flat file” is and how they are different than:

  • Multi-row files
  • Relational files
  • XML and JSON files

We then shared some of the frequently used built-in commands and functions for parsing text in AutoHotkey.

Then we got into various custom-built in libraries and functions

Then our guest speaker, Jean, took over walking us through some examples of common problems with AutoHotkey LoopParse command

The library is pretty cool and here are some of the main functions available:

  • CSV2Collection() – Transfer the content of a CSV file to a collection of objects.
  • Collection2CSV() – Transfer the selected fields from a collection of objects to a CSV file.
  • Collection2Fixed() – Transfer the selected fields from a collection of objects to a fixed-width file.
  • Collection2HTML() – Builds an HTML file based on a template where variables are replaced with content.
  • Collection2XML() – Builds an XML file from the content of the collection.
  • Collection2ListView() – Transfer the selected fields from a collection of objects to ListView.
  • ListView2Collection() – Transfer the selected lines of the selected columns of a ListView to a collection of objects.

ObjCSV Examples:  Here are the files Jean worked through.

  • SIMPLE CSV FILE – COMA DELIMITED (TheBeatles-coma.txt)
  • SIMPLE CSV FILE – TAB DELIMITED – ISSUE #1 (TheBeatles-tab.txt)
  • MULTI-LINE CSV FILE – COMA DELIMITED – ISSUE #2 (TheBeatles-Lyrics.txt)

Jean then gave us a review of how to use his ObjCSV library

  • Example using ObjCSV_CSV2Collection
  • SIMPLE CSV FILE – COMA DELIMITED (TheBeatles-coma.txt)
  • SIMPLE CSV FILE – TAB DELIMITED – ISSUE #1 SOLVED (TheBeatles-tab.txt)
  • MULTI-LINE CSV FILE – COMA DELIMITED – ISSUE #2 SOLVED (TheBeatles-Lyrics.txt)
  • PROGRESS BAR WITH LARGE CSV FILE – SEMI-COLON DELIMITED (LargeFile-semicolon.txt)
  • CUSTOM HEADER – SIMPLE CSV FILE – COMA DELIMITED (TheBeatles-coma.txt)
  • GUI Example using Collection2ListView
  • SAVING WITH DIFFERENT DELIMITER using ObjCSV_Collection2CSV
  • EXPORT HTML Example using ObjCSV_Collection2HTML

Wrapping up the first section we then saw a peak at CSVBuddy

CSVBuddy Examples

  • Reading large file with auto-detection
  • Editing field names, re-order or select fields
  • Saving options
  • Saving file with multi-line content converted to single-line (Excel-ready)
  • Reverting the single-line file to multi-line (for example, after manipulation in Excel)

Video Hour 2: Q&A:

In the second hour we discussed various issues working with text data as well as took a deeper look into Quick Access Popup.

Near the end of the time Capn Odin shared a Trie script he’s been working on.  You can check out the script on the forum

Use Variables/Parameters to pass Methods or Properties in COM w/AutoHotkey

pass Methods or Properties in COMI had wanted to be able to make a function where I pass Methods or Properties in COM.  (this makes it much more flexible and eases updating / maintaining of code.)  Unfortunately it was not as straight-forward as I’d hoped.

Thankfully maestrith, Author of AutoHotkey Studio, had already solved this problem and shared the solution with me!  The code which shows how to pass Methods or Properties in COM is listed below.  While in the video I demonstrate connecting to IE and perform web scraping, the solution is equally applicable to any COM object like Excel, etc.

 

Demonstration Video to pass Methods or Properties in COM

Passing methods or properties as a variable in a COM object