How to 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 am getting an error indicating that I cannot have a value in the final return statement. I also tried using Exit instead of returning.

How do I return a value from an AutoHotkey script?

+6
source share
1 answer

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

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

When calling the script, you must use RunWait and pass the UseErrorLevel parameter to UseErrorLevel . This will set the ErrorLevel variable to the exit code of the called process if it starts 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 is not.

+6
source

Source: https://habr.com/ru/post/899493/


All Articles