Jump to content

Functions-behavior in for-loop


pumpaij
 Share

Recommended Posts

Hello!

I've written a script that automates certain geo-spatial workflows in a windows-programm.

This windows-program is a bit buggy and from time to time it fails to do the things the way it is supposed to.
However, most of the time it works great and my automatization with AutoIt does the job perfectly.

To be on the safe side I've decided to add a screenshot-routine that takes screenshots of the whole area I'm interested in so I can browse through them afterwards to see if the program messed up.

The problem is that my script doesn't give me a screenshot for each of my coordinate-pairs but only one for one whole loop.
I just don't understand it.

 

Here's the code:

; One array for the X- and one array for the Y-Coordinate. I use fixed numbers here - in my script those are dynamic in size.
Local $arrayXR[9] = [0,10,20,30,40,50,60,70,80]
Local $arrayYR[6] = [0,10,20,30,40,50]

;Get the sizes of those arrays
$iMaxXR = UBound($arrayXR)
$iMaxYR = UBound($arrayYR)

; Now I create two loops to get the coordinate-pairs (0|0),(0|10),(0|20),...
; after writing down each pair, I want a screenshot to be taken. Unfortunately, this doesn't work.
For $i = 0 to $iMaxXR - 1
    For $j = 0 to $iMaxYR -1
        ConsoleWrite($arrayXR[$i] & @LF)
        ConsoleWrite($arrayYR[$j] & @LF)
        ConsoleWrite(@CRLF)
        takescreenshot()
    Next
Next


; This is my function for taking screenshots
Func takescreenshot()
    Local $hBitmap

    ; Capture full screen
    $hBitmap = _ScreenCapture_Capture("")

    ; Save bitmap to file
    _ScreenCapture_SaveImage($screenshotpath & "screenshot" & $i & $timestamp & ".jpg", $hBitmap)
EndFunc


The output, as far as the coordinates are concerned, is fine:

0
0

0
10

0
20

0
30

0
40

0
50

10
0

10
10

and so on...

but I only get nine screenshots out of this. It takes one screenshot for each loop of the $arrayYR-coordinates.

I just don't get it - if I replace my screenshot-function with something like "ConsoleWrite(""Buuuuhaaa!") it works fine:

For $i = 0 to $iMaxXR - 1; subtract 1 from size to prevent an out of bounds error
    For $j = 0 to $iMaxYR -1
        ConsoleWrite($arrayXR[$i] & @LF)
        ConsoleWrite($arrayYR[$j] & @LF)
        ConsoleWrite("Buuuuhaaa!" & @LF)
        ConsoleWrite(@CRLF)
    Next
Next

Output:

0
0
Buuuuhjaaa!

0
10
Buuuuhjaaa!

0
20
Buuuuhjaaa!

0
30
Buuuuhjaaa!

0
40
Buuuuhjaaa!

0
50
Buuuuhjaaa!

10
0
Buuuuhjaaa!

10
10
Buuuuhjaaa!

10
20
Buuuuhjaaa!

and so on...


Thanks for reading!

Link to comment
Share on other sites

You only get nine screenshots because you're only using the $i variable for the screenshot name.

Quote

_ScreenCapture_SaveImage($screenshotpath & "screenshot" & $i & $timestamp & ".jpg", $hBitmap)

You're actually taking all of the screenshots, you're just overwriting 36 of them because of this. Use both the $i variable and $j variable in the file name, and this shouldn't happen. Technically it shouldn't happen because of the timestamp variable, but I have no way of knowing what that's set to because your code doesn't show that.

If I posted any code, assume that code was written using the latest release version unless stated otherwise. Also, if it doesn't work on XP I can't help with that because I don't have access to XP, and I'm not going to.
Give a programmer the correct code and he can do his work for a day. Teach a programmer to debug and he can do his work for a lifetime - by Chirag Gude
How to ask questions the smart way!

I hereby grant any person the right to use any code I post, that I am the original author of, on the autoitscript.com forums, unless I've specifically stated otherwise in the code or the thread post. If you do use my code all I ask, as a courtesy, is to make note of where you got it from.

Back up and restore Windows user files _Array.au3 - Modified array functions that include support for 2D arrays.  -  ColorChooser - An add-on for SciTE that pops up a color dialog so you can select and paste a color code into a script.  -  Customizable Splashscreen GUI w/Progress Bar - Create a custom "splash screen" GUI with a progress bar and custom label.  -  _FileGetProperty - Retrieve the properties of a file  -  SciTE Toolbar - A toolbar demo for use with the SciTE editor  -  GUIRegisterMsg demo - Demo script to show how to use the Windows messages to interact with controls and your GUI.  -   Latin Square password generator

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...