Jump to content

PopUpClear - Close popup windows and message boxes


Bowmore
 Share

Recommended Posts

I've been using this script for a few years now and as I was making a few changes I thought I would share the script. I's quite simple, it just sits there running in the background watching for any of the specified windows to appear and then closes them. PopupClear is controlled from an ini file PopupClear.ini which must be in the same directory as the script. The [sETTINGS] section has 3 keys.
CheckFrequency This controls how often the script check for the presence of the specified window.
LogWindowsClosed If this is set to Y then details of all the windows closed will be saved to the log file.
LogFile Specifies the name of the log file.

The [WINDOWS] section specifies the identification details of the windows to be closed. The keys are in the form of
window0=
window1=
...
windown=

Each key should contain up to 3 pipe separated values
windowTitleID is the title of the window to activate. or Title special definition.
windowText is some identifying text in the window to be closed
ButtonID is the optional controlID of the button to be used to close the window.
 

Having the windows to be closed specified in an ini file means that you can add new windows to the list without stopping PopUpClear.exe

The script can monitor for several different windows but don't expect to be able to monitor thousands and still have your other applications responsive.

PopUpClear.au3

#Region ;**** Directives created by AutoIt3Wrapper_GUI ****
#AutoIt3Wrapper_Icon=PopUpClear.ico
#AutoIt3Wrapper_Outfile=Popupclear.exe
#AutoIt3Wrapper_Change2CUI=N
#AutoIt3Wrapper_UseUpx=n
#AutoIt3Wrapper_Compression=4
#AutoIt3Wrapper_Res_Comment=This tool is run in the background and will automatically close specified Popup windows
#AutoIt3Wrapper_Res_Description=Automatically respond to warning message boxes
#AutoIt3Wrapper_Res_Fileversion=1.1.0.0
#AutoIt3Wrapper_Res_FileVersion_AutoIncrement=p
#AutoIt3Wrapper_Res_LegalCopyright=Bowmore 2015
#AutoIt3Wrapper_Res_SaveSource=n
#AutoIt3Wrapper_Au3Check_Stop_OnWarning=Y
#AutoIt3Wrapper_Au3Check_Parameters= -q -d -w 1 -w 2 -w 3 -w 4 -w 5 -w 6 -w 7
#AutoIt3Wrapper_Run_Tidy=y
#Tidy_Parameters=/tc 2 /gd /sci 0 /sf
#AutoIt3Wrapper_Run_Au3Stripper=y
#Au3Stripper_Parameters=/striponly
#AutoIt3Wrapper_Run_Debug_Mode=N
#AutoIt3Wrapper_Add_Constants=n
#EndRegion ;**** Directives created by AutoIt3Wrapper_GUI ****


#Region Options
Opt("WinWaitDelay", 100)
Opt("WinTitleMatchMode", 4)
Opt("WinDetectHiddenText", 1)
Opt("MouseCoordMode", 2)
Opt("PixelCoordMode", 2)
Opt("TrayAutoPause", 0)
#EndRegion Options

#include <array.au3>

#Region Declaraions
Global $g_iInterval = 2000
Global $g_fLogClosed = True
Global $g_sLogFile = @ScriptDir & "\" & StringLeft(@ScriptName, StringInStr(@ScriptName, ".") - 1) & ".log"
Global $g_aWindows[100][3] = [[0, '', '']]
Global $g_sIniFile = @ScriptDir & "\" & StringLeft(@ScriptName, StringInStr(@ScriptName, ".") - 1) & ".ini"
#EndRegion Declaraions

FileInstall("PopUpClear.ini", @ScriptDir & "\" & "PopUpClear.ini", 0)

#Region Includes
#include <file.au3>
#EndRegion Includes

_ReadIniFile()

While 1
  _KillPopup()
  Sleep($g_iInterval)
WEnd

Func _KillPopup()
  Local $sText = ''
  Local $sTitle = ''
  Local $iTime = 0

  For $i = 0 To UBound($g_aWindows) - 1

    ; Check if the window specified has valid infromation
    If ($g_aWindows[$i][0] & $g_aWindows[$i][1]) = "" Then ContinueLoop

    ; Check if the specified window exists
    If WinExists($g_aWindows[$i][0], $g_aWindows[$i][1]) Then
      If $g_fLogClosed Then
        ; Get the details of the window being closed
        $sTitle = WinGetTitle($g_aWindows[$i][0], $g_aWindows[$i][1])
        $sText = WinGetText($g_aWindows[$i][0], $g_aWindows[$i][1])
        $sText = StringRegExpReplace($sText, "\n", "\\n")
      EndIf

      ;Check if a button has been specified to close the window
      If $g_aWindows[$i][2] Then
        ControlClick($g_aWindows[$i][0], $g_aWindows[$i][1], $g_aWindows[$i][2], "primary")
      Else
        ; Just close the window
        WinClose($g_aWindows[$i][0], $g_aWindows[$i][1])
      EndIf
      If $g_fLogClosed Then
        ; Give the window time to close
        ; check if window still active
        $iTime = TimerInit()
        While TimerDiff($iTime) < $g_iInterval And WinExists($g_aWindows[$i][0], $g_aWindows[$i][1])
          Sleep(50)
        WEnd
        If WinExists($g_aWindows[$i][0], $g_aWindows[$i][1]) Then
          _FileWriteLog($g_sLogFile, 'Failed to close window' & $i & '|' & $sTitle & '|' & $sText)
        Else
          _FileWriteLog($g_sLogFile, 'Closed window' & $i & '|' & $sTitle & '|' & $sText)
        EndIf
      EndIf
    EndIf
  Next
EndFunc   ;==>_KillPopup

Func _ReadIniFile()
  Local $sValue = ''
  Local $iWin = 0
  Local $aParts = 0
  Local $iArraySize = UBound($g_aWindows)
  Local $aIniSection = 0

  ; Read how often the script should check if a window is present (value is in milliseconds)
  $sValue = IniRead($g_sIniFile, 'SETTINGS', 'CheckFrequency', '2000')
  If $sValue <> '' Then
    $g_iInterval = Int(Number($sValue))
  EndIf

  ; Read if the windows that have been closed should be logged
  $sValue = IniRead($g_sIniFile, 'SETTINGS', 'LogWindowsClosed', 'Y')
  If $sValue <> '' Then
    $g_fLogClosed = (StringInStr('YyTt1', StringLeft(StringStripWS($sValue, 8), 1)) <> 0)
  EndIf

  ; Read the name of the file to log the windows closed to.
  $sValue = IniRead($g_sIniFile, 'SETTINGS', 'Logfile', '')
  If $sValue <> '' Then
    $g_sLogFile = $sValue
  EndIf

  ; Read the details of the window sto be monitored
  $aIniSection = IniReadSection($g_sIniFile, 'WINDOWS')
  If @error Then
    MsgBox(0x10, "PopUpClearr - Error", "Unable to read any widow details from Ini file!")
    Exit
  EndIf

  ; Resize array to match number of keys in ini "WINDOWS" section
  $iArraySize = UBound($aIniSection)
  ReDim $g_aWindows[$iArraySize][3]

  For $iWin = 0 To UBound($aIniSection) - 1
    $aParts = StringSplit($aIniSection[$iWin][1], '|', 3)
    ;Read the parameters 0=<window title identifier>|1=<window text identifier>|3=<button identifier>
    If IsArray($aParts) Then
      For $i = 0 To UBound($aParts) - 1
        $g_aWindows[$iWin][$i] = $aParts[$i]
      Next
    EndIf
  Next

EndFunc   ;==>_ReadIniFile

Example PopUpClear.ini

[SETTINGS]
CheckFrequency=2000
LogWindowsClosed=Y
Logfile=PopupsClosed.log
[WINDOWS]
window1=Error|C:\Data\Test|
window2=Notepad|&Save|Do&n't Save

Edit: General tidying up of code
 

Edited by Bowmore

"Programming today is a race between software engineers striving to build bigger and better idiot-proof programs, and the universe trying to build bigger and better idiots. So far, the universe is winning."- Rick Cook

Link to comment
Share on other sites

  • 2 years later...

Mr. Bowmore,  I know this is old so I hope you're still watching.  I am having problems with this error in Excel and was hoping to get some guidance as to what the key should be.  I'm new to AutoIt so any advice would be helpful.  I can select 'don't show again' and everything runs fine but hope I can prevent babying the system this is running on every time it restarts.  Thanks, Cand3r.

ACCESS ERROR.PNG

Link to comment
Share on other sites

Try this (If the text has a short-cut character you need to put a &in front of it)

window1=Microsoft Excel|&The operation to AutoRepublish items to Web pages generated warnings or errors:|OK

or this

window1=Microsoft Excel|For more information, click Help.|OK

"Programming today is a race between software engineers striving to build bigger and better idiot-proof programs, and the universe trying to build bigger and better idiots. So far, the universe is winning."- Rick Cook

Link to comment
Share on other sites

I was wondering what the & was for.  I had tried the 'For more information...' but not the other with the &.  I think I've tried every other possible combo for the key.  It will close it if I leave it as just window1=Microsoft Excel|| but of course that kills Excel too.  Funny thing is the log file outputs this.

2016-02-16 11:55:27 : Failed to close window1|Microsoft Excel|\n

So I'm assuming the window just has no identifiers.  I'm testing a VB solution to check the file in question to prevent the failure rather than recover from it so I'll get there.  This solved my 'Invalid Web Query' error when the server goes down tho.  Thanks much.

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