Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I return a value from an AutoHotkey script?

I need to call an AutoHotkey script that will return a value.

For example, something like this:

return_val = Shell("AutoHotKey.exe script.ahk")

My script looks like this:

IfExists, filename
     return 1
Else
     return 0

I get an error telling me I can't have a value in the terminating return statement. I have also tried using the Exit statement instead of return.

How can I return a value from an AutoHotkey script?

like image 520
anthv123 Avatar asked Oct 17 '11 21:10

anthv123


1 Answers

To return an exit code you'll want to call ExitApp along with your desired code. Use IfExist to determine if the file exists. This means the script that you call should look like this:

IfExist, c:\test.txt
    ExitApp, 1
Else
    ExitApp 0

When calling the script you should use RunWait and pass it the UseErrorLevel parameter. This will set the variable ErrorLevel to the exit code of the called process if it launches correctly or the text ERROR if the process cannot be started.

RunWait, C:\Program Files (x86)\AutoHotkey\AutoHotkey.exe "C:\script.ahk",, UseErrorLevel
MsgBox %ErrorLevel%

In this example the message box will display '1' if the file exists or '0' if it doesn't.

like image 162
Gary Hughes Avatar answered Oct 12 '22 10:10

Gary Hughes