Beginner needing help with SetTimer please.

Get help with using AutoHotkey (v1.1 and older) and its commands and hotkeys
Edd1
Posts: 4
Joined: 15 Feb 2018, 15:47

Beginner needing help with SetTimer please.

15 Feb 2018, 16:20

Hi Everybody,
I'm totally new to this and can't get my head around why my code snippet doesn't entirely work.

I have, by some of the scripts I've seen on the forum, a simple script that performs keystrokes to scan a document to make a PDF.

The problem I have is that the scanner sometimes takes longer to scan than I envisaged. My script is working perfectly as I've allowed 10 seconds for the scan to complete and carry on the rest of the operations required. This is ok until the scan takes longer than 10 seconds.

I noticed that the Scan dialogue changes from Scanner ready... to Scanning... and then to Scanner disabled... when it's finished.

I thought it would be simple to insert the following code into my script and when the scan was finished, however long it took, the rest of the script would execute.

This is my standalone test code:

Code: Select all

#Persistent
WinWait, Kodak Scanner Properties (Kodak i40 Scanner), 
IfWinNotActive, Kodak Scanner Properties (Kodak i40 Scanner), , WinActivate, Kodak Scanner Properties (Kodak i40 Scanner), 
WinWaitActive, Kodak Scanner Properties (Kodak i40 Scanner), 
Sleep, 100
DetectHiddenText Off

SetTimer, CheckText, 250
return
MsgBox %text%
ExitApp


CheckText:
WinGetText, text,Kodak Scanner Properties (Kodak i40 Scanner), Scanner disabled...
if (text <> "")
{
SetTimer, CheckText, Off
return
}
The problem is that the message box never gets displayed and the script never ends.

When I check to see what it's doing I can see that it detects when Scanner disabled... is displayed and executes the SetTime, CheckText, Off line but then just sits at return.

I'm sure I'm missing something simple but I just don't know what it is.

Any help would be appreciated.
User avatar
boiler
Posts: 16962
Joined: 21 Dec 2014, 02:44

Re: Beginner needing help with SetTimer please.

15 Feb 2018, 18:13

See the comments in CAPITALS in the code below.

Code: Select all

#Persistent
WinWait, Kodak Scanner Properties (Kodak i40 Scanner), 
IfWinNotActive, Kodak Scanner Properties (Kodak i40 Scanner), , ; <<<< YOU HAD THE FOLLOWING LINE ON THE SAME AS THIS ONE
	WinActivate, Kodak Scanner Properties (Kodak i40 Scanner), ; <<<< IT NEEDS TO BE ON A LINE OF ITS OWN (INDENTED TO SHOW RELATION TO THE IF)
WinWaitActive, Kodak Scanner Properties (Kodak i40 Scanner), 
Sleep, 100
DetectHiddenText Off

SetTimer, CheckText, 250
return ; <<< THE NEXT TWO LINES NEVER EXECUTE BECAUSE THIS LINE STOPS EXECUTION OF CODE IN THIS SECTION
MsgBox %text%
ExitApp


CheckText:
WinGetText, text,Kodak Scanner Properties (Kodak i40 Scanner), Scanner disabled...
if (text <> "")
{
SetTimer, CheckText, Off
return
}
Edd1
Posts: 4
Joined: 15 Feb 2018, 15:47

Re: Beginner needing help with SetTimer please.

16 Feb 2018, 10:34

Thanks for the reply Boiler.

Unfortunately removing the return does not solve the problem.

When I run the script with the scanner window open and either showing "Scanner disabled..." or not I get the message box displayed with nothing in it. When the message box is closed the script terminates. I thought that there was a way to keep "looking" at the scanner window to see when "Scanner disabled..." appears and act upon it when it does.

Am I wrong ?
BoBo
Posts: 6564
Joined: 13 May 2014, 17:15

Re: Beginner needing help with SetTimer please.

16 Feb 2018, 11:01

What about to ignore that "catch a popup"-approach and check for the expected outcome, the PDF file(s size) OR the process load of the scanner instead??
User avatar
boiler
Posts: 16962
Joined: 21 Dec 2014, 02:44

Re: Beginner needing help with SetTimer please.

16 Feb 2018, 12:06

@Edd1: That's because you're executing the MsgBox before the first time SetTimer had a chance to run. Put a loop around the MsgBox, and on subsequent displays of the MsgBox, it will show you the updated value of the text variable.
Edd1
Posts: 4
Joined: 15 Feb 2018, 15:47

Re: Beginner needing help with SetTimer please.

16 Feb 2018, 15:31

@BoBo: It's not a pop up window that appears if that's what you mean, it's a scanner dialouge which updates the status as things happen. Sorry, I don't understand what you mean by the process load of the scanner.

@boiler: The code that I have is just test code to prove that it works before I insert it into my main script. The MsgBox is only there to prove that that everything has run. I'm not expecting MsgBox to display until after the script has confirmed that the required text is being displayed in the scanner dialogue.

Please correct me if I'm wrong but I thought that SetTimer is used to run a subroutine at set intervals until it's told not to. In my head the process if as follows.

Some lines of code ...
....
of things to do
...
before i want
....
to check the text
....
in the scanner window...

Start SetTimer (start looping some subroutine code)
When SetTimer is cancelled by subroutine code

Carry on
....
with some more
....
code to finish the task...

Task finished - Exit

Put subroutine here outside main code body


Is this not correct?

Sorry for being so dumb.
User avatar
boiler
Posts: 16962
Joined: 21 Dec 2014, 02:44

Re: Beginner needing help with SetTimer please.

16 Feb 2018, 16:07

Yes, I know why you have the MsgBox is there, but I'm telling you that text will be blank when it executes as you currently have it because the first time the SetTimer executes in your script is about a quarter of a second after the MsgBox displays (so the text variable never gets filled before the MsgBox command executes). So if you don't want to add a loop, then add a Sleep, 1000 before the MsgBox, otherwise the variable text will be blank when the MsgBox displays.
Edd1
Posts: 4
Joined: 15 Feb 2018, 15:47

Re: Beginner needing help with SetTimer please.

17 Feb 2018, 16:26

Thanks everybody for your help. Everythuing is now sorted by using a loop.

Return to “Ask for Help (v1)”

Who is online

Users browsing this forum: Araphen, Google [Bot] and 273 guests