Jump to content
Sign in to follow this  
muzle6074

Detect New Application Window

Recommended Posts

muzle6074

Hi everyone,

I was wondering how to detect if the user opens up an application?

Regards

Share this post


Link to post
Share on other sites
Xenobiologist

Hi,

just save the info at the beginning and then loop and test again.

You can differ between a new process or a new window ...

Mega


Scripts & functions Organize Includes Let Scite organize the include files

Yahtzee The game "Yahtzee" (Kniffel, DiceLion)

LoginWrapper Secure scripts by adding a query (authentication)

_RunOnlyOnThis UDF Make sure that a script can only be executed on ... (Windows / HD / ...)

Internet-Café Server/Client Application Open CD, Start Browser, Lock remote client, etc.

MultipleFuncsWithOneHotkey Start different funcs by hitting one hotkey different times

Share this post


Link to post
Share on other sites
Skruge

Have you tried WinWait() or ProcessWait()?


[font="Tahoma"]"Tougher than the toughies and smarter than the smarties"[/font]

Share this post


Link to post
Share on other sites
muzle6074

Hi

@Xenobiologist

Yes, I did think of this but it isn't an efficient method of doing this. How ever if there is no function that detects a new window then I will probably have to do this. Do you know how to check sum a process list against another and then extract the difference?

@Skruge

No I haven't because the windows I want to detect could be called anything.

Share this post


Link to post
Share on other sites
WideBoyDixon

See my post in the Example Scripts forum for how you *might* be able to do this. A bit clunky but any port in a storm ...

WBD

Share this post


Link to post
Share on other sites
muzle6074

Hi WideBoyDixon,

Just looking over your script, given me a good insight into how I could implement something similar, thanks.

Share this post


Link to post
Share on other sites
Xenobiologist

Hi,

here is something I found in my Autoit(Forum copied) folder

Global $k = "p", $gk = False, $f = "$M", $m = True, $t = False, $gf = False, $p = "$S:$N$C$L$I", $gp = False, $ps = "Started", $gps = False, $pe = "Ended", $gpe = False, $d = "$W, $M $D, $Y, $H$O$i$O$s $P", $gd = False, $timer = 0, $utimer = False

Func SetUpTray()
    Opt("TrayMenuMode", 1)
    Opt("TrayAutoPause", 0)
    Opt("TrayOnEventMode", 1)

    TraySetClick(8)
    TraySetToolTip("Process Watcher")

    Local $tmb = TrayCreateItem("&MessageBox"), $tt = TrayCreateItem("&Tray"), $tf = TrayCreateItem("&File")
    TrayItemSetOnEvent($tmb, "TrayItemMessageBox")
    TrayItemSetOnEvent($tt, "TrayItemTray")
    TrayItemSetOnEvent($tf, "TrayItemFile")
    TrayItemSetOnEvent(TrayCreateItem("E&xit"), "MyExit")

    If GetFileName() Then
        TrayItemSetState($tf, 1)
    EndIf
    
    If GetMsgBox() Then
        TrayItemSetState($tmb, 1)
    EndIf
    
    If GetTaskBar() Then
        TrayItemSetState($tt, 1)
    EndIf
EndFunc

SetUpTray()

If GetKey() Then
    HotKeySet("^!" & GetKey(), "MyExit")
EndIf

DllCall("kernel32.dll", "int", "SetProcessShutdownParameters", "long", 0x3FF, "long", 0)

$procs = GetProcs()
$oldprocs = $procs

If GetFileName() Then
    Local $message = StringSplit(FormatMessage("", 0, "Log " & GetProcessStart()), ":")
    $message[1] = StringReplace($message[1], "$O", ":", 0, 1)
    FileWriteLine(GetFileName(), $message[1])
EndIf

While True
    Do
        Sleep(100)
        $procs = GetProcs()
    Until $procs[0][0] <> $oldprocs[0][0]

    For $a = 1 To $procs[0][0]
        For $b = 1 To $oldprocs[0][0]
            If Not $oldprocs[$b][2] And $procs[$a][1] = $oldprocs[$b][1] Then
                $oldprocs[$b][2] = True
                $procs[$a][2] = True
                ExitLoop
            EndIf
        Next
    Next

    For $a = 1 To $procs[0][0]
        If Not $procs[$a][2] Then ProcessChange($procs[$a][0], $procs[$a][1], $ps)
    Next

    For $a = 1 To $oldprocs[0][0]
        If Not $oldprocs[$a][2] Then ProcessChange($oldprocs[$a][0], $oldprocs[$a][1], $pe)
    Next

    $oldprocs = $procs

    For $a = 1 To $oldprocs[0][0]
        $oldprocs[$a][2] = False
    Next
    
    If $utimer Then
        If TimerDiff($timer) > 5000 Then
            TrayTip("", "", 0)
            $utimer = False
        EndIf
    EndIf
WEnd

Func TrayItemMessageBox()
    If GetMsgBox() Then
        $m = False
        TrayItemSetState(@TRAY_ID, 4)
    Else
        $m = True
        TrayItemSetState(@TRAY_ID, 1)
    EndIf
EndFunc

Func TrayItemTray()
    If GetTaskBar() Then
        $t = False
        TrayItemSetState(@TRAY_ID, 4)
    Else
        $t = True
        TrayItemSetState(@TRAY_ID, 1)
    EndIf
EndFunc

Func TrayItemFile()
    If GetFileName() Then
        $f = ""
        TrayItemSetState(@TRAY_ID, 4)
    Else
        $f = FileSaveDialog("Log File", @WorkingDir, "All Files (*.*)", 16, "log.txt")
        If GetFileName() Then
            TrayItemSetState(@TRAY_ID, 1)
        EndIf
    EndIf
EndFunc

Func MyExit()
    If MsgBox(0x24, "Exit", "Are you sure you want to exit?") = 6 Then Exit
EndFunc

Func IIf($expression, $true, $false)
    If $expression Then Return $true
    Return $false
EndFunc

Func GetProcs()
    Local $procs = ProcessList()
    Local $ret[$procs[0][0] + 1][3]
    $ret[0][0] = $procs[0][0]
    For $a = 1 To $procs[0][0]
        $ret[$a][0] = $procs[$a][0]
        $ret[$a][1] = $procs[$a][1]
        $ret[$a][2] = False
    Next
    Return $ret
EndFunc

Func GetKey()
    If $gk Then Return $k
    For $a = 1 To $CmdLine[0]
        If StringLeft($CmdLine[$a], 3) = "/k:" Then
            $k = StringLower(StringRight($CmdLine[$a], StringLen($CmdLine[$a]) - 3))
            ExitLoop
        ElseIf $CmdLine[$a] = "/k" Then
            $k = ""
            ExitLoop
        EndIf
    Next
    $gk = True
    Return $k
EndFunc

Func GetFileName(); $M = message box, $T = tray balloon, filename
    If $gf Then Return $f
    For $a = 1 To $CmdLine[0]
        If StringLeft($CmdLine[$a], 3) = "/f:" Then
            $f = StringRight($CmdLine[$a], StringLen($CmdLine[$a]) - 3)
            ExitLoop
        EndIf
    Next
    If StringInStr($f, "$M", 1) Then
        $m = True
        $f = StringReplace($f, "$M", "", 0, 1)
    Else
        $m = False
    EndIf
    If StringInStr($f, "$T", 1) Then
        $t = True
        $f = StringReplace($f, "$T", "", 0, 1)
    Else
        $t = False
    EndIf
    $gf = True
    Return $f
EndFunc

Func GetMsgBox()
    Return $m
EndFunc

Func GetTaskBar()
    Return $t
EndFunc

Func GetFormat()
    If $gp Then Return $p
    For $a = 1 To $CmdLine[0]
        If StringLeft($CmdLine[$a], 3) = "/p:" Then
            $p = StringRight($CmdLine[$a], StringLen($CmdLine[$a]) - 3)
            ExitLoop
        EndIf
    Next
    $gp = True
    If Not StringInStr($p, ":") Then $p = ":" & $p
    Return $p
EndFunc

Func GetProcessStart()
    If $gps Then Return $ps
    For $a = 1 To $CmdLine[0]
        If StringLeft($CmdLine[$a], 3) = "/s:" Then
            $ps = StringRight($CmdLine[$a], StringLen($CmdLine[$a]) - 3)
            ExitLoop
        EndIf
    Next
    $gps = True
    Return $ps
EndFunc

Func GetProcessEnd()
    If $gpe Then Return $pe
    For $a = 1 To $CmdLine[0]
        If StringLeft($CmdLine[$a], 3) = "/e:" Then
            $pe = StringRight($CmdLine[$a], StringLen($CmdLine[$a]) - 3)
            ExitLoop
        EndIf
    Next
    $gpe = True
    Return $pe
EndFunc

Func GetDate()
    If $gd Then Return $d
    For $a = 1 To $CmdLine[0]
        If StringLeft($CmdLine[$a], 3) = "/d:" Then
            $d = StringRight($CmdLine[$a], StringLen($CmdLine[$a]) - 3)
            ExitLoop
        EndIf
    Next
    $gd = True
    Return $d;
EndFunc

Func GetW(); Weekday as string
    If @WDAY = 1 Then Return "Sunday"
    If @WDAY = 2 Then Return "Monday"
    If @WDAY = 3 Then Return "Tuesday"
    If @WDAY = 4 Then Return "Wednesday"
    If @WDAY = 5 Then Return "Thursday"
    If @WDAY = 6 Then Return "Friday"
    Return "Saturday"
EndFunc

Func GetM()
    If Int(@MON) = 1 Then Return "January"
    If Int(@MON) = 2 Then Return "February"
    If Int(@MON) = 3 Then Return "March"
    If Int(@MON) = 4 Then Return "April"
    If Int(@MON) = 5 Then Return "May"
    If Int(@MON) = 6 Then Return "June"
    If Int(@MON) = 7 Then Return "July"
    If Int(@MON) = 8 Then Return "August"
    If Int(@MON) = 9 Then Return "September"
    If Int(@MON) = 10 Then Return "October"
    If Int(@MON) = 11 Then Return "November"
    Return "December"
EndFunc

Func FormatDate()
    Local $ret = GetDate()
    $ret = StringReplace($ret, "$W", GetW(), 0, 1)
   ; Weekday as string
    $ret = StringReplace($ret, "$w", @WDAY, 0, 1)
   ; Weekday as number
    $ret = StringReplace($ret, "$M", GetM(), 0, 1)
   ; Month as string
    $ret = StringReplace($ret, "$mx", @MON, 0, 1)
   ; Month as number, 2 digits
    $ret = StringReplace($ret, "$m", Int(@MON), 0, 1)
   ; Month as number
    $ret = StringReplace($ret, "$D", Int(@MDAY), 0, 1)
   ; Day as number
    $ret = StringReplace($ret, "$d", @MDAY, 0, 1)
   ; Day as 2 digits
    $ret = StringReplace($ret, "$Y", @YEAR, 0, 1)
   ; Year as 4 digits
    $ret = StringReplace($ret, "$y", StringRight(@YEAR, 2), 0, 1)
   ; Year as 2 digits
    $ret = StringReplace($ret, "$Hx", IIf(StringLen(IIf(Int(@HOUR) = 0, 12, IIf(Int(@HOUR) > 12, Int(@HOUR) - 12,Int(@HOUR)))) = 1, "0" & IIf(Int(@HOUR) = 0, 12, IIf(Int(@HOUR) > 12, Int(@HOUR) - 12, Int(@HOUR))), IIf(Int(@HOUR) = 0, 12, IIf(Int(@HOUR) > 12, Int(@HOUR) - 12, Int(@HOUR)))), 0, 1)
   ; Hour12 as 2 digits
    $ret = StringReplace($ret, "$H", IIf(Int(@HOUR) = 0, 12, IIf(Int(@HOUR) > 12, Int(@HOUR) - 12, Int(@HOUR))), 0, 1)
   ; Hour12 as number
    $ret = StringReplace($ret, "$hx", @HOUR, 0, 1)
   ; Hour24 as 2 digit
    $ret = StringReplace($ret, "$h", Int(@HOUR), 0, 1)
   ; Hour24 as number
    $ret = StringReplace($ret, "$I", Int(@MIN), 0, 1)
   ; Minutes as number
    $ret = StringReplace($ret, "$i", @MIN, 0, 1)
   ; Minutes as 2 digit
    $ret = StringReplace($ret, "$S", Int(@SEC), 0, 1)
   ; Seconds as number
    $ret = StringReplace($ret, "$s", @SEC, 0, 1)
   ; Seconds as 2 digit
    $ret = StringReplace($ret, "$P", IIf(@HOUR < 13, "AM", "PM"), 0, 1)
   ; AM/PM
    Return $ret
EndFunc

Func FormatMessage($N, $I, $S)
    Local $ret = GetFormat()
    $ret = StringReplace($ret, "$N", $N, 0, 1)
   ; Process name
    $ret = StringReplace($ret, "$I", $I, 0, 1)
   ; PID
    $ret = StringReplace($ret, "$D", FormatDate(), 0, 1)
   ; Date
    $ret = StringReplace($ret, "$S", $S, 0, 1)
   ; Starting/Ending
    $ret = StringReplace($ret, "$Q", """", 0, 1)
   ; Quote
    $ret = StringReplace($ret, "$C", @CR, 0, 1)
   ; Carridge return
    $ret = StringReplace($ret, "$L", @LF, 0, 1)
   ; Line feed
    Return $ret
EndFunc

Func ProcessChange($N, $I, $S)
    Local $message = StringSplit(FormatMessage($N, $I, $S), ":")
    $message[1] = StringReplace($message[1], "$O", ":", 0, 1)
    $message[1] = StringReplace($message[1], "$@", "$", 0, 1)
    $message[2] = StringReplace($message[2], "$O", ":", 0, 1)
    $message[2] = StringReplace($message[2], "$@", "$", 0, 1)

    If GetTaskBar() Then
        TrayTip($message[1], $message[2], 5, 1)
        $utimer = True
        $timer = TimerInit()
    EndIf

    If GetFileName() Then
        FileWriteLine(GetFileName(), $message[2])
    EndIf

    If GetMsgBox() Then
        If $S == $ps Then
            If MsgBox(0x40124, $message[1], $message[2]) = 6 Then ProcessClose($I)
        Else
            MsgBox(0x40, $message[1], $message[2])
        EndIf
    EndIf
EndFunc

Func OnAutoItExit()
    If GetFileName() Then
        Local $message = StringSplit(FormatMessage("", 0, "Log " & GetProcessEnd()), ":")
        $message[1] = StringReplace($message[1], "$O", ":", 0, 1)
        FileWriteLine(GetFileName(), $message[1])
        FileWriteLine(GetFileName(), "")
    EndIf
EndFunc

Mega


Scripts & functions Organize Includes Let Scite organize the include files

Yahtzee The game "Yahtzee" (Kniffel, DiceLion)

LoginWrapper Secure scripts by adding a query (authentication)

_RunOnlyOnThis UDF Make sure that a script can only be executed on ... (Windows / HD / ...)

Internet-Café Server/Client Application Open CD, Start Browser, Lock remote client, etc.

MultipleFuncsWithOneHotkey Start different funcs by hitting one hotkey different times

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  

×