My updated AutoHotkey Excel function library

Over the years I’ve done a lot in Excel.  Often I leverage my Excel function Library which is, at best, a work in progress.   I commissioned Maestrith, author of AHK Studio, to review it.   We worked through what did & didn’t work plus optimized a few things.

AutoHotkey Excel Function Library

AutoHotkey webinar: Connect to running scripts from Explorer Context Menu

Video Hour 1:  High Level: Connect to running scripts from Explorer Context Menu

  1. Context menus at work
    1. Quick Access Popup real life example
  2. Pieces of the puzzle (Here is a link to the below files)
    1. Receiver (script or compiled): Receiver.ahk
    2. Messenger (compiled): Messenger.exe
    3. Testing at command line: Test.bat
    4. Setup context menus: RegEdit import InstallContextMenus.reg
    5. Remove context menus: RemoveContextMenus.bat
    6. Questions

Video Hour 2: Coding and Q&A

Script Highlight: Random code generator.ahk by Maestrith (Author of AHK Studio)

  • Generate a list of random characters
  • Control what is in the unique list
  • How many unique “Keys” are generated
#SingleInstance,Force
Random:=[],Dup:=[],Items:=[]
for a,b in StrSplit("0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz") ;put whatever characters you want in here
	Random.Push(b) ;Add each character to Random Array
InputBox,Count,How Many Codes,How Many Codes do you want?
InputBox,Length,How Long For Each Code,How Long?
While(Items.MaxIndex()<Count){ ;Keep iterating until you have enough "keys"
	Out:=""  ;Clear out variable
	while(StrLen(Out)<Length){
		Random,Digit,0,% Random.MaxIndex() ;keep max lenght of random at right level
		Out.=Random[Digit] ;append the Random digit to out.
	}
	if(!Dup[Out])
		Items.Push(Out),Dup[Out]:=1 ;Add key to object
}
for a,b in Items ;Iterate over list
	Total.=b "`n" ;Add to total with new line

msgbox % Clipboard:=Total

Automating Chrome with AutoHotkey: Setting InnerText, OuterHTML & Values

Web Scraping is Amazing!  Being able to automate Chrome with AutoHotkey has been a long-term goal of mine!  In this video I (quickly) walk through a function I wrote to leverage GeekDude’s Chrome.ahk class.  You can get the class here or read more about it on the AutoHotkey forum.

There’s lots more to do but I wanted to share this to help others get started using the above Class.  🙂

Here is the code I demonstrate in the below video where I demonstrate  setting text in Chrome with AutoHotkey.

#Include B:\Progs\AutoHotkey_L\AHK Work\WebPage\Chrome\Examples\Chrome.ahk  ;GeekDude Class ; https://github.com/G33kDude/Chrome.ahk  https://autohotkey.com/boards/viewtopic.php?t=42890
Tab:=Chm_Create_Instance("C:\Users\joe\AppData\Local\Google\Chrome\User Data\Default") ;Create instance using default Profile
Chm_Navigate(Tab,"https://the-automator.com") ;Navigate to a URL
Chm_Set(Tab,Method:="Class",Attribute:="s",Index:=2,Property:="v",Value:="Just a test")
;~ Chm_Set(Tab,Method:="Name",Attribute:="s",Index:=2,Property:="v",Value:="Just another test")
;~ Chm_Set(Tab,Method:="tag",Attribute:="Input",Index:=1,Property:="v",Value:="Just a test")
;~ Chm_Set(Tab,Method:="ID",Attribute:="cat",Index:=2,Property:="v",Value:="11")


;********************Chrome Set Property******* Propterty=  i=innerText v=Value  o=outerHTML****************************
Chm_Set(Tab,Method="Class",Attribute="hfeed",Index=1,Property="i",Value=""){
  ;***********Class*******************
  if (Format("{:L}",Method)="class"){  ;Case-insensitive check to see if method = Class
    if (Format("{:L}",Property)="o") ;If Property="o" then set OuterHTML
      Tab.Evaluate("document.getElementsByClassName('" Attribute "')[" Index-1 "].outerHTML='" Value "'").Value
    Else if (Format("{:L}",Property)="v") ;If Property="v" then set Value
      Tab.Evaluate("document.getElementsByClassName('" Attribute "')[" Index-1 "].value='" Value "'").Value
    Else if (Format("{:L}",Property)="i") ;If Property="i" then set innerText
      Tab.Evaluate("document.getElementsByClassName('" Attribute "')[" Index-1 "].innerText='" Value "'").Value
  } ;***********Tag*******************
  Else if (Format("{:L}",Method)="tag"){ ;Case-insensitive check to see if method = Tag
    if (Format("{:L}",Property)="o") ;If Property="o" then set OuterHTML
      Tab.Evaluate("document.getElementsByTagName('" Attribute "')[" Index-1 "].outerHTML='" Value "'").Value
    Else if (Format("{:L}",Property)="v") ;If Property="v" then set Value
      Tab.Evaluate("document.getElementsByTagName('" Attribute "')[" Index-1 "].value='" Value "'").Value
    Else if (Format("{:L}",Property)="i") ;If Property="i" then set innerText
      Tab.Evaluate("document.getElementsByTagName('" Attribute "')[" Index-1 "].innerText='" Value "'").Value
  } ;***********Name*******************
  Else if (Format("{:L}",Method)="Name"){ ;Case-insensitive check to see if method = Name
    if (Format("{:L}",Property)="o") ;If Property="o" then set OuterHTML
      Tab.Evaluate("document.getElementsByName('" Attribute "')[" Index-1 "].outerHTML='" Value "'").Value
    Else if (Format("{:L}",Property)="v") ;If Property="v" then set Value
      Tab.Evaluate("document.getElementsByName('" Attribute "')[" Index-1 "].value='" Value "'").Value
    Else if (Format("{:L}",Property)="i") ;If Property="i" then set innerText
      Tab.Evaluate("document.getElementsByName('" Attribute "')[" Index-1 "].innerText='" Value "'").Value
  } ;***********ID*******************
  Else if (Format("{:L}",Method)="id"){ ;Case-insensitive check to see if method = ID
    if (Format("{:L}",Property)="o") ;If Property="o" then set OuterHTML
      Tab.Evaluate("document.getElementById('" Attribute "').outerHTML='" Value "'").Value
    Else if (Format("{:L}",Property)="v") ;If Property="v" then set Value
      Tab.Evaluate("document.getElementById('" Attribute "').value='" Value "'").Value
    Else if (Format("{:L}",Property)="i") ;If Property="i" then set innerText
      Tab.Evaluate("document.getElementById('" Attribute "').innerText='" Value "'").Value
  } Else{ 	MsgBox fix Method- Valid values are: Name, Class, Tag, ID (case of text does not matter)
}
}
;********************Create instance and use a path to profile***********************************
Chm_Create_Instance(Profile_Path=""){
 if !(Profile_Path){
 FileCreateDir, ChromeProfile ;Make sure folder exists
 ChromeInst := new Chrome("ChromeProfile") ;Create a new Chrome Instance
 }Else{
 try ChromeInst := new Chrome(Profile_Path) ;Create for profile lookup prfile by putting this in your url in Chrome chrome://version/
 }
 return Tab := ChromeInst.GetTab() ;Connect to Active tab
}

;********************Navigate to page***********************************
;~ Chm_Navigate(Tab,"https://the-automator.com")
Chm_Navigate(Tab,URL){
 Tab.Call("Page.navigate", {"url": URL}) ;Navigate to URL
 Tab.WaitForLoad() ;Wait for page to finish loading
}

 

Here’s the video walking through how to use the function with the above

Chrome.ahk Class to setting text in Chrome with AutoHotkey

 

 

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