Error handling and stopping a script

Get help with using AutoHotkey (v1.1 and older) and its commands and hotkeys
Chessel
Posts: 12
Joined: 01 Jan 2022, 04:27

Error handling and stopping a script

Post by Chessel » 29 Jan 2022, 08:55

Hi, newbie questions.
I want to use ImageSearch but I know it is a good idea to put some error handling in my AHK script. For example to check the image file I want to find exists.
Do I put this error handling at the top of my script before coding for any hot keys? Or do I need to put the error handling inside every hot key bit of code?
Another way of asking this question is, if a script is active, does it receive every single keystroke and execute from the top every time?

My second question is how do I remove a script from memory so it isn't running anymore? I have a hot key of Esc::exitapp but I am thinking there should be a way to kill a script without having to do this? Is there? Or is this snippet the best approach?
Many thanks for any help,
Chessel

User avatar
mikeyww
Posts: 26936
Joined: 09 Sep 2014, 18:38

Re: Error handling and stopping a script

Post by mikeyww » 29 Jan 2022, 09:00

See Auto-execute. When you run the script, it starts at the top and executes each line, once, until Return. As an event-driven program, if the script is persistent, it will then stay resident until hotkeys are pressed, etc. The auto-execute section will not repeat itself. Immediately after an ImageSearch, you can check or act upon the ErrorLevel.
ErrorLevel is set to 0 if the image was found in the specified region, 1 if it was not found, or 2 if there was a problem that prevented the command from conducting the search (such as failure to open the image file or a badly formatted option).
You can issue ExitApp anywhere in your script, where a subroutine or function is executing. You can also kill the task from an external program if you like. You can also exit manually via the script's tray icon.

Chessel
Posts: 12
Joined: 01 Jan 2022, 04:27

Re: Error handling and stopping a script

Post by Chessel » 29 Jan 2022, 09:41

Hi Mike,
Thanks for the quick reply.
If I have this 3 line script...

Code: Select all

a::MsgBox Hello
Esc::ExitApp  ; Exit script with Escape key
return
And I run it. Whenever I hit 'a' the msgbox appears. If I then hit escape, 'a' has no effect.
After running the script I couldn't see my script in my system tray and I couldn't see it in Task Manager either. Should I be able to?
I am blind and using the NVDA screen reader but access to the system tray and task manager is pretty good so I don't think I'm being stopped from seeing my script due to accessibility problems.
If you could clarify that would be great but I guess it isn't too important as I just put the exitapp command in.
I need to go improve my script rather than worrying about this. I maybe posting a query about ImageSearch and how it is affected by different screen resolutions later.
Many thanks,
Chessel

User avatar
mikeyww
Posts: 26936
Joined: 09 Sep 2014, 18:38

Re: Error handling and stopping a script

Post by mikeyww » 29 Jan 2022, 10:01

Try the following with the latest version of AHK.

Code: Select all

a::MsgBox, Hello
Esc::ExitApp
This does run in the tray as a green icon with the letter "H" in white, though it could be hidden in the overflow area. Pressing Esc does exit this script as noted in the script's hotkey for Esc, so pressing A after that would just do whatever that key usually does.

An uncompiled script will appear in Task Manager as AutoHotkey.exe.

Post Reply

Return to “Ask for Help (v1)”