A collection of small ahk functions
- Trubbleguy
- Posts: 18
- Joined: 11 Jul 2014, 03:00
Case (fixes names like o'donnel and McNamra)
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
Re: A collection of small ahk functions
It's just a random string I used as example for the function. RSHash creates a small numeric only hash.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.
MsgBox % RSHash("some_random_string_to_rshash_func")
[AHK] v2.0.5 | [WIN] 11 Pro (Version 22H2) | [GitHub] Profile
Re: A collection of small ahk functions
CRC32 (Cyclic redundancy check)
Source: (Implementation via AutoHotkey & via DllCalls)
CRC32.ahk (GitHub)
example:
output:
Ref:
- Cyclic redundancy check (wiki)
Source: (Implementation via AutoHotkey & via DllCalls)
CRC32.ahk (GitHub)
example:
Code: Select all
MsgBox % CRC32("The quick brown fox jumps over the lazy dog")
Code: Select all
0x414fa339
Ref:
- Cyclic redundancy check (wiki)
[AHK] v2.0.5 | [WIN] 11 Pro (Version 22H2) | [GitHub] Profile
Re: A collection of small ahk functions
GetMacAddress (Get a list of computers MAC address.)
Source:
GetMacAddress.ahk (GitHub)
example:
output:
Ref:
- MAC address (wiki)
Source:
GetMacAddress.ahk (GitHub)
example:
Code: Select all
for k, v in GetMacAddress()
MsgBox % v
Code: Select all
01:23:45:67:89:ab
Ref:
- MAC address (wiki)
[AHK] v2.0.5 | [WIN] 11 Pro (Version 22H2) | [GitHub] Profile
Re: A collection of small ahk functions
Useful collection!
There's a script to do that somewhere, but too large for this category!just me wrote:This is getting unclear. What about a TOC in the first post?
itros "ylbbub eht tuO kaerB" a ni kcuts m'I pleH
Re: A collection of small ahk functions
That only works for integers. This works for doubles toojNizM 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 varsCode: Select all
max(x, y) { return x ^ ((x ^ y) & -(x < y)) } MsgBox % max(27, 64) ; ==> 64
Code: Select all
min(x,y)
{
return (x<y)*x+(y<=x)*y
}
max(x,y)
{
return (x>y)*x+(y>=x)*y
}
Re: A collection of small ahk functions
HasVal (for Simple Arrays)
Checks if a value exists in an array (similar to HasKey)
FoundPos := HasVal(Haystack, Needle)
Examples
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
}
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
Re: A collection of small ahk functions
GetDnsAddress (Get a list of DNS servers used by the local computer.)
Source:
GetDnsAddress.ahk (GitHub)
example:
output:
Ref:
- Domain Name System (wiki)
Source:
GetDnsAddress.ahk (GitHub)
example:
Code: Select all
for k, v in GetDnsAddress()
MsgBox % v
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
- JoeWinograd
- Posts: 2200
- Joined: 10 Feb 2014, 20:00
- Location: U.S. Central Time Zone
Re: A collection of small ahk functions
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
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
Re: A collection of small ahk functions
merci & fixed
[AHK] v2.0.5 | [WIN] 11 Pro (Version 22H2) | [GitHub] Profile
Re: A collection of small ahk functions
RefreshPolicy (Causes policy to be applied immediately on the computer like gpupdate does)
Source:
RefreshPolicy.ahk (GitHub)
example:
Ref:
- RefreshPolicy function (msdn)
- RefreshPolicyEx function (msdn)
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
Re: A collection of small ahk functions
UpdateScript
Update running (.ahk) script in realtime (~ 500-1000 ms) if you change something in the script
Example
Ref:
Refresh program to detect code change
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
}
}
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
Re: A collection of small ahk functions
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
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
Re: A collection of small ahk functions
@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
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
Re: A collection of small ahk functions
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)
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
Re: A collection of small ahk functions
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)
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
Re: A collection of small ahk functions
IsUEFIorBIOS (Detect if Windows was installed in Legacy BIOS or UEFI mode)
Source:
Example:
Output:
Ref:
- GetFirmwareEnvironmentVariable function (msdn)
- How distinguish when Windows was installed in Legacy BIOS or UEFI mode using Delphi?
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"
}
Code: Select all
MsgBox % IsUEFIorBIOS()
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
Re: A collection of small ahk functions
ElfHash (ELF Hash Implementation in AutoHotkey)
Source:
Example:
Output:
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)
}
Code: Select all
MsgBox % ElfHash("The quick brown fox jumps over the lazy dog")
Code: Select all
0x4280c57
[AHK] v2.0.5 | [WIN] 11 Pro (Version 22H2) | [GitHub] Profile
Re: A collection of small ahk functions
A few more simple-to-implement hash functions, mostly for educational purposes:
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.
- Jenkins' one-at-a-time (32-bit)
- FNV-1a (32/64-bit)
- Bernstein's DJB2
- SDBM
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)
}
Return to “Scripts and Functions (v1)”
Who is online
Users browsing this forum: No registered users and 151 guests