neo-layout/windows/neo-vars/tools/script/util.ahk

55 lines
1.8 KiB
AutoHotkey

; Utilities module for tools
; Run a command and returns its exit code,
; and retrieves standard (error-)output.
;
; Uses the Windows-Script Host to run cmd.exe.
;
; Known limitations:
; - Opens terminal window in foreground for the duration of the command.
; - Character encoding: uses shell codepage; problems with unicode.
;
; References:
; - https://www.autohotkey.com/docs/commands/Run.htm#StdOut
; - https://docs.microsoft.com/en-us/previous-versions//2f38xsxe(v=vs.85)
util_runWaitCMD(command, ByRef strOut:="", ByRef strErr:="")
{
; create WshScriptExec object running our command
shell := ComObjCreate("WScript.Shell")
exec := shell.Exec(ComSpec . " /C " . """" . command . """")
; bring our window on front of shell window
if (exec.Status == 0)
sleep 100
Gui, %A_DefaultGui%: Show
; read from output-streams until finished
strOut := exec.StdOut.ReadAll()
strErr := exec.StdErr.ReadAll()
return exec.ExitCode
}
; Path normalization
;
; Returns the fully expanded, normalized and simplified absolute path.
; Relative paths are expanded relative to the current working directory.
; Input path does not need to be a valid file or directory.
;
; Reference/Source:
; - https://www.autohotkey.com/docs/misc/LongPaths.htm
; - https://github.com/AutoHotkey/Ahk2Exe/blob/faec21a5d8ad9a7fcab6849b4329ea2887731f4b/Compiler.ahk
util_getFullPath(path)
{
; count characters; ANSI limits pathlength
cc := A_IsUnicode
? DllCall("GetFullPathName", "str", path, "uint", 0, "ptr", 0, "ptr", 0, "uint")
: 260
VarSetCapacity(buf, cc * (A_IsUnicode ? 2 : 1))
return DllCall("GetFullPathName", "str", path, "uint", cc, "str", buf, "ptr", 0) ? buf : ""
}
; Return "s" if val is plural or zero, "" otherwise
util_pluralS(val)
{
return (Abs(val) == 1 ? "" : "s")
}