Reputation: 1507
The following is code that belongs to a compiled AHK script:
;@Ahk2Exe-ConsoleApp
FileAppend, Hello World, *
ExitApp
When the compiled exe is called from windows terminal, it will return "Hello World".
A few weeks ago I was asking around how I can return output to the terminal without compiling the script to an executable, it was suggested that I take a copy of the Autohotkey program (C:\Program Files\AutoHotkey\AutoHotkey.exe
) and give it the same name as my script and then call that executable at the command line.
So now I have something like this:
F:\AutoHotkey\AHKreturn.ahk
F:\AutoHotkey\AHKreturn.exe
When I call AHKreturn.exe
at the terminal with the .ahk
script file having something like:
Msgbox, Hello World
the message box shows up, but when I update it with something like this:
FileAppend, Hello, *
ExitApp
Nothing gets returned at the command line.
Some of my command line tools leverage AutoHotkey, currently I achieve this by compiling AutoHotkey scripts for each Cli tool. Its a pain having to work with these compiled AHK script as I cannot see the source code and I also have to recompile any time I want to make a slight change.
I want to do away with the compiling step, yet still be able to return output to the command line, I thought mikeys solution would be it but I am not so sure now.
Any help would be greatly appreciated!
Upvotes: 0
Views: 755
Reputation: 6489
You need to capture the script's output and then output it to your current command window. This can be done by e.g. simply piping to a for-each stdout printing loop.
Try it out in PowerShell:
& "C:\Program Files\AutoHotkey\AutoHotkeyU64.exe" .\script.ahk | ForEach-Object { Write-Output $_ }
To make this pleasant to work with, create a PowerShell script named ahk.ps1
:
param ([string] $AhkScriptPath) & "C:\Program Files\AutoHotkey\AutoHotkeyU64.exe" $AhkScriptPath | ForEach-Object { Write-Output $_ }
For cmd compatibility, create a cmd script named ahk.cmd
:
@powershell -noprofile -ex unrestricted "& 'ahk.ps1' %*;exit $lastexitcode"
(Of course, be sure to correct the AHK executable file path if needed))
Now you have two files ahk.ps1
and ahk.cmd
.
You can already try it out:
.\ahk.ps1 .\script.ahk
But of course you don't want .\
and .ps1
to be necessary, so move the files to a folder that's found in your PATH. Maybe some script folder you have? If not, just create any folder, place the files there, add that folder to your PATH and reboot your computer. If you're using Scoop, you can just quickly shim
the scripts and have them be in your PATH instantly.
Now that the scripts are found in your PATH, you can run ahk script.ahk
in any folder and via PowerShell or cmd, and printing to stdout will work perfectly fine.
Test script:
Logger := FileOpen("*", "w")
Logger.WriteLine("Hello!")
Logger.Write("Hi ")
MyFunc()
Logger.WriteLine("Bye now..")
return
MyFunc()
{
global Logger
if (false)
Logger.WriteLine("No, I'm not logging here")
Logger.WriteLine("again!")
}
FileOpen()
(docs) used intead of FileAppend
(docs) for a cleaner more modern script.
Output:
Upvotes: 2