Jump to content

Help with a simple function.


 Share

Recommended Posts

Hi, just started with autoit as it seems like a pretty powerful tool and I'm always happy to have those. Anyway I started on my first script and I am having some issues. What I am trying to do is write a script that once started checks if a certain program (notepad in this case) is the active window, and if it is not make it so, and continue doing this till the stop button is pressed. My script doesn't seem to be working and I would very much appreciate being pointed in the right direction. Here's what I have so far:

#Region ;**** Directives created by AutoIt3Wrapper_GUI ****
#AutoIt3Wrapper_Res_requestedExecutionLevel=asInvoker
#EndRegion ;**** Directives created by AutoIt3Wrapper_GUI ****
#include <ButtonConstants.au3>
#include <GUIConstantsEx.au3>
#include <StaticConstants.au3>
#include <WindowsConstants.au3>
#Region ### START Koda GUI section ### Form=C:\Users\vmicchia\Downloads\Forms\FMEFK.kxf
$Form1 = GUICreate("DMFocusKeeper", 117, 191, 192, 124)
$Button1 = GUICtrlCreateButton("Start", 16, 72, 75, 25)
$Button2 = GUICtrlCreateButton("Stop", 16, 104, 75, 25)
$Button3 = GUICtrlCreateButton("Close", 16, 136, 75, 25)
$Label1 = GUICtrlCreateLabel("Eterm Focus Keeper", 8, 32, 100, 20, BitOR($SS_CENTER,$SS_CENTERIMAGE))
GUISetState(@SW_SHOW)
#EndRegion ### END Koda GUI section ###
While 1
$nMsg = GUIGetMsg()
Switch $nMsg
  Case $GUI_EVENT_CLOSE, $Button3
   Exit
  Case $Button1
   $var = 1
   Call("CheckActive")
  Case $Button2
   $var = 0
   Call("CheckActive")
EndSwitch
WEnd
Func CheckActive($var)
  While $var == 1
   If NOT WinActive("Notepad") Then WinActivate("Notepad")
  WEnd
EndFunc
Link to comment
Share on other sites

Couple of suggestions:

- add a little bit of sleep inside your while loop; there is no need to do that all the time (saves on CPU utilization)

- bind $var to a hotkey instead of a button - when bound to a button, your script will be stuck inside your CheckActive function forever

- use appropriate WinTitleMatch option (for partial matches - otherwise you'll need to specify complete window title every time)

SNMP_UDF ... for SNMPv1 and v2c so far, GetBulk and a new example script

wannabe "Unbeatable" Tic-Tac-Toe

Paper-Scissor-Rock ... try to beat it anyway :)

Link to comment
Share on other sites

Try this! I think its what u need.

#include <GUIConstantsEx.au3>
#include <StaticConstants.au3>

$Form1 = GUICreate("DMFocusKeeper", 117, 191, 192, 124,0x00000008)
$Button1 = GUICtrlCreateButton("Start", 20, 72, 75, 25)
$Button3 = GUICtrlCreateButton("Close", 20, 116, 75, 25)
$Label1 = GUICtrlCreateLabel("Eterm Focus Keeper", 8, 32, 100, 20, BitOR($SS_CENTER,$SS_CENTERIMAGE))
$var=0
GUISetState(@SW_SHOW)
While 1
$msg=GUIGetMsg()
Select
  Case $msg=$gui_event_close
   Exit
  Case $msg=$Button3
   Exit
  Case $msg=$Button1
   CheckActive()
  Case Else
   ToolTip("Not Working")
   Sleep(10)
EndSelect
WEnd
Func CheckActive()
If $var=1 Then
  GUICtrlSetData($Button1,"Start")
  $var=0
Else
  GUICtrlSetData($Button1,"Stop")
  $var=1
EndIf
While $var = 1
$msg=GUIGetMsg()
Select
  Case $msg=$gui_event_close
   Exit
  Case $msg=$Button3
   Exit
  Case $msg=$Button1
   CheckActive()
  Case Else
   ToolTip("Working")
   Sleep(10)
  If NOT WinActive("Notepad") Then WinActivate("Notepad")
EndSelect
WEnd
EndFunc
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...