Autohotkey shortcut in batch file.

Get help with using AutoHotkey (v1.1 and older) and its commands and hotkeys
Mr Qwerky
Posts: 12
Joined: 20 Mar 2023, 18:35

Autohotkey shortcut in batch file.

Post by Mr Qwerky » 20 Mar 2023, 18:52

Hi. I was registered here several years ago, but it appears that my registration is no longer valid, so I created a new account.

I am very inexperienced with AHK, but have for several years had an AHK script that I created, run at system startup. It is basically a list of hotkeys that I can system wide at any time. One of the hotkeys is to start Thunderbird, and send it "2", which sets its focus to the thread pane:

Code: Select all

; Meta-T runs Thunderbird.
#T::Run C:\Applications\Communications\Thunderbird\thunderbird.exe
WinWait Thunderbird ahk_class MozillaWindowClass
If ErrorLevel = 0
{
  WinActivate
  Send 2
}
Return
My question is this: is there any way that I can use such a shortcut in a normal batch file? Well, actually I mean a .btm file, which is a Take Command batch file that incorporates all normal Windows batch file features.

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

Re: Autohotkey shortcut in batch file.

Post by mikeyww » 20 Mar 2023, 19:38

Welcome (back) to this AutoHotkey forum!

The first question is whether you can debug your script so that it works. Your Run command is currently immediately followed by an implicit Return, which you can verify by running the script and seeing that it does not work. If you move that command to the next line (below the hotkey), it should fix that issue.

A hotkey has no role inside a batch file, as I understand it, because the syntax would not match, but you can easily use a batch file to run the script.

Code: Select all

#Requires AutoHotkey v1.1.33
#t::MsgBox C:\Applications\Communications\Thunderbird\thunderbird.exe
MsgBox Nothing here or below ever executes! Really!
WinWait Thunderbird ahk_class MozillaWindowClass
If ErrorLevel = 0
{
  WinActivate
  Send 2
}
FileDelete, % myEntireHardDrive
MsgBox Zapped all of it!
Reload
ExitApp
Run Away
Return
Explained: Command line

Incidentally, AHK v2 emerged while you were away!

:)

Mr Qwerky
Posts: 12
Joined: 20 Mar 2023, 18:35

Re: Autohotkey shortcut in batch file.

Post by Mr Qwerky » 21 Mar 2023, 13:31

mikeyww wrote:
20 Mar 2023, 19:38
Welcome (back) to this AutoHotkey forum!
Thanks! :D And thanks for the reply.
The first question is whether you can debug your script so that it works. Your Run command is currently immediately followed by an implicit Return, which you can verify by running the script and seeing that it does not work.
Here I am completely lost. In the reference that you linked, I read that the top section of a script is called the "Auto-Execute" section, and that the first hotkey has the same effect as 'return'. Is that what you are referring to? If so, sorry that I wasn't clear enough in the original post, but my script, which runs at system start-up, contains a list of many hotkeys, so this particular one is not the first. When I use this hotkey, it does launch Thunderbird.
If you move that command to the next line (below the hotkey), it should fix that issue.
You are saying to move the 'run' command below the hotkey?

Code: Select all

#T::
Run C:\Applications\Communications\Thunderbird\thunderbird.exe
A hotkey has no role inside a batch file, as I understand it, because the syntax would not match, but you can easily use a batch file to run the script.
So the batch file would call a script which would contain just that single hotkey/function, but there is no way from the batch file to trigger that specific hotkey within the multi-hotkey script?

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

Re: Autohotkey shortcut in batch file.

Post by mikeyww » 21 Mar 2023, 14:05

Your original script runs Thunderbird but does not send 2. That is what I meant: your original script does not actually work. You could confirm that simply by running it.

Yes, you moved the command down, to the right place.

Yes, your batch file can run any script. Your batch file might be able to trigger a hotkey, but I don't see why you'd want to attempt to do that. You could just put the commands that you want into the batch file.

If needed, an AHK script can receive and process its own command-line arguments, too. These could be passed from another program. This is explained on the page that I have already cited. Command-line parameters can be used to do anything. The script would identify any command-line parameters, and then act accordingly.

Mr Qwerky
Posts: 12
Joined: 20 Mar 2023, 18:35

Re: Autohotkey shortcut in batch file.

Post by Mr Qwerky » 21 Mar 2023, 14:48

mikeyww wrote:
21 Mar 2023, 14:05
Your original script runs Thunderbird but does not send 2. That is what I meant: your original script does not actually work. You could confirm that simply by running it.

Yes, you moved the command down, to the right place.
Okay, thank you. The command is now in the right place, so it is not doing an implicit return. But, it is still not sending the 2?
Yes, your batch file can run any script. Your batch file might be able to trigger a hotkey, but I don't see why you'd want to attempt to do that. You could just put the commands that you want into the batch file.

If needed, an AHK script can receive and process its own command-line arguments, too. These could be passed from another program. This is explained on the page that I have already cited. Command-line parameters can be used to do anything. The script would identify any command-line parameters, and then act accordingly.
So, I have this AHK script which runs at start-up, containing a list of hotkeys, which I can use system-wide at any time. I also have a batch file which can be run at any time, which starts a group of applications. However, I don't think the batch file can send keystrokes to programs, which is where AHK comes in. So probably the simplest would be to have the batch file call a simple AHK script, that has the sole purpose of sending that keystroke (once I get that part working)?

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

Re: Autohotkey shortcut in batch file.

Post by mikeyww » 21 Mar 2023, 16:40

Two things:

1. If you want your script to execute code when it is first run, put that code at the top of your script, in the auto-execute section, instead of (or in addition to) a hotkey. Alternatively, you can have the script use the Gosub command at the top of the script, to call a labeled subroutine. That fixes your issue about triggering the hotkey. The script can execute the code instantly when the script is run.

2. If the 2 is not sent, do some troubleshooting: what window is actually active, according to the script? Does the Send line ever execute? If so, how do you know? What is the actual ErrorLevel value before that (in the conditional statement)? You can add lines to the script to answer these questions. Use the script itself to help you understand what is happening, or use some of the AHK debugging tools.

PS, since you have decided not to use a timeout with WinWait, what purpose does checking the ErrorLevel serve? What are its possible values?

In some cases, a window can exist before it is ready to receive user input. A sleep before sending keys is usually the fix in those situations.

Mr Qwerky
Posts: 12
Joined: 20 Mar 2023, 18:35

Re: Autohotkey shortcut in batch file.

Post by Mr Qwerky » 21 Mar 2023, 17:13

mikeyww wrote:
21 Mar 2023, 16:40
Two things:

1. If you want your script to execute code when it is first run, put that code at the top of your script, in the auto-execute section, instead of (or in addition to) a hotkey. Alternatively, you can have the script use the Gosub command at the top of the script, to call a labeled subroutine. That fixes your issue about triggering the hotkey. The script can execute the code instantly when the script is run.

2. If the 2 is not sent, do some troubleshooting: what window is actually active, according to the script? Does the Send line ever execute? If so, how do you know? What is the actual ErrorLevel value before that (in the conditional statement)? You can add lines to the script to answer these questions. Use the script itself to help you understand what is happening, or use some of the AHK debugging tools.

PS, since you have decided not to use a timeout with WinWait, what purpose does checking the ErrorLevel serve? What are its possible values?

In some cases, a window can exist before it is ready to receive user input. A sleep before sending keys is usually the fix in those situations.
Thank you for all the help. After some experimentation, the following now works:

Code: Select all

; Meta-T runs Thunderbird.
#T::
Run C:\Applications\Communications\Thunderbird\thunderbird.exe
WinWait, Thunderbird, , 3
If ErrorLevel = 0
{
  WinActivate
  Send 2
}
Return
It was either changing the title (removing the class part), with "setTitleMatchMode 2" (which it also was before), or else adding the wait to the WinWait. In the batch file, I added a Take Command KEYSTACK command to send the "2", with a wait of about three seconds. Both now work, and I can see the Thunderbird window load, then elements fill in, then the thread pane become selected, so it definitely needed some time to get ready to receive that "2". You hit the nail on the head! :lol:

Post Reply

Return to “Ask for Help (v1)”