Jump to content

Combine Tray and ExecNotificationQuery


Recommended Posts

Hi

I´m speak english very bad, sorry. I´m learn very much in this forum. Thanks a lot

I´m have a cuestion

How I can combine this two scripts?

$strComputer = "."
 $objWMIService = ObjGet("winmgmts:\\" & $strComputer & "\root\cimv2")

 $colMonitoredEvents = $objWMIService.ExecNotificationQuery _
    ("SELECT * FROM __InstanceOperationEvent WITHIN 5 WHERE " _
        & "Targetinstance ISA 'CIM_DirectoryContainsFile' and " _
            & "TargetInstance.GroupComponent= " _
                & "'Win32_Directory.Name=""c:\\\\1""'")

While 1
     $objEventObject = $colMonitoredEvents.NextEvent()

    Select
        Case $objEventObject.Path_.Class()="__InstanceCreationEvent"
           ConsoleWrite ("A new file was just created: " & $objEventObject.TargetInstance.PartComponent() & @CR)
        Case $objEventObject.Path_.Class()="__InstanceDeletionEvent"
           ConsoleWrite ("A file was just deleted: " & $objEventObject.TargetInstance.PartComponent() & @CR)
    EndSelect
WEndoÝ÷ Ù«­¢+Ø%¹±Õ±Ðí
½¹ÍѹÑ̹ÔÌÐì(9½QÉå%½¸()=ÁÐ ÅÕ½ÐíQÉå5¹Õ5½ÅÕ½Ðì°Ä¤$ìÕ±ÐÑÉ䵹ԥѵ̡MÉ¥ÁÐAÕͽá¥Ð¤Ý¥±°¹½ÐÍ¡½Ý¸¸((ÀÌØíÍÑÑ¥¹Í¥Ñ´$ôQÉå
ÉÑ5¹Ô ÅÕ½ÐíMÑÑ¥¹ÌÅÕ½Ðì¤(ÀÌØí¥ÍÁ±å¥Ñ´$ôQÉå
ÉÑ%Ñ´ ÅÕ½Ðí¥ÍÁ±äÅÕ½Ðì°ÀÌØíÍÑÑ¥¹Í¥Ñ´¤(ÀÌØíÁÉ¥¹ÑɥѴ$ôQÉå
ÉÑ%Ñ´ ÅÕ½ÐíAÉ¥¹ÑÈÅÕ½Ðì°ÀÌØíÍÑÑ¥¹Í¥Ñ´¤)QÉå
ÉÑ%Ñ´ ÅÕ½ÐìÅÕ½Ðì¤(ÀÌØí½ÕѥѴ$$ôQÉå
ÉÑ%Ñ´ ÅÕ½Ðí½ÕÐÅÕ½Ðì¤)QÉå
ÉÑ%Ñ´ ÅÕ½ÐìÅÕ½Ðì¤(ÀÌØíá¥Ñ¥Ñ´$$ôQÉå
ÉÑ%Ñ´ ÅÕ½Ðíá¥ÐÅÕ½Ðì¤()QÉåMÑMÑÑ ¤()]¡¥±Ä($ÀÌØíµÍôQÉåÑ5Í ¤(%M±Ð($%
ÍÀÌØíµÍôÀ($$%
½¹Ñ¥¹Õ1½½À($%
ÍÀÌØíµÍôÀÌØí½ÕѥѴ($$%5ͽà ØаÅÕ½Ðí½ÕÐèÅÕ½Ðì°ÅÕ½ÐíÕѽ%Ð̵QÉäµÍµÁ±ÅÕ½Ðì¤($%
ÍÀÌØíµÍôÀÌØíá¥Ñ¥Ñ´($$%á¥Ñ1½½À(%¹M±Ð)]¹()á¥Ð
Link to comment
Share on other sites

Hi

I´m speak english very bad, sorry. I´m learn very much in this forum. Thanks a lot

I´m have a cuestion

How I can combine this two scripts?

$strComputer = "."
 $objWMIService = ObjGet("winmgmts:\\" & $strComputer & "\root\cimv2")

 $colMonitoredEvents = $objWMIService.ExecNotificationQuery _
    ("SELECT * FROM __InstanceOperationEvent WITHIN 5 WHERE " _
        & "Targetinstance ISA 'CIM_DirectoryContainsFile' and " _
            & "TargetInstance.GroupComponent= " _
                & "'Win32_Directory.Name=""c:\\\\1""'")

While 1
     $objEventObject = $colMonitoredEvents.NextEvent()

    Select
        Case $objEventObject.Path_.Class()="__InstanceCreationEvent"
           ConsoleWrite ("A new file was just created: " & $objEventObject.TargetInstance.PartComponent() & @CR)
        Case $objEventObject.Path_.Class()="__InstanceDeletionEvent"
           ConsoleWrite ("A file was just deleted: " & $objEventObject.TargetInstance.PartComponent() & @CR)
    EndSelect
WEnd

#Include <Constants.au3>
#NoTrayIcon

Opt("TrayMenuMode",1)  ; Default tray menu items (Script Paused/Exit) will not be shown.

$settingsitem   = TrayCreateMenu("Settings")
$displayitem    = TrayCreateItem("Display", $settingsitem)
$printeritem    = TrayCreateItem("Printer", $settingsitem)
TrayCreateItem("")
$aboutitem  = TrayCreateItem("About")
TrayCreateItem("")
$exititem     = TrayCreateItem("Exit")

TraySetState()

While 1
    $msg = TrayGetMsg()
    Select
        Case $msg = 0
            ContinueLoop
        Case $msg = $aboutitem
            Msgbox(64,"About:","AutoIt3-Tray-sample")
        Case $msg = $exititem
            ExitLoop
    EndSelect
WEnd

Exit
I think you should change the second script to TrayOnEventMode = 1 and assign functions to the $aboutitem and $exititem events. Then the While/WEnd loop from the first script will keep the script running instead of the TrayGetMsg() loop in the second.

:D

Valuater's AutoIt 1-2-3, Class... Is now in Session!For those who want somebody to write the script for them: RentACoder"Any technology distinguishable from magic is insufficiently advanced." -- Geek's corollary to Clarke's law
Link to comment
Share on other sites

Hi again

I probe this (and another things...) but the menu don´t work

#Include <Constants.au3>
#NoTrayIcon

Opt("TrayMenuMode",1)   ; Default tray menu items (Script Paused/Exit) will not be shown.
Opt("TrayOnEventMode",1)

$aboutitem  = TrayCreateItem("About")
GUICtrlSetOnEvent($aboutitem, "aboutitemm")
TrayCreateItem("")
$exititem = TrayCreateItem("Exit")
GUICtrlSetOnEvent($exititem, "exititemm")
TraySetState()

$Dir = "E:\ed2k"
$Dir=StringReplace($Dir,"\","\\\\")

$strComputer = "."
$objWMIService = ObjGet("winmgmts:\\" & $strComputer & "\root\cimv2")

$colMonitoredEvents = $objWMIService.ExecNotificationQuery _
    ("SELECT * FROM __InstanceOperationEvent WITHIN 3 WHERE " _
        & "Targetinstance ISA 'CIM_DirectoryContainsFile' and " _
            & "TargetInstance.GroupComponent= " _
                & "'Win32_Directory.Name=""" & $Dir & """'")        

While 1     
    $objEventObject = $colMonitoredEvents.NextEvent()           
    Select
        Case $objEventObject.Path_.Class()="__InstanceCreationEvent"            
            $NewFile=$objEventObject.TargetInstance.PartComponent()
            $desde=StringInStr($NewFile,"Name")+6
            $Hasta=Stringlen($NewFile)- $desde
            $NewFile=StringMid ($NewFile,$desde,$Hasta)
            $NewFile=StringReplace($NewFile,"\\","\")
            ConsoleWrite($NewFile & @LF)
            TrayTip ( "Nuevo archivo", "A new file was just created: " & @CR & $NewFile, 3, 1 )
        Case $objEventObject.Path_.Class()="__InstanceDeletionEvent"
            ConsoleWrite ("A file was just deleted: " & $objEventObject.TargetInstance.PartComponent() & @CR)   
    EndSelect   
WEnd      

Func aboutitemm()
    Msgbox(64,"about:","AutoIt3-Tray-sample")
EndFunc

Func exititemm()
    Exit
EndFuncoÝ÷ Ø Ý¶¬víÂäj¢«­¢+ØÀÌØí½ÕѥѴ$ôQÉå
ÉÑ%Ñ´ ÅÕ½Ðí½ÕÐÅÕ½Ðì¤)QÉåMÑ=¹Ù¹Ð ÀÌØí½ÕѥѴ°ÅÕ½Ðí½Õѥѵ´ÅÕ½Ðì¤)QÉå
ÉÑ%Ñ´ ÅÕ½ÐìÅÕ½Ðì¤(ÀÌØíá¥Ñ¥Ñ´ôQÉå
ÉÑ%Ñ´ ÅÕ½Ðíá¥ÐÅÕ½Ðì¤)QÉåMÑ=¹Ù¹Ð ÀÌØíá¥Ñ¥Ñ´°ÅÕ½Ðíá¥Ñ¥Ñµ´ÅÕ½Ðì¤)QÉåMÑMÑÑ ¤((¸¸¸
Edited by arredemo
Link to comment
Share on other sites

in this form, the msgbox only show when a event is created.

For example. If a new file is created, show msgbox and after show the traytip but if no event no msgbox

#Include <Constants.au3>
#NoTrayIcon

Opt("TrayMenuMode",1)   ; Default tray menu items (Script Paused/Exit) will not be shown.
Opt("TrayOnEventMode",1)

$aboutitem  = TrayCreateItem("About")
TrayItemSetOnEvent (-1, "aboutitemm")

TrayCreateItem("")

$exititem = TrayCreateItem("Exit")
TrayItemSetOnEvent (-1, "exititemm")

TraySetState()


Func aboutitemm()
    Msgbox(64,"about:","AutoIt3-Tray-sample")
EndFunc

Func exititemm()
    Exit
EndFunc


$Dir = "E:\ed2k"
$Dir=StringReplace($Dir,"\","\\\\")

$strComputer = "."
$objWMIService = ObjGet("winmgmts:\\" & $strComputer & "\root\cimv2")

$colMonitoredEvents = $objWMIService.ExecNotificationQuery _
    ("SELECT * FROM __InstanceOperationEvent WITHIN 3 WHERE " _
        & "Targetinstance ISA 'CIM_DirectoryContainsFile' and " _
            & "TargetInstance.GroupComponent= " _
                & "'Win32_Directory.Name=""" & $Dir & """'")        

While 1     
    $objEventObject = $colMonitoredEvents.NextEvent()           
    Select
        Case $objEventObject.Path_.Class()="__InstanceCreationEvent"            
            $NewFile=$objEventObject.TargetInstance.PartComponent()
            $desde=StringInStr($NewFile,"Name")+6
            $Hasta=Stringlen($NewFile)- $desde
            $NewFile=StringMid ($NewFile,$desde,$Hasta)
            $NewFile=StringReplace($NewFile,"\\","\")
            ConsoleWrite($NewFile & @LF)
            TrayTip ( "Nuevo archivo", "A new file was just created: " & @CR & $NewFile, 3, 1 )
        Case $objEventObject.Path_.Class()="__InstanceDeletionEvent"
            ConsoleWrite ("A file was just deleted: " & $objEventObject.TargetInstance.PartComponent() & @CR)   
    EndSelect   
WEnd
Link to comment
Share on other sites

Take a look at this topic: Problem with OnEvent mode combined with Objects

Note that .NextEvent() is a blocking method even if you hadn't used TrayOnEventMode. You need an Async method. See the help file under ObjEvent().

:D

Edited by PsaltyDS
Valuater's AutoIt 1-2-3, Class... Is now in Session!For those who want somebody to write the script for them: RentACoder"Any technology distinguishable from magic is insufficiently advanced." -- Geek's corollary to Clarke's law
Link to comment
Share on other sites

Well, this work but I´m not sure Why

Thanks a lot

#Include <Constants.au3>
#NoTrayIcon

$oMyError = ObjEvent("AutoIt.Error","_COMError")

Opt("TrayMenuMode",1)   ; Default tray menu items (Script Paused/Exit) will not be shown.
Opt("TrayOnEventMode",1)

$aboutitem  = TrayCreateItem("About")
TrayItemSetOnEvent (-1, "aboutitemm")

TrayCreateItem("")

$exititem = TrayCreateItem("Exit")
TrayItemSetOnEvent (-1, "exititemm")

TraySetState()

Func aboutitemm()
    Msgbox(64,"about:","AutoIt3-Tray-sample")
EndFunc

Func exititemm()
    Exit
EndFunc 

$Dir = ""E:\ed2k"
$Dir=StringReplace($Dir,"\","\\\\")

$strComputer = "."
$objWMIService = ObjGet("winmgmts:\\" & $strComputer & "\root\cimv2")

;~ ObjEvent($objWMIService,"aboutitemm")

$colMonitoredEvents = $objWMIService.ExecNotificationQuery _
    ("SELECT * FROM __InstanceOperationEvent WITHIN 3 WHERE " _
        & "Targetinstance ISA 'CIM_DirectoryContainsFile' and " _
            & "TargetInstance.GroupComponent= " _
                & "'Win32_Directory.Name=""" & $Dir & """'")        

While 1     
    $objEventObject = $colMonitoredEvents.NextEvent(100)
    If Not @error Then      
    Switch $objEventObject.Path_.Class()
        Case "__InstanceCreationEvent"      
            $NewFile=$objEventObject.TargetInstance.PartComponent()
            $desde=StringInStr($NewFile,"Name")+6
            $Hasta=Stringlen($NewFile)- $desde
            $NewFile=StringMid ($NewFile,$desde,$Hasta)
            $NewFile=StringReplace($NewFile,"\\","\")
            ConsoleWrite($NewFile & @LF)
            TrayTip ( "Nuevo archivo", "A new file was just created: " & @CR & $NewFile, 3, 1 )
        Case "__InstanceDeletionEvent"
            ConsoleWrite ("A file was just deleted: " & $objEventObject.TargetInstance.PartComponent() & @CR)
    EndSwitch 
    Else
        ConsoleWrite("")
    EndIf
WEnd   
   
Func _COMError()       
    Local $err = $oMyError.number
    If $err = 0 Then $err = -1
    SetError($err)  ; to check for after this function returns
EndFunc
Edited by arredemo
Link to comment
Share on other sites

Well, this work but I´m not sure Why

Thanks a lot

CODE
#Include <Constants.au3>

#NoTrayIcon

$oMyError = ObjEvent("AutoIt.Error","_COMError")

Opt("TrayMenuMode",1) ; Default tray menu items (Script Paused/Exit) will not be shown.

Opt("TrayOnEventMode",1)

$aboutitem = TrayCreateItem("About")

TrayItemSetOnEvent (-1, "aboutitemm")

TrayCreateItem("")

$exititem = TrayCreateItem("Exit")

TrayItemSetOnEvent (-1, "exititemm")

TraySetState()

Func aboutitemm()

Msgbox(64,"about:","AutoIt3-Tray-sample")

EndFunc

Func exititemm()

Exit

EndFunc

$Dir = ""E:\ed2k"

$Dir=StringReplace($Dir,"\","\\\\")

$strComputer = "."

$objWMIService = ObjGet("winmgmts:\\" & $strComputer & "\root\cimv2")

;~ ObjEvent($objWMIService,"aboutitemm")

$colMonitoredEvents = $objWMIService.ExecNotificationQuery _

("SELECT * FROM __InstanceOperationEvent WITHIN 3 WHERE " _

& "Targetinstance ISA 'CIM_DirectoryContainsFile' and " _

& "TargetInstance.GroupComponent= " _

& "'Win32_Directory.Name=""" & $Dir & """'")

While 1

$objEventObject = $colMonitoredEvents.NextEvent(100)

If Not @error Then

Switch $objEventObject.Path_.Class()

Case "__InstanceCreationEvent"

$NewFile=$objEventObject.TargetInstance.PartComponent()

$desde=StringInStr($NewFile,"Name")+6

$Hasta=Stringlen($NewFile)- $desde

$NewFile=StringMid ($NewFile,$desde,$Hasta)

$NewFile=StringReplace($NewFile,"\\","\")

ConsoleWrite($NewFile & @LF)

TrayTip ( "Nuevo archivo", "A new file was just created: " & @CR & $NewFile, 3, 1 )

Case "__InstanceDeletionEvent"

ConsoleWrite ("A file was just deleted: " & $objEventObject.TargetInstance.PartComponent() & @CR)

EndSwitch

Else

ConsoleWrite("")

EndIf

WEnd

Func _COMError()

Local $err = $oMyError.number

If $err = 0 Then $err = -1

SetError($err) ; to check for after this function returns

EndFunc

It works because you applied a timeout to .NextEvent() and added a COM error handler to deal with the timeouts.

Without the timeout parameter, .NextEvent() is blocking. A timeout drives a COM error, so you have to handle that every time there is no event happening. Getting rid of the COM error is better than handling it (sometimes that's not an option).

If you could figure out the async method, it would call a desgnated function on the normal event, just as the COM error calls _COMError() here, but without any COM errors.

:D

Valuater's AutoIt 1-2-3, Class... Is now in Session!For those who want somebody to write the script for them: RentACoder"Any technology distinguishable from magic is insufficiently advanced." -- Geek's corollary to Clarke's law
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...