[2.0-rc1] Usage of stdout in FileAppend?

Get help with using AutoHotkey (v2 or newer) and its commands and hotkeys
User avatar
hoppfrosch
Posts: 443
Joined: 07 Oct 2013, 04:05
Location: Rhine-Maine-Area, Hesse, Germany
Contact:

[2.0-rc1] Usage of stdout in FileAppend?

Post by hoppfrosch » 25 Nov 2022, 03:10

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?
Last edited by hoppfrosch on 25 Nov 2022, 03:22, edited 1 time in total.

User avatar
jNizM
Posts: 3183
Joined: 30 Sep 2013, 01:33
Contact:

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

Post by jNizM » 25 Nov 2022, 03:19

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)
[AHK] v2.0.5 | [WIN] 11 Pro (Version 22H2) | [GitHub] Profile

gregster
Posts: 8916
Joined: 30 Sep 2013, 06:48

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

Post by gregster » 25 Nov 2022, 04:15

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 ;).

User avatar
joedf
Posts: 8940
Joined: 29 Sep 2013, 17:08
Location: Canada
Contact:

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

Post by joedf » 25 Nov 2022, 10:45

@jNizM Done! :thumbup:

Code: Select all

[docs2]FileAppend[/docs2]
FileAppend
Image Image Image Image Image
Windows 10 x64 Professional, Intel i5-8500, NVIDIA GTX 1060 6GB, 2x16GB Kingston FURY Beast - DDR4 3200 MHz | [About Me] | [About the AHK Foundation] | [Courses on AutoHotkey]
[ASPDM - StdLib Distribution] | [Qonsole - Quake-like console emulator] | [LibCon - Autohotkey Console Library]

lexikos
Posts: 9553
Joined: 30 Sep 2013, 04:07
Contact:

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

Post by lexikos » 25 Nov 2022, 22:35

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.

User avatar
hoppfrosch
Posts: 443
Joined: 07 Oct 2013, 04:05
Location: Rhine-Maine-Area, Hesse, Germany
Contact:

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

Post by hoppfrosch » 28 Nov 2022, 02:28

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)

william_ahk
Posts: 481
Joined: 03 Dec 2018, 20:02

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

Post by william_ahk » 28 Nov 2022, 04:44

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

lexikos
Posts: 9553
Joined: 30 Sep 2013, 04:07
Contact:

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

Post by lexikos » 28 Nov 2022, 18:13

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",...)).

Post Reply

Return to “Ask for Help (v2)”