• Intro to AutoHotkey HotStrings with AutoHotkey Intermediate AutoHotkey GUIs are Easy with AutoHotkey Intro to DOS & AutoHotkey

AutoHotkey Webinar 10/2021: Creating and Using AutoHotkey Functions

AutoHotkey webinar AutoHotkey functionsHour 1: Intro to Functions

Hour 2: Q&A Styles and helping Attendees

Script Highlight:  Times around the world by jeeswg

The Automators’ Podcast 

#113: 7 reasons to talk to others about your programming
#114: 5 tips how to reduce the amount of personal support you provide
#115: What is an idea worth? How much $ should you pay the “idea person”?

Resources presented in Webinar

What is a Function?

  • A function is similar to a subroutine (Gosub) except that it can accept parameters (inputs) from its caller.
  • In addition, a function may optionally return a value/values to its caller.

66 Built-in AHK Functions https://www.autohotkey.com/docs/Functions.htm#BuiltIn

  • Polythene’s Command functions https://github.com/Paris/AutoHotkey-Scripts/blob/master/Functions.ahk
  • jeeswg commands as functions (AHK v2 functions for v1) https://www.autohotkey.com/boards/viewtopic.php?f=37&t=29689

Some main benefits of Functions

  • They can be used without storing value
  • You can have multiple on one line / nest
  • Scope is limited (both a blessing and a curse)
  • You create functions to “wrap” complex code which greatly simplifies it’s usage
  • Via a DLL call you can leverage TONS of functions outside AutoHotkey!
  • Variadic functions take a “variable” number of parameters

Built-in AutoHotkey functions

  • Using built-in Functions (here are a couple examples)
  • SubStr(String, StartingPos [, Length])
  • Round(Number[,N])
  • Trim()
  • StrSplit(String , [Delimiters, OmitChars, MaxParts])

Highlights:

  • Positional parameters (the order matters they’re not named parameters)
  • Required parameters
  • Optional parameters

Creating your own Functions in AutoHotkey

  • Why you want to create them
  • Defining a Function verse Calling a function
  • Working with SCOPE
  • Global
  • Static
  • byRef

What we’ll cover:

  • Accessing variables outside your AutoHotkey function
  • Passing parameters
  • Setting default values for your parameters
  • Returning value
  • Returning more than 1 value (byRef & Objects/Arrays)
  • Using the #Includes directive and Function Libraries

Biggest things that will trip you up!

  • Scope seems like a design flaw however it is a great benefit. Most noobs (and even seasoned programmers) will sometimes forget they are inside a function and try and access a variable outside the function
  • Forgetting you returned an object and trying to access it as a variable
  • Making something Global and then reusing the variable in something else

Skins & Styles – Dimitri  (I’ll work on a working example to include here)

Dimitri Geertz joined the webinar and demonstrated his work using the skinsUSkin.dll.

You can learn more about it here in the forum post

or this post from CodeProject

I made this video which shows how simple it can be.  You can download the script below

Notify Function that creates simple GUIs in AutoHotkey

The other day I uploaded a function with utilized the Notify() function from gwarble.

I thought I’d covered it in another post but that was in a webinar so I thought I’d make an intro here to show how it can be used and give you a link to download Notify().  If you’d like to use the tool I used in the video to show which icon/images are in the shell32.dll file, you can see the video and get the script here.

Example using Notify Function in AutoHotkey

Here is the example script I worked through using the Notify Function

AutoHotkey Webinar- Converting GoSubs to a Function and Script walk through

Video Hour 1: High-level overview: Hour 1

Script highlight: Encoding text for some security of passwords

Original script with GoSubs

;*******************************************************
x:=0
ToolTip Pre gosub`nThe value is: %X%`nSleep is 3300
sleep, 3300
;**************************************
GoSub Start
GoSub,Middle
GoSub,End
return

;*******************************************************
Start:
x+=50
ToolTip I'm at Start `nValue is %x%`nSleep is 3000
Sleep, 3000
ToolTip
Return
;*******************************************************
Middle:
x+=150
ToolTip Now I'm Middle`nValue is: %x%`nsleeping 3200
Sleep, 3200
ToolTip
Return
;*******************************************************
End:
x+=350
ToolTip And finally at End`nValue is: %x%`nsleeping 4000
Sleep, 4000
ToolTip
Return

Version with a Function

x:=0
x:=Add_Display_and_Sleep(3300,"I'm here `nValue is",x,50) ;CAll that function
x:=Add_Display_and_Sleep(3000,"I'm at Middle `nValue is",x,100) ;CAll that function
x:=Add_Display_and_Sleep(4300,"I'm at End `nValue is",x,200) ;CAll that function
x:=Add_Display_and_Sleep(2300,"I'm at one more `nValue is",x,500) ;CAll that function
return
Add_Display_and_Sleep(Sleep_Time,Message,x,Value_Add){
  x+=Value_Add
  ToolTip %Message% %x%`nSleep is %Sleep_Time%
  Sleep, %Sleep_Time%
  ToolTip
  Return x
}

Script Walk Through

Below is the version Jackie walked through in the webinar which utilized the old “pre COM” approach.  I found there is a a newer version of the Taskbar Notification script which doesn’t use the old COM interface

var=
(
One
Two
three
four
)

;*********Use For loop over Var going line by line*********************
for i, row in Loopobj:=StrSplit(var,"`n","`r") 
{ ;use For loop to parse over line breaks
  
  TaskbarProgress((i/(Loopobj.maxindex()))*100	,"P",WinExist("ahk_class AutoHotkeyGUI")) ;pwb is to IE, N=green, P=Yellow E=Red  ;Taskbar notifier for loop
  
  MsgBox % Row " and percent: " round((i/(Loopobj.maxindex()))*100) "%"
}
return


TaskbarProgress(pct, state="", hwnd=""){
  static tbl, s0:=0, sI:=1, sN:=2, sE:=4, sP:=8
  if !tbl 	{
    COM_Init() ; Create a TaskbarList object with ITaskbarList3 interface:
    if DllCall("ole32\CoCreateInstance", "uint", COM_GUID4String(CLSID,"{56FDF344-FD6D-11d0-958A-006097C9A090}"), "uint", 0, "uint", 21, "uint", COM_GUID4String(IID,"{ea1afb91-9e28-4b86-90e9-9e9f8a5eefaf}"), "uint*", tbl) != 0 		{
      MsgBox 16,, Error creating TaskbarList object. Is this script running on Windows 7? Exiting.
      ExitApp
    }
  }
  if hwnd =
    hwnd := WinExist()
  if pct is not number
    state := pct, pct := ""
  else if (pct = 0 && state="")
    state := 0, pct := ""
  if state in 0,I,N,E,P   ; ITaskbarList3::SetProgressState
    DllCall(NumGet(NumGet(tbl+0)+40), "uint", tbl, "uint", hwnd, "uint", s%state%)
  if pct !=               ; ITaskbarList3::SetProgressValue
    DllCall(NumGet(NumGet(tbl+0)+36), "uint", tbl, "uint", hwnd, "int64", pct*10, "int64", 1000)
}

;********************From ; COM.ahk Standard Library ; by Sean ; http://www.autohotkey.com/forum/topic22923.html***********************************
COM_Init(bUn = ""){
  Static	h
  Return	(bUn&&!h:="")||h==""&&1==(h:=DllCall("ole32\OleInitialize","Uint",0))?DllCall("ole32\OleUninitialize"):0
}

COM_GUID4String(ByRef CLSID, String){
  VarSetCapacity(CLSID,16,0)
  DllCall("ole32\CLSIDFromString", "Uint", &String, "Uint", &CLSID)
  Return	&CLSID
}

 

My updated AutoHotkey Excel function library

Excel with AutoHotkeyOver 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

  • Intro to AutoHotkey HotStrings with AutoHotkey Intermediate AutoHotkey GUIs are Easy with AutoHotkey Intro to DOS & AutoHotkey