Use the Sleep Command to Debug Intermittent Routines
Last night, I received the following comment from Thom Blake about last week’s blog “Auto-Capitalize the First Letter of Sentences” prompting this Quick Tip about adding the Sleep command at critical points in certain types of AutoHotkey routines:
Jack
Great script but I seem to have a problem with the capitalize a word function. It only works intermittently. The message box says “No text selected!” when clearly there is.
Thanks, Thom!
This is a needed reminder about a common problem when using the Send command with the Clipboard routine. Sometimes, for whatever reason, certain commands execute before the completion of the last action. In this case, AutoHotkey attempts to copy text to the Windows Clipboard before the SendInput text selection action completes:
CapsLock::
OldClipboard := ClipboardAll
Clipboard := "" ; Clears the Clipboard
SendInput ^+{left} ; Selects word to the left
SendInput ^c ; Copy text
ClipWait 0 ;pause for Clipboard data
If ErrorLevel
{
MsgBox, No text selected!
}
StringUpper, Clipboard, Clipboard, T ; Cap first char (Title)
SendInput %Clipboard%
Clipboard := OldClipboard
Return
The ClipWait, 0 command pauses 500 milliseconds while waiting for something to appear in the Clipboard. However, if the previous command selects the word too slowly, the copy command (SendInput ^c) runs right by it. The MsgBox window pops up saying “No text selected!”
Generally, the solution involves pausing the script using the Sleep command at critical points:
CapsLock:: OldClipboard := ClipboardAll Clipboard := "" ; Clears the Clipboard SendInput ^+{left} ; Selects word to the left Sleep, 100 SendInput ^c ; Copy text ClipWait 0 ;pause for Clipboard data If ErrorLevel { MsgBox, No text selected! } StringUpper, Clipboard, Clipboard, T ; Cap first char (Title) Sleep, 100 SendInput %Clipboard% Clipboard := OldClipboard Return
If the MsgBox pops up, then the first SendInput command did not finish the word selection. If it appears that the Hotkey doesn’t do anything, then possibly the replacement command (SendInput %Clipboard%) executed before the capitalization occurred in the Clipboard. Each may require a slight wait time.
When I first write these types of routines, I don’t add Sleep commands unless I see a problem. Whether a routine needs the extra pauses depends upon the computer, the programs and whatever else Windows might have running in the background. Many of my scripts do include the Sleep command—although not consistently.
Some people suggest always including the Sleep command in every such routine. (They might be right.) But I’ve seen some scripts which include the hesitation on almost every other line. That adds a lot of slow down. Also, how long the script should Sleep is a guess which may depend upon your operating environment. I’ve seen people use anywhere from 50 milliseconds to 500 milliseconds (one-half of a second). (Who knows the optimum value?) However, putting pauses in at the correct place can add reliability and robustness to your scrips—even if they cause you to wait a few hundred more milliseconds.
Over the years, I’ve written about this issue many times. This problem comes up so often in these types of AutoHotkey routines, that I felt it was worthwhile to post this reminder. (Often enough, I forget to at least mention it when introducing new routines.) If you do much with the Send command, you will run into this again. When you do, consider whether one AutoHotkey command might have outrun another. Test whether the Sleep command solves the problem. You might even have an intermittent routine running on your computer right now which would benefit from this tip.
This post was proofread by Grammarly
(Any other mistakes are all mine.)
(Full disclosure: If you sign up for a free Grammarly account, I get 20¢. I use the spelling/grammar checking service all the time, but, then again, I write a lot more than most people. I recommend Grammarly because it works and it’s free.)
Find my AutoHotkey books at ComputorEdge E-Books!
Hi Jack,
great post again. By the way, the same story is shown here:
Kind regards, mslonik
LikeLike
[…] Quick Tip: Using the Sleep Command with Clipboard Routines […]
LikeLike