call AHK script from .BAT or .PS1 and capture errorcode?

Get help with using AutoHotkey (v1.1 and older) and its commands and hotkeys
teknowledgist
Posts: 14
Joined: 22 Nov 2016, 10:20
Contact:

call AHK script from .BAT or .PS1 and capture errorcode?

15 Jul 2018, 23:01

I would like to put a timer in my AHK script that exits the script if it takes too long. I need to know if the script has exited this way or the "correct" way so my PoSh (or CMD) script can continue in the correct direction. The ExitApp documentation indicates that this should possible, but I cannot seem to get it to work.

test.ahk:

Code: Select all

SetTimer, TooLong, 5000

msgbox, Testing!
ExitApp

TooLong:
ExitApp, 666
When I do this in a CMD window:

Code: Select all

autohotkey test.ahk
echo %errorlevel%
I inevitably get "0" no matter if I click the msgbox (which should give a "0") or I wait and the script exists (which should return "666").

I also tried with PowerShell:

Code: Select all

$Proc = start-process -filepath autohotkey -argumentlist .\test.ahk -passthru
$handle = $proc.handle  # See:  https://stackoverflow.com/a/23797762
Write-Host $proc.exitcode
which also always gives me an exitcode of "0".

These appear to be giving me the errorcode that indicates the script was *started* successfully. How can I capture the completion error code I am exiting with?

Thanks.
User avatar
Alguimist
Posts: 428
Joined: 05 Oct 2015, 16:41
Contact:

Re: call AHK script from .BAT or .PS1 and capture errorcode?

15 Jul 2018, 23:49

You can get %ERRORLEVEL% in the command prompt if you start the command line with "cmd /c".

The exit code can also be retrieved this way:

Code: Select all

RunWait, Temp.ahk,, UseErrorLevel
MsgBox % ErrorLevel
tmplinshi
Posts: 1604
Joined: 01 Oct 2013, 14:57

Re: call AHK script from .BAT or .PS1 and capture errorcode?

16 Jul 2018, 07:49

Compile your script to console app using the new ahk2exe

Code: Select all

;@Ahk2Exe-ConsoleApp
FileAppend, Hello`n, *
ExitApp 1234
teknowledgist
Posts: 14
Joined: 22 Nov 2016, 10:20
Contact:

Re: call AHK script from .BAT or .PS1 and capture errorcode?

16 Jul 2018, 10:42

Alguimist wrote:You can get %ERRORLEVEL% in the command prompt if you start the command line with "cmd /c".
That doesn't work:

Code: Select all

c:>cmd /c autohotkey test.ahk && echo %errorlevel%
0
The "0" return is immediate before the AHK script times out. The %errorlevel% is for the success of AHK starting the script, not the results of the script.
Alguimist wrote: The exit code can also be retrieved this way:

Code: Select all

RunWait, Temp.ahk,, UseErrorLevel
MsgBox % ErrorLevel
But that is getting the exitcode within another AHK script! I need to know the exitcode outside of AHK, and I don't want to show a dialog. I have other things that will happen (outside of AHK) based on the AHK exit code (assuming I can get it!).

Thanks for the reply though.
teknowledgist
Posts: 14
Joined: 22 Nov 2016, 10:20
Contact:

Re: call AHK script from .BAT or .PS1 and capture errorcode?

16 Jul 2018, 10:44

tmplinshi wrote:Compile your script to console app using the new ahk2exe

Code: Select all

;@Ahk2Exe-ConsoleApp
FileAppend, Hello`n, *
ExitApp 1234
Thanks for the suggestion, but I must have a plain-text AHK script, not something compiled.
tmplinshi
Posts: 1604
Joined: 01 Oct 2013, 14:57

Re: call AHK script from .BAT or .PS1 and capture errorcode?

16 Jul 2018, 11:05

Well then you can change AutoHotkey.exe to console program
Spoiler
Edit: Doesn't have to do that. See Alguimist's reply below.
Last edited by tmplinshi on 16 Jul 2018, 16:01, edited 1 time in total.
User avatar
Alguimist
Posts: 428
Joined: 05 Oct 2015, 16:41
Contact:

Re: call AHK script from .BAT or .PS1 and capture errorcode?

16 Jul 2018, 15:27

teknowledgist wrote:That doesn't work:
Try again, this time with this batch file:

Code: Select all

@ECHO OFF
C:\Path\To\AutoHotkey.exe Test.ahk
ECHO %ERRORLEVEL%
PAUSE
teknowledgist wrote:But that is getting the exitcode within another AHK script! I need to know the exitcode outside of AHK
ErrorLevel can be passed as a command line parameter to a batch file:

Code: Select all

RunWait, Test.ahk,, UseErrorLevel
Run Outside.bat %ErrorLevel%
teknowledgist
Posts: 14
Joined: 22 Nov 2016, 10:20
Contact:

Re: call AHK script from .BAT or .PS1 and capture errorcode?

16 Jul 2018, 21:52

Doh! I'm eating crow for desert tonight!

All my calls to AutoHotKey were not full path calls because it is in the path (or so I thought). It turns out, what is in the path is a "shim" that redirects AHK calls to the correct path (AHK is installed via Chocolatey.)

The shim is blocking the exitcode (or passing it at the expense of a big, ugly, cmd window if I use the a particular switch).

Once I used the full path to AutoHotKey.exe as you instructed, it is working as promised.

Sorry for the noise and thanks for leading me in the right direction.
User avatar
KuroiLight
Posts: 327
Joined: 12 Apr 2015, 20:24
Contact:

Re: call AHK script from .BAT or .PS1 and capture errorcode?

16 Jul 2018, 22:13

just an after thought, for powershell you can do (Start-Process -FilePath './cmdtest.ahk' -PassThru -Wait).ExitCode and call cmd /c "cmdtest.ahk" for cmdprompt
Windows 10, Ryzen 1600, 16GB G.Skill DDR4, 8GB RX 480 | [MyScripts][MySublimeSettings] [Unlicense][MIT License]
01/24/18
[/color]

Return to “Ask for Help (v1)”

Who is online

Users browsing this forum: masterkomp, OrangeCat, ruespe and 389 guests