Jump to content
Sign in to follow this  
bradsmithsite

AutoIt #UseConsole

Recommended Posts

bradsmithsite

Hi.

Can one of the developers please add a #UseConsole condition (or whatever) that forces a call to the Windows AllocConsole API for running console applications without having to compile the code everytime.

Also, if a #UseConsole is specified, running a Test.au3 file from the command prompt should attach to the existing console instead of opening a new console, just like a AU3 compiled as "Console".

Really driving me crazy to debug.

Can one of the developers add it?

Edited by bradsmithsite

Share this post


Link to post
Share on other sites
Mat

Not quite as easy as that I'm afraid, it would require changes to SciTE as well. I am yet to find a way to allocate a console and for it to work correctly when running from SciTE, that's even with messing around creating a new child process with various CreateProcess flags. For some reason the SciTE output window just swallows all the streams :(

The easiest thing to do is to add AutoIt3.exe to PATH (I usually keep a copy in C:binaut.exe with some other tools, and add C:bin to PATH), then you can keep cmd open and when you want to test just type "aut MyScript.au3".

Share this post


Link to post
Share on other sites
bradsmithsite

All new feature requests should be submitted via the Bug Tracker link at the top of the forum.

 

Thanks for the info. Wasn't sure where to post.

Share this post


Link to post
Share on other sites
bradsmithsite

Not quite as easy as that I'm afraid, it would require changes to SciTE as well. I am yet to find a way to allocate a console and for it to work correctly when running from SciTE, that's even with messing around creating a new child process with various CreateProcess flags. For some reason the SciTE output window just swallows all the streams :(

The easiest thing to do is to add AutoIt3.exe to PATH (I usually keep a copy in C:binaut.exe with some other tools, and add C:bin to PATH), then you can keep cmd open and when you want to test just type "aut MyScript.au3".

 

AutoIt3.exe is a GUI application so a console isn't allocated for it when it is runs. All console output (i.e. ConsoleWrite) is ignored.

We need a console AutoIt3Console.exe that can be called from the command prompt. .au3c files (or whatever) could be registered to run AutoIt3Console so that you could natively run .au3c files from a command prompt by simply typing "clean_temp.au3c".

I think using AutoIt to make command line programs would make AutoIt way more useful for creating administrative and miscellaneous utilities.

Can't believe a AutoIt3 console interpreter isn't available!

Will file a feature request.

AutoIt3 isn't made for GUIs. I mean, it's difficult to use AutoIt to create a proper GUI application that's event driven. Message boxes aren't event driven from a coding point of view:

Display message box, wait for input (input box), process, show message.

Same as console write, wait for input, process and write console.

First C/C++ program "Hello World" is usually a console program!

Are most AutoIt3 scripts event GUI applications? Mine aren't. Most would run better as console apps, hence why I believe the feature should be added.

Share this post


Link to post
Share on other sites
Mat

Then just compile a blank application as AutoItConsole.au3.

Edit: Just tried to get back to the system I used to have an can't get it to work. I'll have to see what I'm doing differently.

Edited by Mat

Share this post


Link to post
Share on other sites
MHz

bradsmithsite,
Your feature request may take some time to process. So, this idea I suggest below is something for now. I am not sure how 32 bit and 64 bit are going to be handled. I have just considered 32 bit for now.

1. Compile the AutoIt3 code as a console executable and name it AutoIt3Cui.exe
2. Place in AutoIt3 directory
3. Double click it to register

; compile this script as a cui executable and name it as AutoIt3Cui.exe and place it in the AutoIt3 directory so the std include folder is found
; you can then double click on it to register the file type and association. It will be ready to use once done.

; this block handles parameters passed if /AutoIt3ExecuteScript was not used and runs them.
; It is more efficient to use /AutoIt3ExecuteScript to run @AutoItExe initially so this script is not loaded into memory.
; file association uses /AutoIt3ExecuteScript by default so this script will not be loaded into memory.
If $CMDLINE[0] Then
    Global $parameters
    ; loop through command line parameters
    For $1 = 1 To UBound($CMDLINE) -1
        ; skip unwanted parameters
        Switch $CMDLINE[$1]
            Case '/AutoIt3ExecuteScript', '/ErrorStdOut'
                ContinueLoop
        EndSwitch
        ; collect parameters and add quotes if needed
        If StringInStr($CMDLINE[$1], ' ') Then
            $parameters &= '"' & $CMDLINE[$1] & '" '
        Else
            $parameters &= $CMDLINE[$1] & ' '
        EndIf
    Next
    ; run the command
    Run('"' & @AutoItExe & '" /ErrorStdOut /AutoIt3ExecuteScript ' & $parameters)
    If @error Then ConsoleWriteError('Unable to run the command' & @CRLF)
    Exit
EndIf

; if no parameters passed then we will handle registering or unregistering file type and associations.
; Or perhaps just selecting a script to run.

Global $assoc, $return_code, $parameters

$return_code = 0

; setup au3c
$assoc = RegRead('HKCU\Software\Classes\.au3c', '')
If Not $assoc And RegWrite('HKCU\Software\Classes\.au3c', '', 'REG_SZ', 'AutoIt3ScriptCui') Then
    $return_code += 1
    ConsoleWrite('Associated .au3c to AutoIt3ScriptCui' & @CRLF)
EndIf

; setup AutoIt3ScriptCui
RegRead('HKCU\Software\Classes\AutoIt3ScriptCui', '')
If @error > 0 And RegWrite('HKCU\Software\Classes\AutoIt3ScriptCui\Shell\Open\Command', '', 'REG_SZ', '"' & @ScriptFullPath & '" /ErrorStdOut /AutoIt3ExecuteScript "%1"') Then
    $return_code += 2
    ConsoleWrite('Created AutoIt3ScriptCui command string' & @CRLF)
EndIf

; show registered message or give option to unregister or else select a file to run
If $return_code Then
    ; show registered message
    MsgBox(0, @ScriptName, 'Registered .au3c file type with return code ' & $return_code)
ElseIf MsgBox(0x24, @ScriptName, 'Do you want to unregister .au3c file type?') = 6 Then; ID_YES
    ; unregister
    RegDelete('HKCU\Software\Classes\.au3c')
    RegDelete('HKCU\Software\Classes\AutoIt3ScriptCui')
Else ; select a file to run
    $parameters = FileOpenDialog('Run Script', '', 'AutoIt3 (*.au3;*.au3c)|All (*.*)')
    If Not @error And $parameters Then
        If StringInStr($parameters, ' ') Then $parameters = '"' & $parameters & '"'
        Run('"' & @AutoItExe & '" /ErrorStdOut /AutoIt3ExecuteScript ' & $parameters)
        If @error Then ConsoleWriteError('Unable to run the command' & @CRLF)
        Exit
    EndIf
EndIf

4. Add the text below into User Options in Scite4AutoIt3

# au3c used type below. You can setup a compile to console exe etc.
autoit3cui=$(SciteDefaultHome)\..\AutoIt3Cui.exe
command.go.*.au3c="$(autoit3cui)" /ErrorStdOut /AutoIt3ExecuteScript "$(FilePath)" $(1) $(2) $(3) $(4)
command.go.subsystem.*.au3c=2
au3=*.au3;*.au3c

The Go option in Scite4AutoIt3 should now run a .au3c file with AutoIt3Cui.exe.
If you open CMD in a directory with e.g. test.au3c in it, then type test.au3c and press return then you should get the script to run in the CMD window.
Double click test.au3c in Explorer should run test.au3c in a CMD window.

A temporary fix or perhaps a full solution if it could be developed more. i consider that I experienced your pain with testing this (compile...test...compile...test...) as a console app.

Edit:

Added missing /ErrorStdOut to User Options text

Edited by MHz

Share this post


Link to post
Share on other sites
Mat

Ok, I've figured out how I used to do it.

The test script (called test.au3) looks something like (uses my Console.au3):

#include "Console.au3"

_Console_Attach()

_Console_Write("Please enter your name: ")

$s = _Console_Read()
_Console_Write("Hello, " & $s & "!" & @LF)

I need to play around a bit more, because there are some cases where you want to use _Console_Alloc, others where you want to use _Console_Attach and also when you want to use ConsoleWrite rather than the winapi WriteConsole (there are differences for some reason).

Anyway, I then just run cmd /k AutoIt3.exe test.au3 from a console. For some reason you need the cmd /k, as running AutoIt3 directly causes cmd to get itself in a complete mess.

The alternative is having a test script like:

#include "Console.au3"

_Console_Alloc()

_Console_Write("Please enter your name: ")

$s = _Console_Read()
_Console_Write("Hello, " & $s & "!" & @LF)

_Console_Pause()
_Console_Free()

And then just running it normally (but not from SciTE).

 

Anyway, I will test out what MHz has got, as there does need to be an easier way to do it. It's one of the reasons I gave up trying to write console examples.

Share this post


Link to post
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
Sign in to follow this  

×