Sign in to follow this  
Followers 0
faldo

Loop problems

17 posts in this topic

Whenever i put a loop in my script, it halts the rest of the functions, i can't even quit :/

Here's what i got:

#include <GUIConstants.au3>


If Random() < 0.5 Then
;Capitals
   $Letter1 = Chr(Random(Asc("A"), Asc("Z")+1))
   $Letter2 = Chr(Random(Asc("A"), Asc("Z")+1))
   $Letter3 = Chr(Random(Asc("A"), Asc("Z")+1))
   $Letter4 = Chr(Random(Asc("A"), Asc("Z")+1))
   $Letter5 = Chr(Random(Asc("A"), Asc("Z")+1))
   $Letter6 = Chr(Random(Asc("A"), Asc("Z")+1))
Else
;Lower case
   $Letter1 = Chr(Random(Asc("a"), Asc("z")+1))
   $Letter2 = Chr(Random(Asc("a"), Asc("z")+1))
   $Letter3 = Chr(Random(Asc("a"), Asc("z")+1))
   $Letter4 = Chr(Random(Asc("a"), Asc("z")+1))
   $Letter5 = Chr(Random(Asc("a"), Asc("z")+1))
   $Letter6 = Chr(Random(Asc("a"), Asc("z")+1))
Endif


GUICreate("Windows title changer")
$n1=GUICtrlCreateCombo("Select a window", 10, 20)
GUICtrlSetData(-1, getWindows())
GUISetState()

GUICtrlCreateLabel ( "Do you want to change the window-title of:",  60, 50)
$selected=GUICtrlCreateLabel ( GuiRead($n1),  70, 70, 300, 20)
GUICtrlCreateLabel ( "to:",  60, 90)
$changed=GUICtrlCreateLabel ( $Letter1 & $Letter2 & $Letter3 & $Letter4 & $Letter5 & $Letter6,  70, 110, 300)

$cancel=GUICtrlCreateButton ("Cancel",  120, 140, 50)
$yes=GUICtrlCreateButton ( "Yes",  70, -1, 50)
$refresh=GUICtrlCreateButton ( "Refresh",  320, 10, 50)

GUISetState () 


; Run the GUI until the dialog is closed
While 1
 $msg = GUIGetMsg()
 If $msg = $GUI_EVENT_CLOSE Then ExitLoop
 If $msg = $n1 Then GUICtrlSetData($selected, GUIRead($n1))
 If $msg = $cancel Then ExitLoop
 If $msg = $yes Then WinSetTitle(GuiRead($n1), "", GuiRead($changed))
 If $msg = $yes Then MsgBox(0, "Titlechange", 'The window-title: "' & GuiRead($n1) & '" was successfully changed to: 

"' & GuiRead($changed) & '"' )
 If $msg = $yes Then GUICtrlSetData($n1, getWindows())
 If $msg = $yes Then keeptitle()
 If $msg = $refresh then GUICtrlSetData($n1, getWindows())
Wend

Func getWindows()
 Local $T_Windows = ""
 Local $Var = WinList ()
 For $i = 1 To $var[0][0]
; Only display visble windows that have a title
    If $var[$i][0] <> "" And IsVisible($var[$i][1]) Then
          $T_Windows = $T_Windows & "|" & $var[$i][0]
    EndIf
 Next
 Return $T_Windows
EndFunc;==>getWindows


Func IsVisible($handle)
 If BitAND( WinGetState($handle), 2) Then
    Return 1
 Else
    Return 0
 EndIf
EndFunc;==>IsVisible


Func keeptitle()
Do
  Sleep (1000)
  $title = WinGetTitle(GuiRead($changed), "")
  If not $title = $Letter1 & $Letter2 & $Letter3 & $Letter4 & $Letter5 & $Letter6 then
  WinSetTitle(GuiRead($n1), "", GuiRead($changed))
  EndIf
Until not $title=GuiRead($changed)
Endfunc

Whenever the "If $msg = $yes" gets called, the "keeptitle()-loop" i made prevents anything else from happening... could i please get some help on how to change the loop ?

Share this post


Link to post
Share on other sites



You have in your do until

WinSetTitle(GuiRead($n1), "", GuiRead($changed))
changing title to exactly the same as before, as in last code, but do until $title does not equal the same as before
Until not $title=GuiRead($changed)
which reads earlier in the script
$changed=GUICtrlCreateLabel ( $Letter1 & $Letter2 & $Letter3 & $Letter4 & $Letter5 & $Letter6,  70, 110, 300)
Do you think I am reading this right?

Share this post


Link to post
Share on other sites

You're reading it exactly right... and it works great, as soon as the title of the window changes, it chages back to the original title set by the script...

But i'm not very used to this kind of scripting since i updated to this new and improved version not long ago. I just can't figure out how to make the keeptitle() function and the rest of the script running simultaniously :)

Share this post


Link to post
Share on other sites

Until not $title=GuiRead($changed)
to
Until $title=GuiRead($changed)

That seems to work?

Share this post


Link to post
Share on other sites

#5 ·  Posted (edited)

Nah... that will just run the function once and not run it again. The "not" makes the function loop untill the title is changed, and then changes it back to the one given by the script.

But in that loop, the entire script seems to "halt" :)

Edited by faldo

Share this post


Link to post
Share on other sites

#6 ·  Posted (edited)

Priority problem:

not $title=GuiRead($changed)

means:

(not $title)=GuiRead($changed)

you probably meant

not ($title=GuiRead($changed))

or just

$title <> GuiRead($changed)

And PLEASE, remove that UGLY :):) $letter1, $letter2, $letter3 thing!

Here an idea for making better!

Edited by ezzetabi

Share this post


Link to post
Share on other sites

Ok... so now i changed that... but noone has yet answeded my question... i asked if someone could correct my keeptitle() function so that the loop won't halt the entire script.

As is is now, once the script calls the keeptitle() function, i can't quit the trainer. I'm not sure if i'm making myself unclear :/

Share this post


Link to post
Share on other sites

#8 ·  Posted (edited)

Ok... so now i changed that... but noone has yet answeded my question... i asked if someone could correct my keeptitle() function so that the loop won't halt the entire script.

As is is now, once the script calls the keeptitle() function, i can't quit the trainer. I'm not sure if i'm making myself unclear :/

<{POST_SNAPBACK}>

tried to understand the purpose of the keeptitle() function but cannot figure it out.

What do you want to accomplish in it ??

Edited by JdeB

Visit the SciTE4AutoIt3 Download page for the latest versions        Beta files                                                          Forum Rules
 
Live for the present,
Dream of the future,
Learn from the past.
  :)

Share this post


Link to post
Share on other sites

#9 ·  Posted (edited)

Ok... lets take Notepad for example:

When you use the WinSetTitle function the title changes, everything is fine, but if i were to save the notepad document, the title would change with a command comming from notepad.

So keeptitle() function detects this change and changes it back to the original title that was given by the script.

Edited by faldo

Share this post


Link to post
Share on other sites

Ok... lets take Notepad for example:

When you use the WinSetTitle function the title changes, everything is fine, but if i were to save the notepad document, the title would change with a command comming from notepad.

So keeptitle() function detects this change and changes it back to the original title that was given by the script.

<{POST_SNAPBACK}>

ok understand now.

Only way i can see it working is that you keep an array of window handles and their New titles. Then in an Adlib function check all of them and change them back as needed.


Visit the SciTE4AutoIt3 Download page for the latest versions        Beta files                                                          Forum Rules
 
Live for the present,
Dream of the future,
Learn from the past.
  :)

Share this post


Link to post
Share on other sites

Get the windows handles, the titles, change the title, check every second with a adlib function if it changed and if it is, reset it using the handle.

Share this post


Link to post
Share on other sites

Yikes... never used adlib or arrays before :/

Any hints?

Share this post


Link to post
Share on other sites

#13 ·  Posted (edited)

RTFM

About Arrays, and about the Adlib function.

With the lastest unstable release you have amost the job done.

The still undocumented func WinList() will make for you an array with both all handles and all titles!

$a = WinList()

$msg = ''
For $c = 1 to $a[0][0]
   If BitAND(WinGetState($a[$c][1],''),2) = 2 Then;If visible
      $msg = $msg & $a[$c][0] & ' <-> ' &$a[$c][1] & @lf
   EndIf
Next
MsgBox(0,'',$msg)

This sample code just makes a msgbox with all visible windows.

So when you rename a windows, just make the list again.

Start the adlib that takes all titles with WinGetTitle() from the handles, it any is different from the one you still have in the old array rename it.

Just for personal knowledge, why do you need something like that?!?

Edited by ezzetabi

Share this post


Link to post
Share on other sites

#14 ·  Posted (edited)

Yikes... never used adlib or arrays before :/

Any hints?

<{POST_SNAPBACK}>

This is untested ... but should give you an idea how it could work:

#include <GUIConstants.au3>
Global $SWinTitles[50][2]
Global $SWinCount = 0
AdlibEnable("keeptitle")
If Random() < 0.5 Then
  ;Capitals
   $Letter1 = Chr(Random(Asc("A"), Asc("Z") + 1))
   $Letter2 = Chr(Random(Asc("A"), Asc("Z") + 1))
   $Letter3 = Chr(Random(Asc("A"), Asc("Z") + 1))
   $Letter4 = Chr(Random(Asc("A"), Asc("Z") + 1))
   $Letter5 = Chr(Random(Asc("A"), Asc("Z") + 1))
   $Letter6 = Chr(Random(Asc("A"), Asc("Z") + 1))
Else
  ;Lower case
   $Letter1 = Chr(Random(Asc("a"), Asc("z") + 1))
   $Letter2 = Chr(Random(Asc("a"), Asc("z") + 1))
   $Letter3 = Chr(Random(Asc("a"), Asc("z") + 1))
   $Letter4 = Chr(Random(Asc("a"), Asc("z") + 1))
   $Letter5 = Chr(Random(Asc("a"), Asc("z") + 1))
   $Letter6 = Chr(Random(Asc("a"), Asc("z") + 1))
EndIf
GUICreate("Windows title changer")
$n1 = GUICtrlCreateCombo("Select a window", 10, 20)
GUICtrlSetData(-1, getWindows())
GUISetState()
GUICtrlCreateLabel("Do you want to change the window-title of:", 60, 50)
$selected = GUICtrlCreateLabel(GUIRead($n1), 70, 70, 300, 20)
GUICtrlCreateLabel("to:", 60, 90)
$changed = GUICtrlCreateLabel($Letter1 & $Letter2 & $Letter3 & $Letter4 & $Letter5 & $Letter6, 70, 110, 300)
$cancel = GUICtrlCreateButton("Cancel", 120, 140, 50)
$yes = GUICtrlCreateButton("Yes", 70, -1, 50)
$refresh = GUICtrlCreateButton("Refresh", 320, 10, 50)
GUISetState()
; Run the GUI until the dialog is closed
While 1
   $msg = GUIGetMsg()
   If $msg = $GUI_EVENT_CLOSE Then ExitLoop
   If $msg = $n1 Then GUICtrlSetData($selected, GUIRead($n1))
   If $msg = $cancel Then ExitLoop
   If $msg = $yes Then 
      WinSetTitle(GUIRead($n1), "", GUIRead($changed))
      MsgBox(0, "Titlechange", 'The window-title: "' & GUIRead($n1) & '" was successfully changed to:"' & GuiRead($changed) & '"' )
      GUICtrlSetData($n1, getWindows())
    ; save info in array
      $SWinCount = $SWinCount + 1
      $SWinTitles[$SWinCount][0] = WinGetHandle(GUIRead($changed))
      $SWinTitles[$SWinCount][1] = GUIRead($changed)
   EndIf
   If $msg = $refresh Then GUICtrlSetData($n1, getWindows())
Wend
Func getWindows()
   Local $T_Windows = ""
   Local $Var = WinList ()
   For $i = 1 To $Var[0][0]
    ; Only display visble windows that have a title
      If $Var[$i][0] <> "" And IsVisible($Var[$i][1]) Then
         $T_Windows = $T_Windows & "|" & $Var[$i][0]
      EndIf
   Next
   Return $T_Windows
EndFunc  ;==>getWindows
Func IsVisible($handle)
   If BitAND( WinGetState($handle), 2) Then
      Return 1
   Else
      Return 0
   EndIf
EndFunc  ;==>IsVisible
Func keeptitle()
   For $x = 1 to $SWinCount
      If WinGetTitle($SWinTitles[$x][0], "") <> $SWinTitles[$x][1] Then
         WinSetTitle($SWinTitles[$x][0], "", $SWinTitles[$x][1])
      EndIf
   Next
EndFunc  ;==>keeptitle
Edited by JdeB

Visit the SciTE4AutoIt3 Download page for the latest versions        Beta files                                                          Forum Rules
 
Live for the present,
Dream of the future,
Learn from the past.
  :)

Share this post


Link to post
Share on other sites

Of course it works... JdeB, but I think that giving solution so straight won't help a noob. Do you think he'll will actually study and understand how did you do?

Share this post


Link to post
Share on other sites

#16 ·  Posted (edited)

Of course it works... JdeB, but I think that giving solution so straight won't help a noob. Do you think he'll will actually study and understand how did you do?

<{POST_SNAPBACK}>

@ezzetabi: No, not right away, but there is something to study now .... :) Edited by JdeB

Visit the SciTE4AutoIt3 Download page for the latest versions        Beta files                                                          Forum Rules
 
Live for the present,
Dream of the future,
Learn from the past.
  :)

Share this post


Link to post
Share on other sites

Acctually, i never enter a line of code in a script without understanding how it works...

Anyways, thanx both of you.

Share this post


Link to post
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
Sign in to follow this  
Followers 0