Jump to content
Sign in to follow this  
magician13134

How to tell if a window has been opened..?

Recommended Posts

magician13134

I want a function that will alert you when a window has been opened. Like, for instance I open Mozilla, a message box will pop up and say "A window called 'Google - Mozilla Firefox' has been opened"

I started one, but I'm just not good enough in Autoit to finish it... I'd really appreciate some help. Also, I'd like one that alerts when a window is closed, but I should be able to do that if someone can finish this one up. Thanks...

(I'll comment this code so you can make more sense of it)

$windows = WinList() ; Original list of windows for comparison 

While 1 
    Sleep(250) ; Just to pause the script a little
    $return = newWindowOpened($windows) ; Calls the function, $return will either be an array or 0
    $windows = WinList() ; Refresh the current list of windows
    If IsArray($return) Then ; If new windows were found
        For $i = 1 to $return[0] ; $return[0] is the number of new windows
            MsgBox(0, "", $return[$i]) ; Output each new window's title
        Next
    EndIf
WEnd


Func newWindowOpened(ByRef $oWArray)
    ;$timer = TimerInit() ; To test the efficiency of my function, not very good, ~250mS...
    $tempArray = WinList() ; Get current windows
    Dim $newWindows[$tempArray[0][0] + 1] ; Dim an array with the maximum amount of new windows (the amount of currently open windows, + 1 for $newWindows[0], which is the number of new windows
    $newWindows[0] = 0 ; We haven't found any new windows yet
    For $i = 1 to $tempArray[0][0] ; Run through all the new windows
        $found = 0 ; The window is new so far
        For $j = 1 to $oWArray[0][0] ; Run through the old windows
            If $tempArray[$i][1] = $oWArray[$j][1] Then ; If the new window's handle is found in the list of old handles...
                $found = 1 ; The window is no longer marked 'new'
                ExitLoop ; Leave 'for' loop now to save time
            EndIf
        Next
                If $found == 0 Then ; If the handle wasn't found, it's new
                $newWindows[0]+=1 ; Add one to the amount of new windows
                $newWindows[$newWindows[0]] = $tempArray[$i][0] ; Store the window's handle
            EndIf
    Next
    ;MsgBox(0, "", "This function took: " & TimerDiff($timer) & " milliseconds")
    If $newWindows[0] <> 0 Then ; If windows were found
        Return $newWindows ; Return the array
    Else
        Return 0
    EndIf
EndFunc

This script works, kinda, but it's slow, therefor some windows slip passed...

Edited by magician13134

Share this post


Link to post
Share on other sites
Generator

Base on your problem, change the Sleep(250) to Sleep(1)?

Share this post


Link to post
Share on other sites
Sardith

@ magician13134

Are you looking for just - Mozilla Firefox windows being opened or?


[font="Verdana"]Valik:Get it straight - I'm not here to say please, I'm here to help - if my help's not appreciated then lotsa luck, gentlemen.[/font]

Share this post


Link to post
Share on other sites
therks

The problem I perceive with a function/script like this is that there are LOTS of windows open at one time that aren't normal windows, or that are hidden, etc. A lot of the time when you open one program that appears to only have one window, several windows are created. For example: Every tooltip is it's own window, and every tooltip's shadow, is again it's own window.

Just some stuff to keep in mind.

Share this post


Link to post
Share on other sites
magician13134

@Generator - No, because in the program I'm using it, it will not be called every millisecond. The function itself takes 250mS to complete the two for loops

@Sardith - No, all programs

@Saunders - Well... that makes it harder...

Is there any Windows dll call or anything that will alert me of something like this? Thanks

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  

×

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.