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

AutoHotkey Webinar- Finding presence of text in a large file

AutoHotkey webinarIn this working-session webinar we helped Dimitri with a project he was working on.  In short, he is looking for specific text in a “large” text file.  We discussed various approaches to the solution and created a few example solutions

Script Highlight:  Control panel objects by jNizM

Several things to consider before writing a solution

1.Just how large is/will the text file get?

A.Is the file going to be appended to over time?

2.Do you just need to answer “is the text value in the file” or do you need more information

A.how many occurrences

B.which lines it is on

C.other values related to it (key-value pairs, other fields, etc.)

3.Do you care if the text is found multiple times?

4.How important is speed

5.Would a “fuzzy search” help?

6.Will you be running single or multiple searches?

Possible Approaches for finding text in a large text file

  • Use InString on file read (see below example)
  • Shove it into an Object  (see below example)
  • Use built-in ADO database query by VXE
  • Use SQLite3

Simple InStr solution

Loop,Read, B:\the-automator\Webinar\Scripts\2020-01-Working Session\Dimitri\glbintf3.dat ;loop over data file (line by line)
  if InStr(A_LoopReadLine,"ENP-001213") ;If there is a : in the row, then proceed
    m(A_LoopReadLine,A_Index)

Shoving into an Object

  obj:={} ;Create object for storing data
  Loop,Read, B:\the-automator\Webinar\Scripts\2020-01-Working Session\Dimitri\glbintf3.dat ;loop over data file (line by line)
  {
    if InStr(A_LoopReadLine,":"){ ;If there is a : in the row, then proceed
      RegExMatch(A_LoopReadLine,"(\d+:)(\s+)?(?<Str>\S+.*)",arr_) ;Grab everything to the right of the : and push into Arr_Str
      obj[arr_Str]:=1
    }
  }
;********************Show all in object***********************************
  for k,v in obj
    output.=k "`n"
  MsgBox  %output% 
  
;********************Loop and find your value***********************************
  for k,v in obj{
    if(InStr(k,"ENP-003066"))
      m(k)
  }

 

 

AutoHotkey Webinar- 12/2019 Working Session

webnar 300x200In this webinar we worked on a script from Skrommel’s 1-hour software

Video Hour 1: High-level overview: Hour 1
Video Hour 2: Q&A: Hour 2

Here’s the version that Maestrith (author of AHK Studio) wrote but we tweaked.  Granted, it does less than the original code but it is also 9 lines instead of ~400.   I’d probably add a few more lines to limit what program it works in…

#SingleInstance,Force
~LButton Up:: ;Watch for mouse-up but don't "absorb" it
Clipboard:="" ;Clear clipboard
while(Clipboard) ;Wait until clipboard is blank
  Sleep,10
Send,^c ;Send selection to clipboard
ClipWait,1 ;Wait for up to 1 second for clipboard to have content
if (clipboard)
  MsgBox % Clipboard

Sign up for future webinars here

See past AutoHotkey webinars here

AutoHotkey Webinar- Working session for November 2019

This, and future, webinars are going to be “working sessions” where we help people with their scripts or work-through a script from the forum, etc.  If you have suggestions / problems and want to submit a script for the next webinar, email me at  Joe@the-Automator.com

Video Hour 1: High-level overview: Hour 1
Video Hour 2: Q&A: Hour 2

Here is the script that Maestrith (Chad Wilson, author of AHK Studio) worked out with Dale

#SingleInstance,Force
#Persistent
global Info
SetHotkeys(1)
for a,b in Info:={"c99":"()","c88":"[]","c77":"{{}{}}"}{
  Hotstring(":X*:" a,"HString")
}
return
KeyEvent(a*){
  delay := A_TickCount + 375 ;wait this time in ms to cap letter, lower number = smaller wait
  capped := false
  key := SubStr(A_ThisHotkey,2,1)				;hotkey is ~a etc, so remove the ~
  while(GetKeyState(key,"P")) {
    if ( (!capped) AND (a_tickcount > delay)) {
      SendInput,	{BackSpace}{shift Down}%key%{shift Up}
      hotkey,	%key%,nada,on ;assign key to a nil event, so that windows doesn't spam it ie aaaaaaa
      capped := true 			;capped set to true so that script does not repeat
    }
  }
  if (capped)
    hotkey,~%key%,keyEvent,on  ;assign key back to this event
}
HString(){
  static Key	
  String:=SubStr(A_ThisHotkey,InStr(A_ThisHotkey,":",,,2)+1)
  SendInput,% Info[String] "{Left}"
  Input,Key,L1
  SendInput,+{%Key%}
  return
}
return
nada:
return
SetHotkeys(On:=0){
  Alphabet := "abcdefghijklmnopqrstuvwxyz-=,./;'[]\``"
  for a,b in StrSplit(Alphabet){
    Hotkey,~%b%,KeyEvent,% (On?"On":"Off")
  }
  return
}

 

Sign up for future webinars

See past AutoHotkey webinars

 

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
}

 

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