by burque505 » 28 Jan 2024, 13:09
I'll start posting Keysharp scripts here for the time being. Porting some of
@jNizM's
Collection of useful AutoHotkey v2 scripts and functions seemed like a good place to start.
Keysharp at present can run some AHK scripts without changes (even if they include '#Requires AutoHotkey v2.0'), others with minor changes, some with major changes and some not at all. I'll pick low-hanging fruit to start with.
(Please note that were commits to Keysharp since the latest release - if a script doesn't run for you, you could try building Keysharp yourself. It's fast and relatively easy to do with Visual Studio 2022 Community.)
FileFindString.ks
FileCountLines.ks
DisableCloseButton.ks
SpoilerCode: Select all
; PORTING REQUIRED (MINOR)
; =============================================================================================================================================================
; Author ........: jNizM
; Released ......: 2017-04-13
; Modified ......: 2023-01-16
; Tested with....: AutoHotkey v2.0.2 (x64)
; Tested on .....: Windows 11 - 22H2 (x64)
; Function ......: GuiDisableCloseButton()
; KS ............: 2024-01-23
; Parameter(s)...: Handle - Gui.Hwnd
;
; Return ........: Disables the GUI Close Button.
; =============================================================================================================================================================
#Requires AutoHotkey v2.0
Main := Gui()
Main.OnEvent("Close", (*) => ExitApp())
Main.Show("w400 h300")
GuiDisableCloseButton(Main.Hwnd)
GuiDisableCloseButton(Handle)
{
static SC_CLOSE := 0xF060
static MF_GRAYED := 0x00000001
static MF_DISABLED := 0x00000002
hMenu := DllCall("user32\GetSystemMenu", "Ptr", Handle, "Int", False, "Ptr")
DllCall("user32\EnableMenuItem", "Ptr", hMenu, "UInt", SC_CLOSE, "UInt", MF_GRAYED | MF_DISABLED)
return DllCall("user32\DrawMenuBar", "Ptr", Handle)
}
; =============================================================================================================================================================
DisableMove.ks
SpoilerCode: Select all
; PORTING WAS REQUIRED (MINOR)
; =============================================================================================================================================================
; Author ........: jNizM
; Released ......: 2017-04-13
; Modified ......: 2023-01-16
; Tested with....: AutoHotkey v2.0.2 (x64)
; Tested on .....: Windows 11 - 22H2 (x64)
; Function ......: GuiDisableMove()
; Tested with KS.: 2024-01-23
; Parameter(s)...: Handle - Gui.Hwnd
;
; Return ........: Disables the GUI Move function.
; =============================================================================================================================================================
#Requires AutoHotkey v2.0
Main := Gui()
Main.OnEvent("Close", (*) => ExitApp()) ; parens added for ExitApp
Main.Show("w400 h300")
GuiDisableMove(Main.Hwnd)
GuiDisableMove(Handle)
{
static SC_MOVE := 0xF010
static MF_BYCOMMAND := 0x00000000
hMenu := DllCall("user32\GetSystemMenu", "Ptr", Handle, "Int", False, "Ptr")
DllCall("user32\RemoveMenu", "Ptr", hMenu, "UInt", SC_MOVE, "UInt", MF_BYCOMMAND)
return DllCall("user32\DrawMenuBar", "Ptr", Handle)
}
; =============================================================================================================================================================
DNSServerList.ks
SpoilerCode: Select all
; =============================================================================================================================================================
; Author ........: jNizM
; Released ......: 2023-01-17
; Modified ......: 2023-01-17
; Tested with....: AutoHotkey v2.0.2 (x64)
; Tested on .....: Windows 11 - 22H2 (x64)
; Function ......: DnsServerList()
; Keysharp ......: Tested with rebuilt Keysharp 2024-01-28
; Parameter(s)...: No parameters used
;
; Return ........: Gets a list of DNS servers for the local computer.
; =============================================================================================================================================================
#Requires AutoHotkey v2.0
DnsServerList()
{
static STATUS_SUCCESS := 0
static DnsConfigDnsServerList := 6
DllCall("dnsapi\DnsQueryConfig", "Int", DnsConfigDnsServerList, "UInt", 0, "Ptr", 0, "Ptr", 0, "Ptr", 0, "UInt*", &Size := 0)
Buf := Buffer(Size)
DNS_STATUS := DllCall("dnsapi\DnsQueryConfig", "Int", DnsConfigDnsServerList, "UInt", 0, "Ptr", 0, "Ptr", 0, "Ptr", Buf, "UInt*", Buf.Size)
if (DNS_STATUS = STATUS_SUCCESS)
{
DNS_SERVER := Array()
loop NumGet(Buf, 0, "UInt")
{
DNS_SERVER.Push(DllCall("ws2_32\inet_ntoa", "UInt", NumGet(Buf, 4 * A_Index, "UInt"), "AStr"))
}
return DNS_SERVER
}
throw OSError()
}
; =============================================================================================================================================================
; Example
; =============================================================================================================================================================
DNS := DnsServerList()
loop DNS.Length
MsgBox DNS[A_Index]
CreateUUID.ks
SpoilerCode: Select all
; =============================================================================================================================================================
; Author ........: jNizM
; Released ......: 2021-10-13
; Modified ......: 2023-01-12
; Tested with....: AutoHotkey v2.0.2 (x64)
; Tested on .....: Windows 11 - 22H2 (x64)
; Function ......: CreateUUID()
; Keysharp ......: Tested with rebuilt Keysharp 2024-01-28
; Parameter(s)...: No parameters used
;
; Return ........: Creates an Universally Unique IDentifier (UUID).
; A UUID provides a unique designation of an object such as an interface, a manager entry-point vector, or a client object.
; =============================================================================================================================================================
#Requires AutoHotkey v2.0
CreateUUID()
{
static RPC_S_OK := 0, UUID := ""
pUUID := Buffer(16)
if (DllCall("rpcrt4\UuidCreate", "Ptr", pUUID) = RPC_S_OK)
{
if (DllCall("rpcrt4\UuidToStringW", "Ptr", pUUID, "Ptr*", &StringUuid := 0) = RPC_S_OK)
{
UUID := StrGet(StringUuid)
DllCall("rpcrt4\RpcStringFreeW", "Ptr*", StringUuid)
}
}
return UUID
}
; =============================================================================================================================================================
; Example
; =============================================================================================================================================================
MsgBox CreateUUID()
I'll start posting Keysharp scripts here for the time being. Porting some of [mention]jNizM[/mention]'s [url=https://www.autohotkey.com/boards/viewtopic.php?f=83&t=89720&hilit=filefindstring]Collection of useful AutoHotkey v2 scripts and functions[/url] seemed like a good place to start.
Keysharp at present can run some AHK scripts without changes (even if they include '#Requires AutoHotkey v2.0'), others with minor changes, some with major changes and some not at all. I'll pick low-hanging fruit to start with.
(Please note that were commits to Keysharp since the latest release - if a script doesn't run for you, you could try building Keysharp yourself. It's fast and relatively easy to do with Visual Studio 2022 Community.)
FileFindString.ks
[spoiler][code]; =============================================================================================================================================================
; Author ........: jNizM
; Released ......: 2021-04-23
; Modified ......: 2023-01-12
; Tested with....: AutoHotkey v2.0.2 (x64)
; Tested on .....: Windows 11 - 22H2 (x64)
; Function ......: FileFindString( FileName, Search )
; Keysharp ver...: 2024-01-24
; Parameter(s)...: FileName - path to the file
; Search - the word or string to search for
;
; Return ........: Finds a specific word / string in a text file.
; =============================================================================================================================================================
#Requires AutoHotkey v2.0
FileFindString(FileName, Search)
{
try
File := FileOpen(FileName, "r-d")
catch as Err
{
MsgBox "Can't open '" FileName "`n`n" Type(Err) ": " Err.Message
return
}
Found := Map()
while !(File.AtEOF)
{
if (InStr(GetLine := File.ReadLine(), Search))
{
Found[A_Index] := GetLine
}
}
File.Close()
return Found
}
; =============================================================================================================================================================
; Example
; =============================================================================================================================================================
for Index, Value in FileFindString("YourFileWithFullPath.txt", "YourString") ; ADAPT THIS LINE FOR YOUR OWN USE
MsgBox "Line: " Index "`n`n" Value[/code][/spoiler]
FileCountLines.ks
[spoiler][code]; =============================================================================================================================================================
; Author ........: jNizM
; Released ......: 2021-04-23
; Modified ......: 2023-01-12
; Ported to KS...: 2024-01-23
; Tested with....: AutoHotkey v2.0.2 (x64)
; KS Test with...: Keysharp v0.0.0.6
; Tested on .....: Windows 11 - 22H2 (x64)
; Function ......: FileCountLines( FileName )
;
; Parameter(s)...: FileName - path to the file
;
; Return ........: Count the number of lines in a file.
; =============================================================================================================================================================
#Requires AutoHotkey v2.0
FileCountLines(FileName)
{
try
File := FileOpen(FileName, "r-d")
catch as Err
{
MsgBox "Can't open '" FileName "`n`n" Type(Err) ": " Err.Message
return
}
CountLines := 0
while !(File.AtEOF)
{
File.ReadLine()
CountLines++
}
File.Close()
return CountLines
}
; =============================================================================================================================================================
; Example
; =============================================================================================================================================================
MsgBox FileCountLines("YourFileHere.txt")[/code][/spoiler]
DisableCloseButton.ks
[spoiler][code]; PORTING REQUIRED (MINOR)
; =============================================================================================================================================================
; Author ........: jNizM
; Released ......: 2017-04-13
; Modified ......: 2023-01-16
; Tested with....: AutoHotkey v2.0.2 (x64)
; Tested on .....: Windows 11 - 22H2 (x64)
; Function ......: GuiDisableCloseButton()
; KS ............: 2024-01-23
; Parameter(s)...: Handle - Gui.Hwnd
;
; Return ........: Disables the GUI Close Button.
; =============================================================================================================================================================
#Requires AutoHotkey v2.0
Main := Gui()
Main.OnEvent("Close", (*) => ExitApp())
Main.Show("w400 h300")
GuiDisableCloseButton(Main.Hwnd)
GuiDisableCloseButton(Handle)
{
static SC_CLOSE := 0xF060
static MF_GRAYED := 0x00000001
static MF_DISABLED := 0x00000002
hMenu := DllCall("user32\GetSystemMenu", "Ptr", Handle, "Int", False, "Ptr")
DllCall("user32\EnableMenuItem", "Ptr", hMenu, "UInt", SC_CLOSE, "UInt", MF_GRAYED | MF_DISABLED)
return DllCall("user32\DrawMenuBar", "Ptr", Handle)
}
; =============================================================================================================================================================[/code][/spoiler]
DisableMove.ks
[spoiler][code]; PORTING WAS REQUIRED (MINOR)
; =============================================================================================================================================================
; Author ........: jNizM
; Released ......: 2017-04-13
; Modified ......: 2023-01-16
; Tested with....: AutoHotkey v2.0.2 (x64)
; Tested on .....: Windows 11 - 22H2 (x64)
; Function ......: GuiDisableMove()
; Tested with KS.: 2024-01-23
; Parameter(s)...: Handle - Gui.Hwnd
;
; Return ........: Disables the GUI Move function.
; =============================================================================================================================================================
#Requires AutoHotkey v2.0
Main := Gui()
Main.OnEvent("Close", (*) => ExitApp()) ; parens added for ExitApp
Main.Show("w400 h300")
GuiDisableMove(Main.Hwnd)
GuiDisableMove(Handle)
{
static SC_MOVE := 0xF010
static MF_BYCOMMAND := 0x00000000
hMenu := DllCall("user32\GetSystemMenu", "Ptr", Handle, "Int", False, "Ptr")
DllCall("user32\RemoveMenu", "Ptr", hMenu, "UInt", SC_MOVE, "UInt", MF_BYCOMMAND)
return DllCall("user32\DrawMenuBar", "Ptr", Handle)
}
; =============================================================================================================================================================[/code][/spoiler]
DNSServerList.ks
[spoiler][code]; =============================================================================================================================================================
; Author ........: jNizM
; Released ......: 2023-01-17
; Modified ......: 2023-01-17
; Tested with....: AutoHotkey v2.0.2 (x64)
; Tested on .....: Windows 11 - 22H2 (x64)
; Function ......: DnsServerList()
; Keysharp ......: Tested with rebuilt Keysharp 2024-01-28
; Parameter(s)...: No parameters used
;
; Return ........: Gets a list of DNS servers for the local computer.
; =============================================================================================================================================================
#Requires AutoHotkey v2.0
DnsServerList()
{
static STATUS_SUCCESS := 0
static DnsConfigDnsServerList := 6
DllCall("dnsapi\DnsQueryConfig", "Int", DnsConfigDnsServerList, "UInt", 0, "Ptr", 0, "Ptr", 0, "Ptr", 0, "UInt*", &Size := 0)
Buf := Buffer(Size)
DNS_STATUS := DllCall("dnsapi\DnsQueryConfig", "Int", DnsConfigDnsServerList, "UInt", 0, "Ptr", 0, "Ptr", 0, "Ptr", Buf, "UInt*", Buf.Size)
if (DNS_STATUS = STATUS_SUCCESS)
{
DNS_SERVER := Array()
loop NumGet(Buf, 0, "UInt")
{
DNS_SERVER.Push(DllCall("ws2_32\inet_ntoa", "UInt", NumGet(Buf, 4 * A_Index, "UInt"), "AStr"))
}
return DNS_SERVER
}
throw OSError()
}
; =============================================================================================================================================================
; Example
; =============================================================================================================================================================
DNS := DnsServerList()
loop DNS.Length
MsgBox DNS[A_Index][/code][/spoiler]
CreateUUID.ks
[spoiler][code]; =============================================================================================================================================================
; Author ........: jNizM
; Released ......: 2021-10-13
; Modified ......: 2023-01-12
; Tested with....: AutoHotkey v2.0.2 (x64)
; Tested on .....: Windows 11 - 22H2 (x64)
; Function ......: CreateUUID()
; Keysharp ......: Tested with rebuilt Keysharp 2024-01-28
; Parameter(s)...: No parameters used
;
; Return ........: Creates an Universally Unique IDentifier (UUID).
; A UUID provides a unique designation of an object such as an interface, a manager entry-point vector, or a client object.
; =============================================================================================================================================================
#Requires AutoHotkey v2.0
CreateUUID()
{
static RPC_S_OK := 0, UUID := ""
pUUID := Buffer(16)
if (DllCall("rpcrt4\UuidCreate", "Ptr", pUUID) = RPC_S_OK)
{
if (DllCall("rpcrt4\UuidToStringW", "Ptr", pUUID, "Ptr*", &StringUuid := 0) = RPC_S_OK)
{
UUID := StrGet(StringUuid)
DllCall("rpcrt4\RpcStringFreeW", "Ptr*", StringUuid)
}
}
return UUID
}
; =============================================================================================================================================================
; Example
; =============================================================================================================================================================
MsgBox CreateUUID()[/code][/spoiler]