6

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?

Stevoisiak
  • 23,794
  • 27
  • 122
  • 225
anthv123
  • 523
  • 1
  • 8
  • 20

1 Answers1

7

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.

Gary Hughes
  • 4,400
  • 1
  • 26
  • 40