A collection of small ahk functions

Post your working scripts, libraries and tools
User avatar
jNizM
Posts: 1987
Joined: 30 Sep 2013, 01:33
GitHub: jNizM

A collection of small ahk functions

08 May 2014, 05:40

A collection of small (maybe useful) ahk functions


Table of Contents
  • Convert Base - Convert Base from 2 - 36 via DllCall.
  • CRC32 - Cyclic redundancy check.
  • CryptBinToHex - Convert binary buffer to hex.
  • Disp - Takes the content of a var and creates a displayable string for it.
  • DnsFlushResolverCache - Flush entire DNS cache, same as ipconfig /flushdns.
  • GCD / MCode GCD (32 bit only) - Find the greatest common divisor of two numbers.
  • GetDnsAddress - Get a list of DNS servers used by the local computer.
  • GetMacAddress - Get a list of computers MAC address.
  • GetVersion - Get Windows Major-, Minor- & Build-Version.
  • HasVal - Checks if a value exists in an array (similar to objects/Object.htm#HasKey)
  • hexToStr - Convert Hex to Text Strings.
  • InArr - Similar to InStr(), just the array is searched for a string.
  • IniParser - Parse ini files and return an array of objects with "__SECTION" and relative keys as properties.
  • isGUID - Check if GUID is valid.
  • isRegKey - Check if RegKey is valid.
  • julka_msgbox - msgbox with custom buttons.
  • Minimum / Maximum - Find the minimum of 2 vars / Find the maximum of 2 vars.
  • min / max
  • OSInstallDate - Returns the install date of your operating system.
  • PopCount - Get a number's population count.
  • Random using RtlRandom
    Random using RtlRandomEx
  • RefreshPolicy - Causes policy to be applied immediately on the computer like gpupdate does
  • repr - Return a string containing a printable representation of an object.
  • RSHash - Robert Sedgewick's string hashing algorithm.
  • ScaleToFit - Returns the dimensions of the scaled source rectangle that fits within the destination rectangle.
  • Start Control Panel Applications (Shortcuts)
  • StringReverse
  • strToHex - Convert Text Strings to Hex.
  • StrTrim - Removes specified leading and trailing characters from a string.
    StrToInt - Converts a string that represents a decimal value to an integer.
    StrToIntEx - Converts a string representing a decimal or hexadecimal number to an integer.
    StrToInt64Ex - Converts a string representing a decimal or hexadecimal value to a 64-bit integer.
  • UpdateScript - Update running (.ahk) script in realtime (~ 500-1000 ms) if you change something in the script.
  • WheelScrollLines - Retrieves or sets the number of lines to scroll when the vertical mouse wheel is moved.


Feel free to add more

example:

Title
Small description

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

MsgBox % "Your code here"


output:
Last edited by jNizM on 08 May 2014, 06:42, edited 1 time in total.
[AHK] 1.1.25.01 x64 Unicode
[WIN] 10 Pro (Version 1607 | Build 14393.970) x64
My GitHub Profile | Donations are appreciated if I could help you
User avatar
jNizM
Posts: 1987
Joined: 30 Sep 2013, 01:33
GitHub: jNizM

Re: A collection of small ahk functions

08 May 2014, 05:43

strToHex
Convert Text Strings to Hex

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


example:

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

MsgBox % "Text:`tAutHotkey`nHex:`t" strToHex("AutHotkey")

output:

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

Text:    AutoHotkey
Hex: 0x41 0x75 0x74 0x48 0x6f 0x74 0x6b 0x65 0x79
Last edited by jNizM on 03 Jun 2014, 03:53, edited 6 times in total.
[AHK] 1.1.25.01 x64 Unicode
[WIN] 10 Pro (Version 1607 | Build 14393.970) x64
My GitHub Profile | Donations are appreciated if I could help you
User avatar
jNizM
Posts: 1987
Joined: 30 Sep 2013, 01:33
GitHub: jNizM

Re: A collection of small ahk functions

08 May 2014, 05:48

hexToStr
Convert Hex to Text Strings

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

hexToStr(hex)
{
static u := A_IsUnicode ? "_wcstoui64" : "_strtoui64"
loop, parse, hex, " "
{
char .= Chr(DllCall("msvcrt.dll\" u, "Str", A_LoopField, "Uint", 0, "UInt", 16, "CDECL Int64"))
}
return char
}

example:

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

MsgBox % "Hex:`t0x41 0x75 0x74 0x48 0x6f 0x74 0x6b 0x65 0x79`nHex:`t" hexToStr("0x41 0x75 0x74 0x48 0x6f 0x74 0x6b 0x65 0x79")

output:

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

Hex:     0x41 0x75 0x74 0x48 0x6f 0x74 0x6b 0x65 0x79
Text: AutoHotkey
Last edited by jNizM on 03 Jun 2014, 03:52, edited 3 times in total.
[AHK] 1.1.25.01 x64 Unicode
[WIN] 10 Pro (Version 1607 | Build 14393.970) x64
My GitHub Profile | Donations are appreciated if I could help you
User avatar
jNizM
Posts: 1987
Joined: 30 Sep 2013, 01:33
GitHub: jNizM

Re: A collection of small ahk functions

08 May 2014, 07:05

Convert Base
Convert Base from 2 - 36 via DllCall (thanks to Laszlo & Gogo)

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

ConvertBase(InputBase, OutputBase, nptr)
{
static u := A_IsUnicode ? "_wcstoui64" : "_strtoui64"
static v := A_IsUnicode ? "_i64tow" : "_i64toa"
VarSetCapacity(s, 66, 0)
value := DllCall("msvcrt.dll\" u, "Str", nptr, "UInt", 0, "UInt", InputBase, "CDECL Int64")
DllCall("msvcrt.dll\" v, "Int64", value, "Str", s, "UInt", OutputBase, "CDECL")
return s
}

example:

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

MsgBox, % "Decimal:`t`t42`n"
. "to Binary:`t`t" ConvertBase(10, 2, 42) "`n"
. "to Octal:`t`t" ConvertBase(10, 8, 42) "`n"
. "to Hexadecimal:`t" ConvertBase(10, 16, 42) "`n`n"
. "Hexadecimal:`t2A`n"
. "to Decimal:`t" ConvertBase(16, 10, "2A") "`n"
. "to Octal:`t`t" ConvertBase(16, 8, "2A") "`n"
. "to Binary:`t`t" ConvertBase(16, 2, "2A") "`n`n"
ExitApp

output:

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

Decimal:       42
Binary: 101010
Octal: 52
Hexadecimal: 2a
Last edited by jNizM on 03 Jun 2014, 03:57, edited 3 times in total.
[AHK] 1.1.25.01 x64 Unicode
[WIN] 10 Pro (Version 1607 | Build 14393.970) x64
My GitHub Profile | Donations are appreciated if I could help you
User avatar
cyruz
Posts: 242
Joined: 30 Sep 2013, 13:31

Re: A collection of small ahk functions

08 May 2014, 09:29

I like this thread!

IniParser
Parse ini files and return an array of objects with "__SECTION" and relative keys as properties.

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

IniParser(sFile) {
arrSection := Object(), idx := 0
Loop, READ, %sFile%
If ( RegExMatch(A_LoopReadline, "S)^\s*\[(.*)\]\s*$", sSecMatch) )
++idx, arrSection[idx] := Object("__SECTION", sSecMatch1)
Else If ( RegExMatch(A_LoopReadLine, "S)^\s*(\w+)\s*\=\s*(.*)\s*$", sKeyValMatch) )
arrSection[idx].Insert(sKeyValMatch1, sKeyValMatch2)
Return arrSection
}


Example

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

obj := IniParser("test.ini")
For idx, item in obj
For k, v in item
If (k == "__SECTION")
MsgBox, Ini Section: %v%
Else
MsgBox, % k . " - " . v
Return
Last edited by cyruz on 08 May 2014, 09:41, edited 1 time in total.
ABCza on the old forum.
My GitHub.
User avatar
cyruz
Posts: 242
Joined: 30 Sep 2013, 13:31

Re: A collection of small ahk functions

08 May 2014, 09:38

CryptBinToHex
Convert binary buffer to hex.
Thanks to nnnik: viewtopic.php?f=6&t=1242#p8376 .

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

CryptBinToHex(ByRef sHex, ByRef cBuf) {
szBuf := VarSetCapacity(cBuf)
DllCall( "Crypt32.dll\CryptBinaryToString", Ptr,&cBuf, UInt,szBuf, UInt,4, Ptr,0, UIntP,szHex )
VarSetCapacity(cHex, szHex*2, 0)
DllCall( "Crypt32.dll\CryptBinaryToString", Ptr,&cBuf, UInt,szBuf, UInt,4, Ptr,&cHex, UIntP,szHex )
sHex := RegExReplace(StrGet(&cHex, szHex, "UTF-16"), "S)\s")
}


Example

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

FileRead, cBuf, *C C:\Windows\System32\drivers\etc\hosts
(FileExist(A_WinDir "\System32\Crypt32.dll")) ? CryptBinToHex(sHex, cBuf)
MsgBox, %sHex%
ABCza on the old forum.
My GitHub.
jballi
Posts: 386
Joined: 29 Sep 2013, 17:34

Re: A collection of small ahk functions

08 May 2014, 17:34

jNizM wrote:Convert Base
Convert Base from 2 - 36 via DllCall (thanks to Laszlo & Gogo)

This function will only work if using Unicode. A small enhancement so that the function will be Unicode-aware should do the trick. Something like...

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

DllCall(A_IsUnicode ? "msvcrt\_wcstoui64":"msvcrt\_strtoui64"
etc.
etc.

I hope this helps.
Builder
Posts: 6
Joined: 20 Apr 2014, 11:31

Re: A collection of small ahk functions

13 May 2014, 09:29

YMD(Date)

Converts a date from YYYYMMDDHH24MISS to YYYY-MM-DD

If Date is left blank, it converts the current date

ex... x=YMD()

if A_Now is 20140513092817 then the value of x as a string is 2014-05-13

YMD(DDate:="")
{
FormatTime, YMD, %DDate%, yyyy-MM-dd
return (YMD)
}
Last edited by Builder on 13 May 2014, 10:46, edited 1 time in total.
Coco
Posts: 771
Joined: 29 Sep 2013, 20:37
GitHub: cocobelgica

Re: A collection of small ahk functions

13 May 2014, 10:40

Builder wrote:Converts a date from YYYYMMDDHH24MISS to YYYY-MM-DD

Something like:
Builder
Posts: 6
Joined: 20 Apr 2014, 11:31

Re: A collection of small ahk functions

13 May 2014, 10:49

Doh!

I forgot to paste the actual code... Corrected with edit

Yes, it uses the format time function...

This just makes it easier to use in an expression...
User avatar
LinearSpoon
Posts: 150
Joined: 29 Sep 2013, 22:55

Re: A collection of small ahk functions

14 May 2014, 00:44

ScaleToFit
Accepts the dimensions of a source rectangle and the dimensions of a destination rectangle.
Returns the dimensions of the scaled source rectangle that fits within the destination rectangle, at the largest possible size and while maintaining aspect ratio.
Also returns the x and y offsets which center the scaled source rectangle within the destination rectangle.

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


Example usage

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

User avatar
jNizM
Posts: 1987
Joined: 30 Sep 2013, 01:33
GitHub: jNizM

Re: A collection of small ahk functions

30 May 2014, 08:29

@jballi
thx for info.. fixed
[AHK] 1.1.25.01 x64 Unicode
[WIN] 10 Pro (Version 1607 | Build 14393.970) x64
My GitHub Profile | Donations are appreciated if I could help you
User avatar
jNizM
Posts: 1987
Joined: 30 Sep 2013, 01:33
GitHub: jNizM

Re: A collection of small ahk functions

13 Jun 2014, 07:08

isGUID
Check if GUID is valid

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

isGUID(GUID) ; https://en.wikipedia.org/wiki/Globally_unique_identifier
{
return RegExMatch(GUID, "^(?:\{){0,1}[[:xdigit:]]{8}-(?:[[:xdigit:]]{4}-){3}[[:xdigit:]]{12}(?:\}){0,1}$") = 1
}

example:

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

MsgBox % isGUID("{21EC2020-3AEA-4069-A2DD-08002B30309D}")
MsgBox % isGUID("21EC2020-3AEA-4069-A2DD-08002B30309D")
MsgBox % isGUID("21EC2020-3AEA-4069-A2DD-08002B30309")

output:
[AHK] 1.1.25.01 x64 Unicode
[WIN] 10 Pro (Version 1607 | Build 14393.970) x64
My GitHub Profile | Donations are appreciated if I could help you
User avatar
jNizM
Posts: 1987
Joined: 30 Sep 2013, 01:33
GitHub: jNizM

Re: A collection of small ahk functions

13 Jun 2014, 07:28

isRegKey
Check if RegKey is valid

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

isRegKey(RegistryKey, FullPath := True)
{
return RegExMatch(RegistryKey, "(?i)\A\h*HK(CC|CR|CU|LM|U|EY_CLASSES_ROOT|EY_LOCAL_MACHINE|EY_USERS|EY_CURRENT_(USER|CONFIG))(64)?(?:\\[^\\]*)" ((FullPath) ? "+" : "*") "\z") = 1
}

example:

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

MsgBox % isRegKey("HKEY_CURRENT_USER\Software\AutoHotkey")
MsgBox % isRegKey("HKEY_LOCAL_MACHINE\Software\Microsoft\Windows\CurrentVersion")
MsgBox % isRegKey("HKEY_CORRENT_USER\Software\AutoHotkey")

output:
[AHK] 1.1.25.01 x64 Unicode
[WIN] 10 Pro (Version 1607 | Build 14393.970) x64
My GitHub Profile | Donations are appreciated if I could help you
User avatar
jNizM
Posts: 1987
Joined: 30 Sep 2013, 01:33
GitHub: jNizM

Re: A collection of small ahk functions

16 Jun 2014, 07:21

GetVersion
Get Windows Major-, Minor- & Build-Version

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

GetVersion()
{
return { 1 : DllCall("Kernel32.dll\GetVersion") & 0xff
, 2 : DllCall("Kernel32.dll\GetVersion") >> 8 & 0xff
, 3 : DllCall("Kernel32.dll\GetVersion") >> 16 & 0xffff }
}

example:

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

GetVersion := GetVersion()
MsgBox, % "Major:`t" GetVersion[1] "`n"
. "Minor:`t" GetVersion[2] "`n"
. "Build:`t" GetVersion[3]

output: (Windows 7 - SP1)

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

Major:    6
Minor: 1
Build: 7601

=========================================================================
example 2:

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

MajorVersion := DllCall("Kernel32.dll\GetVersion") & 0xff
MinorVersion := DllCall("Kernel32.dll\GetVersion") >> 8 & 0xff
BuildVersion := DllCall("Kernel32.dll\GetVersion") >> 16 & 0xffff

MsgBox, % MajorVersion "." MinorVersion "." BuildVersion

output 2: (Windows 7 - SP1)

#########################################################################
example 3: (one-line)

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

MsgBox % ((M := (B := DllCall("Kernel32.dll\GetVersion")) & 0xFFFF) & 0xFF) "." (M >> 8) "." (B >> 16)

output 3: (Windows 7 - SP1)
[AHK] 1.1.25.01 x64 Unicode
[WIN] 10 Pro (Version 1607 | Build 14393.970) x64
My GitHub Profile | Donations are appreciated if I could help you
User avatar
jNizM
Posts: 1987
Joined: 30 Sep 2013, 01:33
GitHub: jNizM

Re: A collection of small ahk functions

29 Jul 2014, 00:58

Minimum
Find the minimum of 2 vars

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

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

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


======================================================================================

Maximum
Find the maximum of 2 vars

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

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

MsgBox % max(27, 64) ; ==> 64
[AHK] 1.1.25.01 x64 Unicode
[WIN] 10 Pro (Version 1607 | Build 14393.970) x64
My GitHub Profile | Donations are appreciated if I could help you
User avatar
nnnik
Posts: 1689
Joined: 30 Sep 2013, 01:01
Location: Germany

Re: A collection of small ahk functions

29 Jul 2014, 02:10

Disp()
This is a very short function I wrote for debugging purposes.
It just takes the content of a var and creates a displayable string for it.

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



Example:

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

Msgbox % Disp("ABC")
Msgbox % Disp({a:{b:{c:[func("Test")]}}})
Test(){
return 42
}


Output:
ABC
{a:{b:{c:[func("Test")]}}}
I am not afraid of many things. But I do fear fear.
Coco
Posts: 771
Joined: 29 Sep 2013, 20:37
GitHub: cocobelgica

Re: A collection of small ahk functions

29 Jul 2014, 03:07

Here's something I use similar to nnnik's Disp():

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

User avatar
joedf
Posts: 5248
Joined: 29 Sep 2013, 17:08
Facebook: J0EDF
Google: +joedf
GitHub: joedf
Location: Canada, Quebec
Contact:

Re: A collection of small ahk functions

29 Jul 2014, 06:30

Nice functions!
toralf
Posts: 430
Joined: 27 Apr 2014, 21:08
Location: Germany

Re: A collection of small ahk functions

29 Jul 2014, 09:59

jNizM wrote:Minimum

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

min(x, y){
return y ^ ((x ^ y) & -(x < y))
}
What is the advantage of bitwise-and and bitwise-exclusiv-or over the tenary operator?

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

min(x, y){
Return x < y ? x : y
}
Same should apply for Maximum (x < y ? y : x).
ciao
toralf

Return to “Scripts and Functions”

Who is online

Users browsing this forum: c4p and 16 guests