Jump to content

AutoIt ConsoleWrite (using "builtin" Autoit Hidden window)


boomingranny
 Share

Recommended Posts

Use this UDF to add a console gui to your script (with log file):
It uses the Hidden Autoit window (that you probably didn't even know existed)
Closing Console window will terminate script.

example of console:
image.png.81c28af5c81bbd9fa3b8c5ad8cee9b04.png

#include-once
#include <GuiEdit.au3>

EnableConsoleGui("example.log")

;example:
;------------------------
ConsoleWrite ("Hello World")
For $i = 1 To 10
    ConsoleWrite (".")
    Sleep(200)
Next
ConsoleWrite ("done")
ConsoleWrite(@CRLF)
ConsoleWrite ("close me to exit"&@CRLF)
While 1
    Sleep(1000)
WEnd

;------------------------
;end of example code

Func EnableConsoleGui($Logfile="")
    ;EnableConsoleGUI
    ;by Daniel Barnes 20/04/2018
    ;Uses AutoIt's Hidden window as a console (output only)

    Global $pidChild

    ;if we don't have a parent (as the parent window would have our script name)
    If Not WinExists(StringTrimRight(@ScriptName,4)) Then

        Opt("TrayIconHide",1)

        ;get Autoit's hidden window handle
        local $hWnd = WinGetHandle(AutoItWinGetTitle())

        ;move the autoit hidden window to the middle of the screen
        WinMove($hWnd, "", (@DesktopWidth / 2) - 250, (@DesktopHeight / 2) - 250, 500, 500)

        ;get the Handle of the edit box in Autoit's hidden window
        $hEditBox = ControlGetHandle($hWnd,"","[CLASS:Edit; INSTANCE:1]")

        ;show it
        WinSetState($hWnd, "", @SW_SHOW)

        ;set its title = our script name
        WinSetTitle($hWnd,"",StringTrimRight(@ScriptName,4))

        ;Spawn a child "copy" of the script, enabling reading of its console...
        If @Compiled Then
            ;2 = $STDOUT_CHILD. This avoids requiring the AutoItConstants.au3 in this sample code
            $pidChild= Run( FileGetShortName(@ScriptFullPath),@ScriptDir,"",2)
        Else
            ;2 = $STDOUT_CHILD. This avoids requiring the AutoItConstants.au3 in this sample code
            $pidChild= Run( FileGetShortName(@AutoItExe) & " " & FileGetShortName(@ScriptFullPath),@ScriptDir,"",2)
        EndIf

        OnAutoItExitRegister("EnableConsoleGui_CloseChildPID")

        ;read the console, while the child window exists (and the console window is visible)
        While ProcessExists($pidChild)
            $ConsoleRead = StdoutRead($pidChild)
            If $ConsoleRead then
                $text = StringLeft(ControlGetText($hWnd,"",$hEditBox),65535)
                If $Logfile Then FileWrite($Logfile,$ConsoleRead)
                $text &= $ConsoleRead
                ControlSetText($hWnd,"",$hEditBox,$text)
                ConsoleWrite($ConsoleRead)
                ;scroll to bottom of console edit window
                _GUICtrlEdit_SetSel($hEditBox, 65535, 65535)
            endif
            Sleep(250)
        WEnd
        exit
    endif
EndFunc

Func EnableConsoleGui_CloseChildPID()
    ;if this func isn't used
    ;when you close the console gui
    ;the child "clone" of your script will keep running
    ProcessClose($pidChild)
EndFunc
Edited by boomingranny
Link to comment
Share on other sites

Nice idea. I would however rename the internal window first to something unique (like adding the PID) and then get the title.

UDF List:

 
_AdapterConnections()_AlwaysRun()_AppMon()_AppMonEx()_ArrayFilter/_ArrayReduce_BinaryBin()_CheckMsgBox()_CmdLineRaw()_ContextMenu()_ConvertLHWebColor()/_ConvertSHWebColor()_DesktopDimensions()_DisplayPassword()_DotNet_Load()/_DotNet_Unload()_Fibonacci()_FileCompare()_FileCompareContents()_FileNameByHandle()_FilePrefix/SRE()_FindInFile()_GetBackgroundColor()/_SetBackgroundColor()_GetConrolID()_GetCtrlClass()_GetDirectoryFormat()_GetDriveMediaType()_GetFilename()/_GetFilenameExt()_GetHardwareID()_GetIP()_GetIP_Country()_GetOSLanguage()_GetSavedSource()_GetStringSize()_GetSystemPaths()_GetURLImage()_GIFImage()_GoogleWeather()_GUICtrlCreateGroup()_GUICtrlListBox_CreateArray()_GUICtrlListView_CreateArray()_GUICtrlListView_SaveCSV()_GUICtrlListView_SaveHTML()_GUICtrlListView_SaveTxt()_GUICtrlListView_SaveXML()_GUICtrlMenu_Recent()_GUICtrlMenu_SetItemImage()_GUICtrlTreeView_CreateArray()_GUIDisable()_GUIImageList_SetIconFromHandle()_GUIRegisterMsg()_GUISetIcon()_Icon_Clear()/_Icon_Set()_IdleTime()_InetGet()_InetGetGUI()_InetGetProgress()_IPDetails()_IsFileOlder()_IsGUID()_IsHex()_IsPalindrome()_IsRegKey()_IsStringRegExp()_IsSystemDrive()_IsUPX()_IsValidType()_IsWebColor()_Language()_Log()_MicrosoftInternetConnectivity()_MSDNDataType()_PathFull/GetRelative/Split()_PathSplitEx()_PrintFromArray()_ProgressSetMarquee()_ReDim()_RockPaperScissors()/_RockPaperScissorsLizardSpock()_ScrollingCredits_SelfDelete()_SelfRename()_SelfUpdate()_SendTo()_ShellAll()_ShellFile()_ShellFolder()_SingletonHWID()_SingletonPID()_Startup()_StringCompact()_StringIsValid()_StringRegExpMetaCharacters()_StringReplaceWholeWord()_StringStripChars()_Temperature()_TrialPeriod()_UKToUSDate()/_USToUKDate()_WinAPI_Create_CTL_CODE()_WinAPI_CreateGUID()_WMIDateStringToDate()/_DateToWMIDateString()Au3 script parsingAutoIt SearchAutoIt3 PortableAutoIt3WrapperToPragmaAutoItWinGetTitle()/AutoItWinSetTitle()CodingDirToHTML5FileInstallrFileReadLastChars()GeoIP databaseGUI - Only Close ButtonGUI ExamplesGUICtrlDeleteImage()GUICtrlGetBkColor()GUICtrlGetStyle()GUIEventsGUIGetBkColor()Int_Parse() & Int_TryParse()IsISBN()LockFile()Mapping CtrlIDsOOP in AutoItParseHeadersToSciTE()PasswordValidPasteBinPosts Per DayPreExpandProtect GlobalsQueue()Resource UpdateResourcesExSciTE JumpSettings INISHELLHOOKShunting-YardSignature CreatorStack()Stopwatch()StringAddLF()/StringStripLF()StringEOLToCRLF()VSCROLLWM_COPYDATAMore Examples...

Updated: 22/04/2018

Link to comment
Share on other sites

On 5/5/2018 at 4:45 AM, Skysnake said:

Would you care to provide a use case?

It is useful if you use ConsoleWrite in your script to debug,
when it is uncompiled you can still use Scite to debug, and when it is compiled (and perhaps on another computer) you can see the ConsoleWrite text on the screen, and later in the log file.

(I know what you might be thinking...you can just compile as a console EXE...that is very true)

The reason I made it was because I read another post about the "AutoIt hidden Window" that, even though I had used AutoIt for years, i had never seen,
Once i saw it I wondered what it was all about and why it had a textbox with no text. So I took it as a challenge...

Link to comment
Share on other sites

On 4/30/2018 at 11:28 PM, guinness said:

Nice idea. I would however rename the internal window first to something unique (like adding the PID) and then get the title.

Sounds smart!

How do you rename the window before getting its title?
Is there another way to get its handle?

Link to comment
Share on other sites

@boomingranny check how I do it in 

 

UDF List:

 
_AdapterConnections()_AlwaysRun()_AppMon()_AppMonEx()_ArrayFilter/_ArrayReduce_BinaryBin()_CheckMsgBox()_CmdLineRaw()_ContextMenu()_ConvertLHWebColor()/_ConvertSHWebColor()_DesktopDimensions()_DisplayPassword()_DotNet_Load()/_DotNet_Unload()_Fibonacci()_FileCompare()_FileCompareContents()_FileNameByHandle()_FilePrefix/SRE()_FindInFile()_GetBackgroundColor()/_SetBackgroundColor()_GetConrolID()_GetCtrlClass()_GetDirectoryFormat()_GetDriveMediaType()_GetFilename()/_GetFilenameExt()_GetHardwareID()_GetIP()_GetIP_Country()_GetOSLanguage()_GetSavedSource()_GetStringSize()_GetSystemPaths()_GetURLImage()_GIFImage()_GoogleWeather()_GUICtrlCreateGroup()_GUICtrlListBox_CreateArray()_GUICtrlListView_CreateArray()_GUICtrlListView_SaveCSV()_GUICtrlListView_SaveHTML()_GUICtrlListView_SaveTxt()_GUICtrlListView_SaveXML()_GUICtrlMenu_Recent()_GUICtrlMenu_SetItemImage()_GUICtrlTreeView_CreateArray()_GUIDisable()_GUIImageList_SetIconFromHandle()_GUIRegisterMsg()_GUISetIcon()_Icon_Clear()/_Icon_Set()_IdleTime()_InetGet()_InetGetGUI()_InetGetProgress()_IPDetails()_IsFileOlder()_IsGUID()_IsHex()_IsPalindrome()_IsRegKey()_IsStringRegExp()_IsSystemDrive()_IsUPX()_IsValidType()_IsWebColor()_Language()_Log()_MicrosoftInternetConnectivity()_MSDNDataType()_PathFull/GetRelative/Split()_PathSplitEx()_PrintFromArray()_ProgressSetMarquee()_ReDim()_RockPaperScissors()/_RockPaperScissorsLizardSpock()_ScrollingCredits_SelfDelete()_SelfRename()_SelfUpdate()_SendTo()_ShellAll()_ShellFile()_ShellFolder()_SingletonHWID()_SingletonPID()_Startup()_StringCompact()_StringIsValid()_StringRegExpMetaCharacters()_StringReplaceWholeWord()_StringStripChars()_Temperature()_TrialPeriod()_UKToUSDate()/_USToUKDate()_WinAPI_Create_CTL_CODE()_WinAPI_CreateGUID()_WMIDateStringToDate()/_DateToWMIDateString()Au3 script parsingAutoIt SearchAutoIt3 PortableAutoIt3WrapperToPragmaAutoItWinGetTitle()/AutoItWinSetTitle()CodingDirToHTML5FileInstallrFileReadLastChars()GeoIP databaseGUI - Only Close ButtonGUI ExamplesGUICtrlDeleteImage()GUICtrlGetBkColor()GUICtrlGetStyle()GUIEventsGUIGetBkColor()Int_Parse() & Int_TryParse()IsISBN()LockFile()Mapping CtrlIDsOOP in AutoItParseHeadersToSciTE()PasswordValidPasteBinPosts Per DayPreExpandProtect GlobalsQueue()Resource UpdateResourcesExSciTE JumpSettings INISHELLHOOKShunting-YardSignature CreatorStack()Stopwatch()StringAddLF()/StringStripLF()StringEOLToCRLF()VSCROLLWM_COPYDATAMore Examples...

Updated: 22/04/2018

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

×
×
  • Create New...