Jump to content

Abbreviation for WinWait, WinActivate and WinWaitActive


Exit
 Share

Recommended Posts

I was annoyed of always entering WinWait, WinActivate and WinWaitActive to test, if a window is up and running.
Those functions set @error to 0 even when a timeout occurred. I had always to test the returned value against '0'.
Therefore I made an abbreviation for SciTE.
If you type 'winact ' (watch the space after winact) in the SciTE editor, the input will be replaced by

(WinWait("[REGEXPTITLE:|]", "", 0) ? (WinActivate("[LAST]") ? WinWaitActive("[LAST]") : 0) : seterror(1,0,0))

The vertical bar indicates where the cursor will be placed.
Just enter the windows title and a timeout value, if required.
Return Values:
     Success: handle to the window. @error=0
     Failure: 0.                                   @error=1  
 
You might add the abbreviation with the SciTE config tool or just use this code snippet.
It updates the two involved files. Note the 2 lines after ShellExecute. They where generated by 'winact '.

#include <StringConstants.au3>
#include <MsgBoxConstants.au3>
While Sleep(100)
    If Not WinClose("[REGEXPTITLE:au3UserAbbrev.properties|au3.keywords.user.abbreviations.properties]") Then ExitLoop
WEnd
ShellExecute("NotePad.exe", @LocalAppDataDir & "\AutoIt v3\SciTE\au3UserAbbrev.properties", "", "", @SW_SHOW)
ShellExecute("NotePad.exe", @LocalAppDataDir & "\AutoIt v3\SciTE\au3.keywords.user.abbreviations.properties", "", "", @SW_SHOW)
$WinHandle1 = (WinWait("[REGEXPTITLE:au3UserAbbrev.properties]", "", 5) ? (WinActivate("[LAST]") ? WinWaitActive("[LAST]") : 0) : SetError(1, 0, 0))
$WinHandle2 = (WinWait("[REGEXPTITLE:au3.keywords.user.abbreviations.properties]", "", 5) ? (WinActivate("[LAST]") ? WinWaitActive("[LAST]") : 0) : SetError(1, 0, 0))
$Text1 = ControlGetText($WinHandle1, "", "Edit1")
$Text2 = ControlGetText($WinHandle2, "", "Edit1")
$Abbrev = 'winact=(WinWait("[REGEXPTITLE:|]", "", 0) ? (WinActivate("[LAST]") ? WinWaitActive("[LAST]") : 0) : seterror(1,0,0))'
If Not (StringInStr($Text1, $Abbrev)) Then
    $Text1 = StringStripWS($Text1, $STR_STRIPTRAILING)
    ControlSetText($WinHandle1, "", "Edit1", $Text1 & @CRLF & $Abbrev & @CRLF)
    ControlSend($WinHandle1, "", "", "^s")
EndIf
If Not (StringInStr($Text2, "winact")) Then
    $Text2 = StringStripWS($Text2, $STR_STRIPTRAILING)
    ControlSetText($WinHandle2, "", "Edit1", $Text2 & " winact")
    ControlSend($WinHandle2, "", "", "^s")
EndIf
$aPos = WinGetPos($WinHandle1)
WinActivate($WinHandle1)
WinMove($WinHandle1, "", $aPos[0], $aPos[1] + 100)
MsgBox($MB_ICONINFORMATION + $MB_TOPMOST, " ", "Abbreviation  ""winact""  is installed. ", 0)
WinMove($WinHandle1, "", $aPos[0], $aPos[1])
WinClose($WinHandle1)
WinClose($WinHandle2)

And here a sample to demonstrate failure and success of the expanded abbreviation:

;~ test if window with title "desktop.ini" is active
(WinWait("[REGEXPTITLE:desktop.ini]", "", 1) ? (WinActivate("[LAST]") ? WinWaitActive("[LAST]") : 0) : SetError(1, 0, 0))
MsgBox(Default, Default, "@Error: " & @error & @LF & "Error 1 (Window not found)", 0)
;~ start NotePad.exe
ShellExecute("NotePad.exe", @DesktopDir & "\desktop.ini")
; get Handle of desktop.ini NotePad window
$Handle = (WinWait("[REGEXPTITLE:desktop.ini]", "", 1) ? (WinActivate("[LAST]") ? WinWaitActive("[LAST]") : 0) : SetError(1, 0, 0))
MsgBox(262144, "Debug " & @ScriptName, "@Error: " & @error & @LF & "Handle: " & $Handle, 0)
WinClose($Handle)

App: Au3toCmd              UDF: _SingleScript()                             

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...