简体   繁体   中英

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?

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM