- Added Stdout() to output to a created console output,appending every subsequent output to console...
Added Stdin() to accept input from console to var...
This is method seldom used to display script output for reasons beyond me,with most people deferring to MsgBox,ToolTip & TrayTip commands or Gui's to display output, TBH i've really only ever seen it used from a script by Lexikos which is where i picked up the idea from... Thought i'd share it, if anything so that people use that builtin GUI for something... Cheers...
Code: Select all
Print("Hello World")
Loop, 100
PrintDebug(A_Index)
MsgBox
PrintDebug() ;clear debug output...
MsgBox
PrintDebug("A new debug session...")
MsgBox
Print(string){
ListVars
WinWait ahk_id %A_ScriptHwnd%
ControlSetText Edit1, %string%
WinWaitClose
}
PrintDebug(string:=""){
Static
string := string ? string . "`r`n" . lastStr : "", lastStr := string
If !WinActive("ahk_class AutoHotkey"){
ListVars
WinWait ahk_id %A_ScriptHwnd%
WinGetTitle, title, ahk_id %A_ScriptHwnd%
}Else If !string{
PostMessage, 0x112, 0xF060,,, %title% ; 0x112 = WM_SYSCOMMAND, 0xF060 = SC_CLOSE
Return
}
ControlSetText Edit1, %string%, ahk_id %A_ScriptHwnd%
}
Stdout() below basically lets output directly to console,without running the script with a | more pipe or even running it from a console, allowing use of console just as any MsgBox,ToolTip & TrayTip commands or Gui's to display output...
StdIn() accepts console input to var...
As per wolf_II's suggestion Stdout is compatible with SciTE, though Stdin is not as i don't know how to read from SciTE console...
Updated: Fixed to work with scripts being run from console,i.e output to console being run on, StdIn may have some issues though with intermittent gafs when accepting input from existing console, cause unkown.
Code: Select all
Loop, 100
Stdout(A_Index, false) ;output to new console with scite check disabled for realtime performance.
Stdout("Hello there.") ;output to new console
name := Stdin("so what's your name?")
Stdout("Hello again... " . name) ;append to console
Sleep 2000
Stdout() ;free console
Sleep 2000
Stdout("Back again") ;output to new console
Sleep 2000
Stdout()
Stdout(output:="", sciteCheck := true){ ;output to console - sciteCheck reduces Stdout/Stdin performance,so where performance is necessary disable it accordingly
Global ___console___
If (sciteCheck && ProcessExist("SciTE.exe") && GetScriptParentProcess() = "SciTE.exe"){ ;if script parent is scite,output to scite console & return
FileAppend, %output%`n, *
Return
} ;CONOUT$ is a special file windows uses to expose attached console output
( output ? ( !___console___? (DllCall("AttachConsole", "int", -1) || DllCall("AllocConsole")) & (___console___:= true) : "" ) & FileAppend(output . "`n","CONOUT$") : DllCall("FreeConsole") & (___console___:= false) & StdExit() )
}
Stdin(output:="", sciteCheck := true){ ;output to console & wait for input & return input
Global ___console___
If (sciteCheck && ProcessExist("SciTE.exe") && GetScriptParentProcess() = "SciTE.exe"){ ;if script parent is scite,output to scite console & return
FileAppend, %output%`n, *
Return
}
( output ? ( !___console___? (DllCall("AttachConsole", "int", -1) || DllCall("AllocConsole")) & (___console___:= true) : "" ) & FileAppend(output . "`n","CONOUT$") & (Stdin := FileReadLine("CONIN$",1)) : DllCall("FreeConsole") & (___console___:= false) & StdExit() )
Return Stdin
}
StdExit(){
If GetScriptParentProcess() = "cmd.exe" ;couldn't get this: 'DllCall("GenerateConsoleCtrlEvent", CTRL_C_EVENT, 0)' to work so...
ControlSend, , {Enter}, % "ahk_pid " . GetParentProcess(GetCurrentProcess())
}
FileAppend(str, file){
FileAppend, %str%, %file%
}
FileReadLine(file,lineNum){
FileReadLine, retVal, %file%, %lineNum%
return retVal
}
ProcessExist(procName){
Process, Exist, % procName
Return ErrorLevel
}
GetScriptParentProcess(){
return GetProcessName(GetParentProcess(GetCurrentProcess()))
}
GetParentProcess(PID)
{
static function := DllCall("GetProcAddress", "ptr", DllCall("GetModuleHandle", "str", "kernel32.dll", "ptr"), "astr", "Process32Next" (A_IsUnicode ? "W" : ""), "ptr")
if !(h := DllCall("CreateToolhelp32Snapshot", "uint", 2, "uint", 0))
return
VarSetCapacity(pEntry, sz := (A_PtrSize = 8 ? 48 : 36)+(A_IsUnicode ? 520 : 260))
Numput(sz, pEntry, 0, "uint")
DllCall("Process32First" (A_IsUnicode ? "W" : ""), "ptr", h, "ptr", &pEntry)
loop
{
if (pid = NumGet(pEntry, 8, "uint") || !DllCall(function, "ptr", h, "ptr", &pEntry))
break
}
DllCall("CloseHandle", "ptr", h)
return Numget(pEntry, 16+2*A_PtrSize, "uint")
}
GetProcessName(PID)
{
static function := DllCall("GetProcAddress", "ptr", DllCall("GetModuleHandle", "str", "kernel32.dll", "ptr"), "astr", "Process32Next" (A_IsUnicode ? "W" : ""), "ptr")
if !(h := DllCall("CreateToolhelp32Snapshot", "uint", 2, "uint", 0))
return
VarSetCapacity(pEntry, sz := (A_PtrSize = 8 ? 48 : 36)+260*(A_IsUnicode ? 2 : 1))
Numput(sz, pEntry, 0, "uint")
DllCall("Process32First" (A_IsUnicode ? "W" : ""), "ptr", h, "ptr", &pEntry)
loop
{
if (pid = NumGet(pEntry, 8, "uint") || !DllCall(function, "ptr", h, "ptr", &pEntry))
break
}
DllCall("CloseHandle", "ptr", h)
return StrGet(&pEntry+28+2*A_PtrSize, A_IsUnicode ? "utf-16" : "utf-8")
}
GetCurrentProcess()
{
return DllCall("GetCurrentProcessId")
}