GeekDude's Tips, Tricks, and Standalones

Put simple Tips and Tricks that are not entire Tutorials in this forum
GeekDude
Posts: 816
Joined: 02 Oct 2013, 22:13

GeekDude's Tips, Tricks, and Standalones

21 Apr 2015, 11:42

GeekDude's Tips, Tricks, and Standalones

This is intended to be a useful reference for any AutoHotkey scriptwriter regardless of their experience. If you find any of the examples to be confusing please let me know so I can update them for clarity.


Table of Contents

  • String Manipulation
  • Objects
  • Logic
  • DllCalls
  • Writing Libraries
  • Windows
  • Threading
  • GUIs
  • Regular Expressions


String manipulation

Repeat a string

Code: [Select all] [Download] GeSHi © Codebox Plus

StrRepeat(String, Times)
{
return StrReplace(Format("{:0" Times "}", 0), "0", String)
}


Pad a string to a given length

Code: [Select all] [Download] GeSHi © Codebox Plus

Var := "abc123"

; Format can be used to left-pad with zeros
MsgBox, % Format("{:010}", Var)

; SubStr can be used for prefix padding or postfix padding with any character.
; However, if the string is already larger than the size you want to pad to,
; the string will be truncated.
MsgBox, % SubStr("-=-=-=-=-=" Var, 1-10)
MsgBox, % SubStr(Var "=-=-=-=-=-", 1, 10)


Remove duplicate delimiters when compiling a list in a loop WITHOUT using an if statement

Code: [Select all] [Download] GeSHi © Codebox Plus

Loop, 9
List .= ", " A_Index

; SubStr can be used to remove a single delimiter
MsgBox, % SubStr(List, 3)

; LTrim can be used to remove many delimiters
MsgBox, % LTrim(List, ", ")


Check if a string starts with another string.

Code: [Select all] [Download] GeSHi © Codebox Plus

; Using InStr
if (InStr("Monkey Tacos", "Monkey") == 1)

; Using SubStr (if you know the length of the other string)
if (SubStr("Monkey Tacos", 1, 6) == "Monkey")

; Using Regular Expressions
if ("Monkey Tacos" ~= "^Monkey")



Objects

Use standard JSON format when defining your objects by placing the definition into a continuation section.

Code: [Select all] [Expand] [Download] GeSHi © Codebox Plus



Retrieve the item count from an associative array. Note that AutoHotkey v1.1.29 makes this unnecessary.


Logic

Do a toggle with only one line.

Code: [Select all] [Download] GeSHi © Codebox Plus

Loop
MsgBox, % Toggle := !Toggle



DllCalls

Lock your screen with a simple DllCall

Code: [Select all] [Download] GeSHi © Codebox Plus

DllCall("LockWorkStation")


Output to a console window Notes:
  • This doesn't work when running from SciTE4AHK.
  • This is not the same as using the standard output, so output done in this way will not appear if you run your script from the command prompt.

Code: [Select all] [Expand] [Download] GeSHi © Codebox Plus




Writing Libraries

When writing a library that has code which must be run before any of your functions can be used, you can create an initialization function that runs itself automatically when the script starts. This eliminates any need for the library's users to put your code into their auto-execute section.

Code: [Select all] [Download] GeSHi © Codebox Plus

Initialize()
{
Static Dummy := Initialize()
MsgBox, This init function has been called automatically
}


If your library defines hotkeys, it can break other scripts that include it in their auto-execute section. You can avoid this by wrapping your hotkey definitions in if False.

Code: [Select all] [Download] GeSHi © Codebox Plus

if False
{
x::MsgBox, You hit x
y::MsgBox, You hit y
}

MsgBox, Auto-execution not interrupted



Windows

Change the default script template by modifying C:\Windows\ShellNew\Template.ahk

Code: [Select all] [Download] GeSHi © Codebox Plus

Run, *RunAs notepad.exe C:\Windows\ShellNew\Template.ahk


Start your scripts on login by placing shortcuts (or the actual scripts) into the Startup folder.


Threading

:!: WARNING :!:

These tricks DO NOT add real multithreading to AutoHotkey. They are bound by the limitations of AutoHotkey's green thread implementation and do not work around those restrictions in any way. If you would like to use real multithreading with AutoHotkey look into AutoHotkey_H or multiprogramming (multiple programs/scripts interacting with eachother).

:!: WARNING :!:

Create a new thread context using SetTimer with a small negative period.

One example of where this is useful is if you have a message handler from OnMessage that you have to do a lot of processing in, but that the result of doesn't actually change what you return to the sender of the message. By using SetTimer to schedule a new thread context to be created, you can be responsive and return immediately, then do your processing afterward.

Code: [Select all] [Expand] [Download] GeSHi © Codebox Plus




GUIs

Create a menu bar for your GUI using a well formatted object instead of a long list of menu commands.

Code: [Select all] [Expand] [Download] GeSHi © Codebox Plus




Regular Expressions

Find, and optionally replace, all matches of regular expression efficiently using a custom enumerator.

Code: [Select all] [Expand] [Download] GeSHi © Codebox Plus





Revision History

You can view old versions of this post from the GitHub link below.

https://gist.github.com/G33kDude/1601bd ... /revisions
Last edited by GeekDude on 30 Aug 2015, 22:19, edited 10 times in total.
User avatar
smorgasbord
Posts: 490
Joined: 30 Sep 2013, 09:34

Re: GeekDude's tips and tricks

21 Apr 2015, 12:06

MsgBox, % StrSplit(FileOpen("No of files in 1 line.txt", "r").Read() "`n", "`n").MaxIndex()-1

by geekdude
John ... you working ?
vasili111
Posts: 747
Joined: 21 Jan 2014, 02:04
Location: Georgia

Re: GeekDude's tips and tricks

21 Apr 2015, 12:11

I am experimenting with thread creation. Why it is not working?

Code: [Select all] [Download] (Script.ahk)GeSHi © Codebox Plus

b := 1
SetTimer, label1, -0

return

label1:
Loop
{
b := b + 1
ToolTip, %b%
}
DRAKON-AutoHotkey: Visual programming for AutoHotkey.
GeekDude
Posts: 816
Joined: 02 Oct 2013, 22:13

Re: GeekDude's tips and tricks

21 Apr 2015, 12:15

smorgasbord wrote:MsgBox, % StrSplit(FileOpen("No of files in 1 line.txt", "r").Read() "`n", "`n").MaxIndex()-1

For reference, this gets the number of newlines in a file. It's more of an exercise in compact code than a practical example.

vasili111 wrote:I am experimenting with thread creation. Why it is not working?


Persistence, I assume. You'll need #Persistent at the top of your script

#Persistent Documentation wrote:If this directive is present anywhere in the script, that script will stay running after the auto-execute section (top part of the script) completes. This is useful in cases where a script contains timers and/or custom menu items but not hotkeys, hotstrings, or any use of OnMessage() or Gui.
vasili111
Posts: 747
Joined: 21 Jan 2014, 02:04
Location: Georgia

Re: GeekDude's tips and tricks

21 Apr 2015, 12:21

Thanks!
Why this is not working? I am getting in tooltip 1 after ~1 sec tooltip disappears. It should count in tooltip, right?

Code: [Select all] [Expand] [Download] (Script.ahk)GeSHi © Codebox Plus

DRAKON-AutoHotkey: Visual programming for AutoHotkey.
GeekDude
Posts: 816
Joined: 02 Oct 2013, 22:13

Re: GeekDude's tips and tricks

21 Apr 2015, 12:31

That's a nuance of how threads in AutoHotkey work. AHK is single threaded, so what we get a "pseudo threads". AHK usually makes these work more or less how you would expect, but in this case they aren't. Instead of switching back and forth between threads, AHK waits for the new thread to end before going back to the old thread. Because of that, this technique cannot be used in this way.
vasili111
Posts: 747
Joined: 21 Jan 2014, 02:04
Location: Georgia

Re: GeekDude's tips and tricks

21 Apr 2015, 12:36

GeekDude
So as I understand it is impossible to run more than one thread in the same time. In any given time only one thread will be running. Am I right?
DRAKON-AutoHotkey: Visual programming for AutoHotkey.
HotKeyIt
Posts: 1643
Joined: 29 Sep 2013, 18:35
Contact:

Re: GeekDude's tips and tricks

21 Apr 2015, 12:47

You would need AutoHotkey.dll for real multi-threading.
Following requires AutoHotkey_H (AutoHotkey[Mini].dll is included in Resources so all you need is AutoHotkey.exe).

Code: [Select all] [Download] (Script.ahk)GeSHi © Codebox Plus

AhkMini("Alias(b," getvar(b:=1) ")`nLoop`nb++") ; use AhkThread to load AutoHotkey.dll instead of AutoHotkeyMini.dll
Loop
ToolTip, %b%
Esc::ExitApp
GeekDude
Posts: 816
Joined: 02 Oct 2013, 22:13

Re: GeekDude's tips and tricks

21 Apr 2015, 12:49

Additional real threads, that is. Hey HotKeyIt, how do I handle code errors from calling the COM interface on your dll? I'm using ahktextdll, then addScript and finally ahkFunction. How do I handle the error from passing malformed code into addScript, or if something throws an exception during runtime?
HotKeyIt
Posts: 1643
Joined: 29 Sep 2013, 18:35
Contact:

Re: GeekDude's tips and tricks

21 Apr 2015, 13:00

addScript will return a pointer to first line of created script.
When script could not be added 0 will be returned and error from dll will be shown as in AutoHotkey.exe, e.g. MsgBox % thread.addScript("This code will error")
This will show error at runtime:

Code: [Select all] [Download] GeSHi © Codebox Plus

(thread:=ComObjCreate("AutoHotkey.Script")).ahkdll()
MsgBox % thread.addScript("ie:=ComObjCreate(""InternetExplorer.Application"")`nie.nav()`nie.Quit()",2)
vasili111
Posts: 747
Joined: 21 Jan 2014, 02:04
Location: Georgia

Re: GeekDude's tips and tricks

21 Apr 2015, 13:12

HotKeyIt
To be able to use AutoHotkey_H as default AutoHotkey I should replace my installed AutoHotkey.exe with AutoHotkey.exe from your link?
Can you please give me link to AutoHotkey_H documentation?
Last edited by vasili111 on 21 Apr 2015, 13:14, edited 1 time in total.
DRAKON-AutoHotkey: Visual programming for AutoHotkey.
tmplinshi
Posts: 1147
Joined: 01 Oct 2013, 14:57

Re: GeekDude's tips and tricks

21 Apr 2015, 13:13

Thank you, I like this one :D
Count := NumGet(&Array, 4*A_PtrSize)
HotKeyIt
Posts: 1643
Joined: 29 Sep 2013, 18:35
Contact:

Re: GeekDude's tips and tricks

21 Apr 2015, 14:17

vasili111 wrote:HotKeyIt
To be able to use AutoHotkey_H as default AutoHotkey I should replace my installed AutoHotkey.exe with AutoHotkey.exe from your link?
Can you please give me link to AutoHotkey_H documentation?

Yes, simply replace the AutoHotkey.exe.

The old documentation is here, but it is not updated anymore: http://hotkeyit.ahk4.net/files/AutoHotkey-txt.html
The new v2 documentation (still not complete) is here: http://hotkeyit.github.io/v2/docs/AutoHotkey.htm
Most of the v2 features are available in v1 too.

The docs are also included in download pack
- v1: https://github.com/HotKeyIt/ahkdll-v1-r ... master.zip
- v2: https://github.com/HotKeyIt/ahkdll-v2-r ... master.zip


Btw. in AutoHotkey_H there is a Count method: MsgBox % {1:1,2:2,test:3,ahk:4}.Count()
vasili111
Posts: 747
Joined: 21 Jan 2014, 02:04
Location: Georgia

Re: GeekDude's tips and tricks

21 Apr 2015, 15:25

GeekDude
HotKeyIt

Thank you!
DRAKON-AutoHotkey: Visual programming for AutoHotkey.
User avatar
Coldblackice
Posts: 16
Joined: 13 Nov 2013, 14:39

Re: GeekDude's tips and tricks

30 Aug 2015, 22:18

Cool thread, thanks!
User avatar
lmstearn
Posts: 149
Joined: 11 Aug 2016, 02:32
GitHub: lmstearn

Re: GeekDude's tips and tricks

05 Mar 2017, 10:08

Bump- (In the hope there are more to add). Thanks for the post! :)
:arrow: itros "ylbbub eht tuO kaerB" a ni kcuts m'I pleH
User avatar
SnowFlake
Posts: 368
Joined: 28 Apr 2015, 05:41
Google: floowsnaake
GitHub: floowsnaake
Contact:

Re: GeekDude's tips and tricks

05 Mar 2017, 14:00

"GeekDude's tips and tricks" what a lovely title xD also great work!
:yawn:
burque505
Posts: 437
Joined: 22 Jan 2017, 19:37

Re: GeekDude's tips and tricks

12 Oct 2017, 10:41

@GeekDude, thank you.
The tip on pseudo-threads is a life-saver! Instead of :headwall: in both LinqPad and UiPath, where I am experimenting with AutoHotkey.Interop, I can now avoid the following error:

Error in #include file "Whatever_file.ahk":
0x800101D - An outgoing call cannot be made since the application is dispatching an input-synchronous call


By just adding


to the top of my code in my scripts that threw that error, and just putting my entire code below the label, I can now create COM objects without calling another script.

By the way,
I have tried Vasili111's code:



in both AHK_H and AHK_L, and it works verbatim for me. Tooltip keeps counting until I shut down the script from the tray. Win7 x64

:bravo:

Thank you!
User avatar
derz00
Posts: 486
Joined: 02 Feb 2016, 17:54
GitHub: derz00
Location: Middle of the round cube

Re: GeekDude's tips and tricks

23 Oct 2017, 10:59

@GeekDude :wave:

Why doesn't this work?

Code: [Select all] [Download] GeSHi © Codebox Plus

MyFunction()
{
Static Dummy := MyFunction()
MsgBox, I see you %A_UserName%
wow:="oops " A_Now
return wow
}
MsgBox % Dummy


I'm just misunderstanding something, but I just can't see what. The second MsgBox is blank.

Any idea why this doesn't work in AHK v2?

Code: [Select all] [Expand] [Download] GeSHi © Codebox Plus

try it and see
...

Return to “Tips and Tricks”

Who is online

Users browsing this forum: No registered users and 1 guest