A collection of small ahk functions

Post your working scripts, libraries and tools for AHK v1.1 and older
User avatar
Trubbleguy
Posts: 18
Joined: 11 Jul 2014, 03:00

Case (fixes names like o'donnel and McNamra)

21 May 2016, 01:55

I needed a way to make all my artist names correctly Upper/Lower case, in my file renaming and databases, so i put this together, I have used it in my Appointment book prog and others. maybe somebody can also make use of it, There is sample code built into the function header that is ignored when the function is called, but runs if you directly run the function from your editor.
Spoiler
User avatar
jNizM
Posts: 3183
Joined: 30 Sep 2013, 01:33
Contact:

Re: A collection of small ahk functions

23 May 2016, 00:55

boiler wrote:I must be missing something, but how did you create the string "21EC2020-3AEA-4069-A2DD-08002B30309D"? Is there a function that is supposed to convert the "1866374240" to that? I'm sure I'm showing my ignorance on this subject.
It's just a random string I used as example for the function. RSHash creates a small numeric only hash.
MsgBox % RSHash("some_random_string_to_rshash_func")
[AHK] v2.0.5 | [WIN] 11 Pro (Version 22H2) | [GitHub] Profile
User avatar
jNizM
Posts: 3183
Joined: 30 Sep 2013, 01:33
Contact:

Re: A collection of small ahk functions

24 May 2016, 04:33

CRC32 (Cyclic redundancy check)

Source: (Implementation via AutoHotkey & via DllCalls)
CRC32.ahk (GitHub)

example:

Code: Select all

MsgBox % CRC32("The quick brown fox jumps over the lazy dog")
output:

Code: Select all

0x414fa339

Ref:
- Cyclic redundancy check (wiki)
[AHK] v2.0.5 | [WIN] 11 Pro (Version 22H2) | [GitHub] Profile
User avatar
jNizM
Posts: 3183
Joined: 30 Sep 2013, 01:33
Contact:

Re: A collection of small ahk functions

13 Jun 2016, 03:48

GetMacAddress (Get a list of computers MAC address.)

Source:
GetMacAddress.ahk (GitHub)

example:

Code: Select all

for k, v in GetMacAddress()
    MsgBox % v
output:

Code: Select all

01:23:45:67:89:ab

Ref:
- MAC address (wiki)
[AHK] v2.0.5 | [WIN] 11 Pro (Version 22H2) | [GitHub] Profile
User avatar
lmstearn
Posts: 694
Joined: 11 Aug 2016, 02:32
Contact:

Re: A collection of small ahk functions

20 Aug 2016, 09:13

Useful collection!
just me wrote:This is getting unclear. What about a TOC in the first post?
There's a script to do that somewhere, but too large for this category!
:arrow: itros "ylbbub eht tuO kaerB" a ni kcuts m'I pleH
Helgef
Posts: 4709
Joined: 17 Jul 2016, 01:02
Contact:

Re: A collection of small ahk functions

09 Sep 2016, 02:14

jNizM wrote:Minimum
Find the minimum of 2 vars

Code: Select all

min(x, y)
{
    return y ^ ((x ^ y) & -(x < y))
}

MsgBox % min(27, 64)    ; ==> 27
======================================================================================

Maximum
Find the maximum of 2 vars

Code: Select all

max(x, y)
{
    return x ^ ((x ^ y) & -(x < y))
}

MsgBox % max(27, 64)    ; ==> 64
That only works for integers. This works for doubles too

Code: Select all

min(x,y)
{
	return (x<y)*x+(y<=x)*y
}

max(x,y)
{
	return (x>y)*x+(y>=x)*y
}
User avatar
jNizM
Posts: 3183
Joined: 30 Sep 2013, 01:33
Contact:

Re: A collection of small ahk functions

12 Sep 2016, 01:18

HasVal (for Simple Arrays)
Checks if a value exists in an array (similar to HasKey)

FoundPos := HasVal(Haystack, Needle)

Code: Select all

HasVal(haystack, needle) {
    for index, value in haystack
        if (value = needle)
            return index
    if !(IsObject(haystack))
        throw Exception("Bad haystack!", -1, haystack)
    return 0
}
Examples

Code: Select all

arr := ["a", "b", "", "d"]
MsgBox % HasVal(arr, "a") "`n"    ; ==> 1
       . HasVal(arr, "c") "`n"    ; ==> 0
	   . HasVal(arr, "d")         ; ==> 4

Code: Select all

arr := {keyA: "A", keyB: "B"}
MsgBox % HasVal(arr, "B") "`n"    ; ==> keyB
       . HasVal(arr, "C")         ; ==> 0

Code: Select all

arr := {keyA: "A", 1: 0, keyC: "C"}
MsgBox % HasVal(arr, 0)   "`n"    ; ==> 1
       . HasVal(arr, "C") "`n"    ; ==> keyC

Code: Select all

str := "A"
MsgBox % HasVal(str, "A")         ; ==> fails -1
[AHK] v2.0.5 | [WIN] 11 Pro (Version 22H2) | [GitHub] Profile
User avatar
jNizM
Posts: 3183
Joined: 30 Sep 2013, 01:33
Contact:

Re: A collection of small ahk functions

10 Oct 2016, 02:40

GetDnsAddress (Get a list of DNS servers used by the local computer.)

Source:
GetDnsAddress.ahk (GitHub)

example:

Code: Select all

for k, v in GetDnsAddress()
    MsgBox % v
output:

Code: Select all

8.8.8.8
8.8.4.4

Ref:
- Domain Name System (wiki)
[AHK] v2.0.5 | [WIN] 11 Pro (Version 22H2) | [GitHub] Profile
User avatar
JoeWinograd
Posts: 2200
Joined: 10 Feb 2014, 20:00
Location: U.S. Central Time Zone

Re: A collection of small ahk functions

23 Jan 2017, 03:20

Hi jNizM,
Very nice job! Thanks for putting this together. Two minor typos that you may want to fix: in the title, "usefull" should be "useful"; in the TOC's InArr entry, "InSrt" should be "InStr". Regards, Joe
User avatar
jNizM
Posts: 3183
Joined: 30 Sep 2013, 01:33
Contact:

Re: A collection of small ahk functions

23 Jan 2017, 03:30

merci & fixed
[AHK] v2.0.5 | [WIN] 11 Pro (Version 22H2) | [GitHub] Profile
User avatar
jNizM
Posts: 3183
Joined: 30 Sep 2013, 01:33
Contact:

Re: A collection of small ahk functions

27 Jan 2017, 05:54

RefreshPolicy (Causes policy to be applied immediately on the computer like gpupdate does)

Source:
RefreshPolicy.ahk (GitHub)

example:

Code: Select all

RefreshPolicy(1, 1)    ; -> refresh the computer policy with force flag
RefreshPolicy(0, 1)    ; -> refresh the user     policy with force flag

Ref:
- RefreshPolicy function (msdn)
- RefreshPolicyEx function (msdn)
[AHK] v2.0.5 | [WIN] 11 Pro (Version 22H2) | [GitHub] Profile
User avatar
jNizM
Posts: 3183
Joined: 30 Sep 2013, 01:33
Contact:

Re: A collection of small ahk functions

17 Feb 2017, 09:36

UpdateScript
Update running (.ahk) script in realtime (~ 500-1000 ms) if you change something in the script

Code: Select all

UpdateScript()
{
    if ((attrb := DllCall("GetFileAttributes", "str", A_ScriptFullPath)) = 0x20) {
        DllCall("SetFileAttributes", "str", A_ScriptFullPath, "uint", attrb &= ~0x20)
        TrayTip,, % "Script has been updated."
        sleep 1000
        reload
    }
}
Example

Code: Select all

; GLOBAL SETTINGS ===============================================================================================================

#NoEnv
#SingleInstance Force
#Persistent

SetBatchLines -1

; CHECK UPDATE ==================================================================================================================

SetTimer, UpdateScript, 500
return

; SCRIPT ========================================================================================================================

; your script here

; FUNCTIONS =====================================================================================================================

UpdateScript()
{
    if ((attrb := DllCall("GetFileAttributes", "str", A_ScriptFullPath)) = 0x20) {
        DllCall("SetFileAttributes", "str", A_ScriptFullPath, "uint", attrb &= ~0x20)
        TrayTip,, % "Script has been updated."
        sleep 1000
        reload
    }
}

Ref:
Refresh program to detect code change
[AHK] v2.0.5 | [WIN] 11 Pro (Version 22H2) | [GitHub] Profile
User avatar
Relayer
Posts: 160
Joined: 30 Sep 2013, 13:09
Location: Delaware, USA

Re: A collection of small ahk functions

17 Feb 2017, 11:51

jNizM,

So I assume it will not reload a script that had a sub-component change like a library or an included file? Seems that one would need to pass the function an argument containing the names of all used files but that is not easy to keep up to date.

Relayer
User avatar
jNizM
Posts: 3183
Joined: 30 Sep 2013, 01:33
Contact:

Re: A collection of small ahk functions

20 Feb 2017, 08:28

@Relayer,

would be possible with a build-in fuction like A_IncludeFiles who returns an array with all #Include's files.

Maybe something for @lexikos if its possible and he is interested enough ;)
[AHK] v2.0.5 | [WIN] 11 Pro (Version 22H2) | [GitHub] Profile
User avatar
jNizM
Posts: 3183
Joined: 30 Sep 2013, 01:33
Contact:

Re: A collection of small ahk functions

03 Mar 2017, 03:23

WM_DEVICECHANGE
Notifies an application of a change to the hardware configuration of a device or the computer. (Removal devices like USB)

Source:
WM_DEVICECHANGE.ahk (GitHub)

Example
See source

Ref:
WM_DEVICECHANGE message (msdn)
DBT_DEVICEARRIVAL event (msdn)
DBT_DEVICEREMOVECOMPLETE event (msdn)
DEV_BROADCAST_HDR structure (msdn)
DEV_BROADCAST_VOLUME structure (msdn)
[AHK] v2.0.5 | [WIN] 11 Pro (Version 22H2) | [GitHub] Profile
User avatar
jNizM
Posts: 3183
Joined: 30 Sep 2013, 01:33
Contact:

Re: A collection of small ahk functions

22 Aug 2017, 05:36

Base64 (String)
Encode a string to base64 format & decode from base64 format to a string (Unicode / UTF-8)

Source:
base64.ahk (GitHub)

Example
See source

Ref:
Base64 (wiki)
CryptBinaryToString function (msdn)
CryptStringToBinary function (msdn)
[AHK] v2.0.5 | [WIN] 11 Pro (Version 22H2) | [GitHub] Profile
User avatar
jNizM
Posts: 3183
Joined: 30 Sep 2013, 01:33
Contact:

Re: A collection of small ahk functions

29 Nov 2019, 09:58

IsUEFIorBIOS (Detect if Windows was installed in Legacy BIOS or UEFI mode)

Source:

Code: Select all

IsUEFIorBIOS()
{
	static DUMMY_GUID := "{00000000-0000-0000-0000-000000000000}"
	static ERROR_INVALID_FUNCTION := 0x1

	DllCall("GetFirmwareEnvironmentVariable", "str", "", "str", DUMMY_GUID, "ptr", 0, "uint", 0, "uint")
	return (A_LastError = ERROR_INVALID_FUNCTION) ? "Legacy BIOS" : "UEFI Boot Mode"
}
Example:

Code: Select all

MsgBox % IsUEFIorBIOS()
Output:

Code: Select all

UEFI Boot Mode

Ref:
- GetFirmwareEnvironmentVariable function (msdn)
- How distinguish when Windows was installed in Legacy BIOS or UEFI mode using Delphi?
[AHK] v2.0.5 | [WIN] 11 Pro (Version 22H2) | [GitHub] Profile
User avatar
jNizM
Posts: 3183
Joined: 30 Sep 2013, 01:33
Contact:

Re: A collection of small ahk functions

21 Jan 2021, 05:09

ElfHash (ELF Hash Implementation in AutoHotkey)

Source:

Code: Select all

ElfHash(string)
{
	h := 0
	loop, parse, string
	{
		h := (h << 4) + (Asc(A_LoopField))
		if (x := h & 0xF0000000)
			h ^= x >> 24
		h &= ~x
	}
	return Format("{:#x}", h)
}
Example:

Code: Select all

MsgBox % ElfHash("The quick brown fox jumps over the lazy dog")
Output:

Code: Select all

0x4280c57
[AHK] v2.0.5 | [WIN] 11 Pro (Version 22H2) | [GitHub] Profile
mcl
Posts: 357
Joined: 04 May 2018, 16:35

Re: A collection of small ahk functions

24 Jan 2021, 15:44

A few more simple-to-implement hash functions, mostly for educational purposes:
  • Jenkins' one-at-a-time (32-bit)
  • FNV-1a (32/64-bit)
  • Bernstein's DJB2
  • SDBM
Implementations here operate on bytes, not characters.
32-bit functions perform bit-trimming after each step, otherwise it may produce different results.

According to some tests (strchr, SO), FNV-1a has very good properties in terms of collisions, speed and resulting randomness. But, in my opinion, CRC-32 will be a better choice in almost any case.

Code: Select all

Hash_JenkinsOneAtATime(string) {
	lenBytes := StrLen(string) * (A_IsUnicode ? 2 : 1)
	hash := 0
	
	Loop % lenBytes
	{
		char := NumGet(string, A_Index-1, "UChar")
		hash += char       , hash &= 0xFFFFFFFF
		hash += hash << 10 , hash &= 0xFFFFFFFF
		hash ^= hash >> 6  , hash &= 0xFFFFFFFF
	}
	
	hash += hash << 3  , hash &= 0xFFFFFFFF
	hash ^= hash >> 11 , hash &= 0xFFFFFFFF
	hash += hash << 15 , hash &= 0xFFFFFFFF
	
	Return Format("{1:08X}", hash)
}


Hash_FNV1a32(string) {
	lenBytes := StrLen(string) * (A_IsUnicode ? 2 : 1)
	hash := 0x811C9DC5
	
	Loop % lenBytes {
		hash ^= NumGet(string, A_Index-1, "UChar")
		hash *= 0x01000193
		hash &= 0xFFFFFFFF
	}
	
	Return Format("{1:08X}", hash)
}


Hash_FNV1a64(string) {
	lenBytes := StrLen(string) * (A_IsUnicode ? 2 : 1)
	hash := 0xCBF29CE484222325
	
	Loop % lenBytes {
		hash ^= NumGet(string, A_Index-1, "UChar")
		hash *= 0x00000100000001B3
	}
	
	Return Format("{1:016X}", hash)
}


Hash_BernsteinDJB2(string) {
	lenBytes := StrLen(string) * (A_IsUnicode ? 2 : 1)
	hash := 5381
	
	Loop % lenBytes {
		hash := ((hash << 5) + hash) + NumGet(string, A_Index-1, "UChar")
		hash &= 0xFFFFFFFF
	}
	
	Return Format("{1:08X}", hash)
}


Hash_SDBM(string) {
	lenBytes := StrLen(string) * (A_IsUnicode ? 2 : 1)
	hash := 0
	mul  := 65599  ; 65587 works as well
	
	Loop % lenBytes {
		hash := hash * mul + NumGet(string, A_Index-1, "UChar")
		hash &= 0xFFFFFFFF
	}
	
	Return Format("{1:08X}", hash)
}
github://oGDIp - GDI+ wrapper for AHK v1.1

Return to “Scripts and Functions (v1)”

Who is online

Users browsing this forum: No registered users and 151 guests