JonatanRaven 0 Posted December 2, 2011 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? Share this post Link to post Share on other sites
water 2,392 Posted December 2, 2011 (edited) 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 December 2, 2011 by water My UDFs and Tutorials: Spoiler UDFs:Active Directory (NEW 2020-10-10 - Version 1.5.2.1) - Download - General Help & Support - Example Scripts - WikiOutlookEX (NEW 2020-12-15 - Version 1.6.3.1) - Download - General Help & Support - Example Scripts - WikiOutlookEX_GUI (2020-06-27 - Version 1.3.2.0) - DownloadOutlook Tools (2019-07-22 - Version 0.6.0.0) - Download - General Help & Support - WikiExcelChart (2017-07-21 - Version 0.4.0.1) - Download - General Help & Support - Example ScriptsPowerPoint (2017-06-06 - Version 0.0.5.0) - Download - General Help & SupportExcel - Example Scripts - WikiWord - WikiTask Scheduler (2019-12-03 - Version 1.5.1.0) - Download - General Help & Support - WikiTutorials:ADO - Wiki, WebDriver - Wiki Share this post Link to post Share on other sites
JonatanRaven 0 Posted December 3, 2011 Ah, thank you. Sometimes you get stuck in the tracks trying to do something. Share this post Link to post Share on other sites
JonatanRaven 0 Posted December 6, 2011 (edited) 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 December 6, 2011 by JonatanRaven Share this post Link to post Share on other sites