Jump to content

Variables and Loops


Recommended Posts

I'm pretty new at scripting so I'm just trying to wrap my head around some functions.

I'm trying to figure out how to accomplish a simple script that when executed does something a number of times using two global variables which I can easily change should I want to. The problem arises when I want the first loop to be slightly different.

$current = 0
$target = 5

If $current = 0 Then
    Call ("First")
    $current = $current + 1
ElseIf $current < $target Then
    Call ("Loop")
    $current = $current + 1
Else
    Call ("Finish")
EndIf
    
Func First ()
    MsgBox(4096,"","First Loop")
    Sleep (15000)
EndFunc

Func Loop ()
    MsgBox(4096,"","Loop")
    Sleep (15000)
EndFunc

Func Finish ()
    Sleep (2000)
    Beep(500, 1000)
    Exit
EndFunc

I started playing around with global variables with the following earlier variant but I couldn't figure out how to make an exception for the first loop.

$current = 0
$target = 5

Do
  Call ("Loop")
  $current = $current + 1
Until $current = $target

Func Loop ()
    MsgBox(4096,"","Loop")
    Sleep (5000)
    
EndFunc

Sleep (2000)
Beep(500, 1000)

Exit

I've been looking in the help pages and trying to find threads about this but I just can't get it to work the way I expect it to.

Any pointers?

Link to comment
Share on other sites

Something like this:

$iNumberOfLoops = 5
For $iIndex = 1 to $iNumberOfLoops
  If $iIndex = 1 Then
    ; do something for the first time
  Else
    ; do something else for all other loops
  EndIf
Next

Edited by water

My UDFs and Tutorials:

Spoiler

UDFs:
Active Directory (NEW 2022-02-19 - Version 1.6.1.0) - Download - General Help & Support - Example Scripts - Wiki
ExcelChart (2017-07-21 - Version 0.4.0.1) - Download - General Help & Support - Example Scripts
OutlookEX (2021-11-16 - Version 1.7.0.0) - Download - General Help & Support - Example Scripts - Wiki
OutlookEX_GUI (2021-04-13 - Version 1.4.0.0) - Download
Outlook Tools (2019-07-22 - Version 0.6.0.0) - Download - General Help & Support - Wiki
PowerPoint (2021-08-31 - Version 1.5.0.0) - Download - General Help & Support - Example Scripts - Wiki
Task Scheduler (NEW 2022-07-28 - Version 1.6.0.1) - Download - General Help & Support - Wiki

Standard UDFs:
Excel - Example Scripts - Wiki
Word - Wiki

Tutorials:
ADO - Wiki
WebDriver - Wiki

 

Link to comment
Share on other sites

Ok, new question, related to my first question. I've made a small GUI with an input field and a checkbox. Depending on what the input in the field is and whether or not the checkbox is checked, I want the script to execute slightly different. It works as intended as I've written it but I'm curious as to whether there is any way to trim this code down or organize it in any other way?

Func _btn_ok()
; ### No amount selected or 0
If GUICtrlRead ($Input1) < 1 Then
  $sText = "Cannot execute 0"
  _ExtMsgBox (16,0, $sTitle, $sText,0,@DesktopWidth -270, @DesktopHeight -200)
Else
  Select
   Case GUICtrlRead ($Checkbox) = $GUI_CHECKED
    Select
; ### Checkbox checked, 1 loop
    Case GUICtrlRead ($Input1) =1
     $sText = "Do you want to execute " & GUICtrlRead ($Input1) & " time?" & @CRLF & "Checkbox checked!"
     _ExtMsgBox (32,0, $sTitle, $sText,0,@DesktopWidth -270, @DesktopHeight -200)
; ### Checkbox checked, more than 1 loop
    Case Else
     $sText = "Do you want to execute " & GUICtrlRead ($Input1) & " times?" & @CRLF & "Checkbox checked!"
     _ExtMsgBox (32,0, $sTitle, $sText,0,@DesktopWidth -270, @DesktopHeight -200)
    EndSelect
   Case Else
    Select
; ### Normal loop, only once
    Case GUICtrlRead ($Input1) =1
     $sText = "Do you want to execute " & GUICtrlRead ($Input1) & " time?"
     _ExtMsgBox (32,0, $sTitle, $sText,0,@DesktopWidth -270, @DesktopHeight -200)
; ### Normal loop, more than once
    Case Else
     $sText = "Do you want to execute " & GUICtrlRead ($Input1) & " times?"
     _ExtMsgBox (32,0, $sTitle, $sText,0,@DesktopWidth -270, @DesktopHeight -200)
    EndSelect
   EndSelect
EndIf
EndFunc

The script only really changes depending on the checkbox other than the number of repetitions. The only reason for this setup is that the message box displayed before starting the script changes.

Edit: I changed the structure a bit and here's what I'm trying now and it seems to work.

Func _btn_ok()
$sText1 = "Cannot execute 0"
$sText2 = "Do you want to execute " & GUICtrlRead ($Input1) & " time?"
$sText3 = "Do you want to execute " & GUICtrlRead ($Input1) & " times?"
$sText4 = @CRLF & "Checkbox checked!"
If GUICtrlRead ($Input1) < 1 Then
  _ExtMsgBox (16,0, $sTitle, $sText1,0,@DesktopWidth -270, @DesktopHeight -200)
Else
  Select
  Case GUICtrlRead ($Checkbox) = $GUI_CHECKED
   Select
   Case GUICtrlRead ($Input1) =1
    _ExtMsgBox (32,0, $sTitle, $sText2  & $sText4,0,@DesktopWidth -270, @DesktopHeight -200)
   Case Else
    _ExtMsgBox (32,0, $sTitle, $sText3  & $sText4,0,@DesktopWidth -270, @DesktopHeight -200)
   EndSelect
  Case Else
   Select
   Case GUICtrlRead ($Input1) =1
    _ExtMsgBox (32,0, $sTitle, $sText2,0,@DesktopWidth -270, @DesktopHeight -200)
   Case Else
    _ExtMsgBox (32,0, $sTitle, $sText3,0,@DesktopWidth -270, @DesktopHeight -200)
   EndSelect
  EndSelect
EndIf
EndFunc

Any thoughts?

Edited by JonatanRaven
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...