Page 1 of 1

[2.0-rc1] Usage of stdout in FileAppend?

Posted: 25 Nov 2022, 03:10
by hoppfrosch
Hi there,

Documentation on FileAppend for AHK 2.0-rc1 says:
Standard Output (stdout): Specifying an asterisk (*) for Filename causes Text to be sent to standard output (stdout)
How to use this?

Code: Select all

str := "Hello"
FileAppend(str, "*", "UTF-8")  ;FAILS with "INVALID HANDLE"
FileAppend(str, *, "UTF-8")    ;FAILS with "SYNTAX ERROR"
-----
BTW: How to link to V2-documentation use the BBCode [docs] in this forum?

Re: [2.0-rc1] Usage of stdout in FileAppend?

Posted: 25 Nov 2022, 03:19
by jNizM
BTW: How to link to V2-documentation use the BBCode [docs] in this forum?
Since the "docs" tag is only for ahk_1.1, the only way to do this is to post the complete url

But maybe @joedf can add another forum tag for v2 docs / url shorter (e.g. doc2 or something)

Re: [2.0-rc1] Usage of stdout in FileAppend?

Posted: 25 Nov 2022, 04:15
by gregster
You could use the editor's url tag, if you don't want to have the raw address in your post's visible text.
Of course, that is a tad more work.
Edit: I guess you already did that ;).

Re: [2.0-rc1] Usage of stdout in FileAppend?

Posted: 25 Nov 2022, 10:45
by joedf
@jNizM Done! :thumbup:

Code: Select all

[docs2]FileAppend[/docs2]
FileAppend

Re: [2.0-rc1] Usage of stdout in FileAppend?

Posted: 25 Nov 2022, 22:35
by lexikos
Specifying * directly in an expression is obviously a syntax error. The function wants a value ("Type: String"), and the way to express that value directly in an expression is with a quoted string. However, there are other ways to get such a value, like Chr() or reading from a file or command-line. I would suggest someone submits a pull request for the documentation if clarification is needed.

The reason you get "invalid handle" is that there is no valid handle. You can't write to stdout when there is no stdout. You probably never noticed with v1 because it reported errors by setting ErrorLevel, and who really ever checks ErrorLevel?

I'm not sure how you would expect to test the result if you aren't redirecting or capturing stdout, but I'm confident that FileAppend to "*" is working in v2.0-rc.1, because I rely on it heavily.

Re: [2.0-rc1] Usage of stdout in FileAppend?

Posted: 28 Nov 2022, 02:28
by hoppfrosch
My question results from porting a v1.1 script to V2.0., where the FileAppend Statements appears without any further context within a function.

I am confused - as I assumed stdout is always there and that therefore there is always a valid handle for stdout. As I understand, this is an incorrect assumption.

What does your answer mean in practice? How to get a valid handle for stdout? (haven't found anything in AHK-Docu)

Re: [2.0-rc1] Usage of stdout in FileAppend?

Posted: 28 Nov 2022, 04:44
by william_ahk
If the said script is not always ran in console, perhaps wrap it in try-catch. Otherwise, from the docs:
However, text sent to stdout will not appear at the command prompt it was launched from. This can be worked around by 1) compiling the script with the Ahk2Exe ConsoleApp directive, or 2) piping a script's output to another command or program. For example:

"%ProgramFiles%\AutoHotkey\AutoHotkey.exe" "My Script.ahk" |more

For /F "tokens=*" %L in ('""%ProgramFiles%\AutoHotkey\AutoHotkey.exe" "My Script .ahk""') do @Echo %L

Source: FileAppend - Syntax & Usage | AutoHotkey v2

Re: [2.0-rc1] Usage of stdout in FileAppend?

Posted: 28 Nov 2022, 18:13
by lexikos
GUI applications do not have a valid stdout handle by default; where would the output go? Console applications have it set to the console by default. In either case, it can be set by the parent process; as with redirecting or piping in Batch or at the command line.

FileAppend throws an error if there's no stdout. You can either handle the error using the standard means of error handling, or check whether you have stdout first (DllCall("GetStdHandle",...)).