GeekDude's tips and tricks

Helpful script writing tricks and HowTo's
GeekDude
Posts: 739
Joined: 02 Oct 2013, 22:13

GeekDude's tips and tricks

21 Apr 2015, 11:42

I hope these short tricks are helpful to all of you guys, experienced users and newcomers alike!

Add padding to a string with SubStr or Format. Note that with SubStr, you have to have as many characters as you want to pad with written in the code.

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

Var := "abc123"
MsgBox, % Format("{:010}", Var) ; Use Format for prefix padding with zeroes
MsgBox, % SubStr("__________" Var, -9) ; Use SubStr for prefix padding with other characters
Var := "12.03"
MsgBox, % SubStr(Var "0000000000", 1, 10) ; Or for postfix padding with 0 or other characters


Use SubStr to remove separators from the beginning of a string

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

Loop, 9
Out .= ", " A_Index
Out := SubStr(Out, 3)


Use a NumGet on your associative array to get item count (I learned this from Coco who learned it from Lex)

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

Count := NumGet(&Array, 4*A_PtrSize)


Use InStr to see if a string starts with a sub string. (Alternatively, SubStr can be used, but it requires the length of the substring)

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

if (InStr("Monkey Tacos", "Monkey") == 1)
if (SubStr("Monkey Tacos", 1, 6) == "Monkey")


Make a function call itself on script start (Useful for init functions). Note this can also work with classes

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

MyFunction()
{
Static Dummy := MyFunction()
MsgBox, I see you %A_UserName%
}


Do a toggle with only one line.

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

Loop
MsgBox, % Toggle := !Toggle


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


Lock your screen with a simple DllCall

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

DllCall("LockWorkStation")


Use SetTimer to spawn a new pseudo thread (Warning: These are not real threads)

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

SetTimer, Label, -0


Put hotkeys in an include without interrupting the AutoExecute section by wrapping them in if False

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

if False
{
x::MsgBox, Test 1
y::MsgBox, Test 2
}
MsgBox, Test


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

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

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


Define your objects with more JSON-like formatting by putting your expression into a continuation section

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



Do console output (not standard output) with DllCall and file io. Note: this doesn't work when running from SciTE4AHK.

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

DllCall("AllocConsole")
Loop, 10
FileAppend, %A_Index%`n, CONOUT$
; Alternately, FileOpen("CONOUT", "w").Write(A_Index "`n")




See post revision history on GitHub
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: 729
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: 739
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: 729
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: 739
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: 729
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: 1504
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: 739
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: 1504
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: 729
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: 1087
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: 1504
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: 729
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: 131
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: 363
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!
burque505
Posts: 73
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!

Return to “Tutorials”

Who is online

Users browsing this forum: No registered users and 7 guests