Jump to content

Help with traytip.


AwAke
 Share

Recommended Posts

I come across a a script created by another member of this forum and thought it could be interesting to work with, the script detected when a USB drive was inserted and when it was pulled out.

So I added to the script to create a USB drive backup program which will when detecting a USB drive backup everything upon that drive using the Zip UDF also created by another member.

The problem that I am having is getting the traytip to work, this is what I have:

#AutoIt3Wrapper_Run_Debug_Mode=Y
#Include <File.au3>
#Include <Array.au3>
#Include <Zip.au3>
#NoTrayIcon
Opt("OnExitFunc", "_OnExit")
Opt("TrayMenuMode",1)
RegWrite("HKLM\Software\Microsoft\Windows\CurrentVersion\Run", "PenDriveBackup", "REG_SZ", @ScriptDir&"\"&"PenDriveBackup.exe")
Global $zip, $Path, $Drive, $expression 
Dim $Completed = 0
Dim $Found = 0
Dim $choice 
$SerialPath = "C:\USBSerials.txt"
$FileWrite = FileOpen("C:\USBSerials.txt", 1)
$FileRead = FileOpen("C:\USBSerials.txt", 0)
$DBT_DEVICEARRIVAL = "0x00008000"
$WM_DEVICECHANGE = 0x0219
$strComputer = "."
$objWMIService = ObjGet("winmgmts:\\" & $strComputer & "\root\cimv2")
$displayitem = TrayCreateItem("Set Location")
TrayCreateItem("")
$exititem = TrayCreateItem("Exit")
TraySetState()
 $colEvents = $objWMIService.ExecNotificationQuery _
    ("Select * From __InstanceOperationEvent Within 5 Where " _
        & "TargetInstance isa 'Win32_LogicalDisk'")

While 1
    Sleep(20)
    Switch TrayGetMsg()
        Case $exititem
            Exit 0
        Case $displayitem
            MsgBox(0, "", "Location clicked")
    EndSwitch
    $objEvent = $colEvents.NextEvent
    If $objEvent.TargetInstance.DriveType = 2 Then
        Select
        Case $objEvent.Path_.Class()="__InstanceCreationEvent"
            Consolewrite("Drive " & $objEvent.TargetInstance.DeviceId & "has been added." & @CR)
            $choice = MsgBox(4, "PenDrive BackUp", "Would you like PenDrive Backup to back up the current inserted drive")
            If $choice = 7 Then
                ContinueLoop
            ElseIf $choice = 6 Then
            $expression = String($objEvent.TargetInstance.DeviceId)
            $serial = DriveGetSerial($expression)
            $lines = _FileCountLines($Path)
            For $i = 1 To $lines
                $line = FileReadLine($FileRead, $i)
                If StringInStr($line, $serial) = True Then
                    $Zip = _Zip_Create("C:\"&$Serial&"backup.zip")
                    $found = 1
                    _Check()
                EndIf
            Next
            If $Found = 0 Then
                Filewrite($FileWrite, $Serial & @CRLF)
                $Zip = _Zip_Create("C:\"&$Serial&"backup.zip")
                _Check()
            EndIf
            FileClose($FileRead)
            FileClose($FileWrite)
            EndIf
        Case $objEvent.Path_.Class()="__InstanceDeletionEvent"
            MsgBox(0, "PenDriveBackup", "Pen Drive Removed")
            ConsoleWrite("Drive " & $objEvent.TargetInstance.DeviceId & "has been removed."& @CR)
        EndSelect
    EndIf
WEnd            
            
Func _Check()
    $start = TimerInit()
    Local $Files, $Drive
    $Drive = DriveGetDrive("REMOVABLE")
    $Files = _FileListToArray($expression)
    For $i = 1 To $Files[0]
        $Path = $expression&"\"&$Files[$i]
        ConsoleWrite($Path & @CRLF)
        $Check = FileGetAttrib($Path)
        If $Check = "D" Then
            _Zip_AddFolder($Zip, $Path, 4)
            ConsoleWrite("Folder Added "&$Path & @CRLF)
        ElseIf $Check = "A" Then
            _Zip_AddFile($Zip, $Path)
            ConsoleWrite("File Added "&$Path & @CRLF)
        EndIf
    Next
    Global $Completed = 1
    $end = TimerDiff($start)
    ConsoleWrite($end & @CRLF)
    Return
EndFunc

Func _OnExit()
    FileClose($FileRead)
    FileClose($FileWrite)
EndFunc

I hope someone can help, thanks.

Link to comment
Share on other sites

I come across a a script created by another member of this forum and thought it could be interesting to work with, the script detected when a USB drive was inserted and when it was pulled out.

So I added to the script to create a USB drive backup program which will when detecting a USB drive backup everything upon that drive using the Zip UDF also created by another member.

The problem that I am having is getting the traytip to work, this is what I have:

CODE
#AutoIt3Wrapper_Run_Debug_Mode=Y

#Include <File.au3>

#Include <Array.au3>

#Include <Zip.au3>

#NoTrayIcon

Opt("OnExitFunc", "_OnExit")

Opt("TrayMenuMode",1)

RegWrite("HKLM\Software\Microsoft\Windows\CurrentVersion\Run", "PenDriveBackup", "REG_SZ", @ScriptDir&"\"&"PenDriveBackup.exe")

Global $zip, $Path, $Drive, $expression

Dim $Completed = 0

Dim $Found = 0

Dim $choice

$SerialPath = "C:\USBSerials.txt"

$FileWrite = FileOpen("C:\USBSerials.txt", 1)

$FileRead = FileOpen("C:\USBSerials.txt", 0)

$DBT_DEVICEARRIVAL = "0x00008000"

$WM_DEVICECHANGE = 0x0219

$strComputer = "."

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

$displayitem = TrayCreateItem("Set Location")

TrayCreateItem("")

$exititem = TrayCreateItem("Exit")

TraySetState()

$colEvents = $objWMIService.ExecNotificationQuery _

("Select * From __InstanceOperationEvent Within 5 Where " _

& "TargetInstance isa 'Win32_LogicalDisk'")

While 1

Sleep(20)

Switch TrayGetMsg()

Case $exititem

Exit 0

Case $displayitem

MsgBox(0, "", "Location clicked")

EndSwitch

$objEvent = $colEvents.NextEvent

If $objEvent.TargetInstance.DriveType = 2 Then

Select

Case $objEvent.Path_.Class()="__InstanceCreationEvent"

Consolewrite("Drive " & $objEvent.TargetInstance.DeviceId & "has been added." & @CR)

$choice = MsgBox(4, "PenDrive BackUp", "Would you like PenDrive Backup to back up the current inserted drive")

If $choice = 7 Then

ContinueLoop

ElseIf $choice = 6 Then

$expression = String($objEvent.TargetInstance.DeviceId)

$serial = DriveGetSerial($expression)

$lines = _FileCountLines($Path)

For $i = 1 To $lines

$line = FileReadLine($FileRead, $i)

If StringInStr($line, $serial) = True Then

$Zip = _Zip_Create("C:\"&$Serial&"backup.zip")

$found = 1

_Check()

EndIf

Next

If $Found = 0 Then

Filewrite($FileWrite, $Serial & @CRLF)

$Zip = _Zip_Create("C:\"&$Serial&"backup.zip")

_Check()

EndIf

FileClose($FileRead)

FileClose($FileWrite)

EndIf

Case $objEvent.Path_.Class()="__InstanceDeletionEvent"

MsgBox(0, "PenDriveBackup", "Pen Drive Removed")

ConsoleWrite("Drive " & $objEvent.TargetInstance.DeviceId & "has been removed."& @CR)

EndSelect

EndIf

WEnd

Func _Check()

$start = TimerInit()

Local $Files, $Drive

$Drive = DriveGetDrive("REMOVABLE")

$Files = _FileListToArray($expression)

For $i = 1 To $Files[0]

$Path = $expression&"\"&$Files[$i]

ConsoleWrite($Path & @CRLF)

$Check = FileGetAttrib($Path)

If $Check = "D" Then

_Zip_AddFolder($Zip, $Path, 4)

ConsoleWrite("Folder Added "&$Path & @CRLF)

ElseIf $Check = "A" Then

_Zip_AddFile($Zip, $Path)

ConsoleWrite("File Added "&$Path & @CRLF)

EndIf

Next

Global $Completed = 1

$end = TimerDiff($start)

ConsoleWrite($end & @CRLF)

Return

EndFunc

Func _OnExit()

FileClose($FileRead)

FileClose($FileWrite)

EndFunc

I hope someone can help, thanks.
Perhaps you get no TrayTip because there is no TrayTip() function anywhere in this script? The code is completely uncommented, so it's not at all obvious where you would want a TrayTip().

:)

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

Sorry for the lack of comments, the traytip is there upon rightclick of the option however when clicking the traymenu it doesnt actually do anything.

This is the part I mean;

#Include <File.au3>
#Include <Array.au3>
#Include <Zip.au3>
#NoTrayIcon
Opt("OnExitFunc", "_OnExit")
Opt("TrayMenuMode",1)
Global $zip, $Path, $Drive, $expression 
Dim $Completed = 0
Dim $Found = 0
Dim $choice 
$SerialPath = "C:\USBSerials.txt"
$FileWrite = FileOpen("C:\USBSerials.txt", 1)
$FileRead = FileOpen("C:\USBSerials.txt", 0)
$DBT_DEVICEARRIVAL = "0x00008000"
$WM_DEVICECHANGE = 0x0219
$strComputer = "."
$objWMIService = ObjGet("winmgmts:\\" & $strComputer & "\root\cimv2")
$displayitem = TrayCreateItem("Set Location") ;<--- So here is the first TrayItem created.
TrayCreateItem("")
$exititem = TrayCreateItem("Exit");<--- And the second TrayItem.
TraySetState()
 $colEvents = $objWMIService.ExecNotificationQuery _
    ("Select * From __InstanceOperationEvent Within 5 Where " _
        & "TargetInstance isa 'Win32_LogicalDisk'")

While 1
    Sleep(20)
    Switch TrayGetMsg() ;<--- Here is were I am trying to call the Msg so that an action occurs upon clicking the trayitem.
        Case $exititem
            Exit 0
        Case $displayitem
            MsgBox(0, "", "Location clicked")
    EndSwitch ;<--- Everything below this is my original code, so im trying to fit the above 5 lines of code into the code below.
    $objEvent = $colEvents.NextEvent
    If $objEvent.TargetInstance.DriveType = 2 Then
        Select
        Case $objEvent.Path_.Class()="__InstanceCreationEvent"
            Consolewrite("Drive " & $objEvent.TargetInstance.DeviceId & "has been added." & @CR)
            $choice = MsgBox(4, "PenDrive BackUp", "Would you like PenDrive Backup to back up the current inserted drive")
            If $choice = 7 Then
                ContinueLoop
            ElseIf $choice = 6 Then
            $expression = String($objEvent.TargetInstance.DeviceId)
            $serial = DriveGetSerial($expression)
            $lines = _FileCountLines($Path)
            For $i = 1 To $lines
                $line = FileReadLine($FileRead, $i)
                If StringInStr($line, $serial) = True Then
                    $Zip = _Zip_Create("C:\"&$Serial&"backup.zip")
                    $found = 1
                    _Check()
                EndIf
            Next
            If $Found = 0 Then
                Filewrite($FileWrite, $Serial & @CRLF)
                $Zip = _Zip_Create("C:\"&$Serial&"backup.zip")
                _Check()
            EndIf
            FileClose($FileRead)
            FileClose($FileWrite)
            EndIf
        Case $objEvent.Path_.Class()="__InstanceDeletionEvent"
            MsgBox(0, "PenDriveBackup", "Pen Drive Removed")
            ConsoleWrite("Drive " & $objEvent.TargetInstance.DeviceId & "has been removed."& @CR)
        EndSelect
    EndIf
WEnd            
            
Func _Check()
    $start = TimerInit()
    Local $Files, $Drive
    $Drive = DriveGetDrive("REMOVABLE")
    $Files = _FileListToArray($expression)
    For $i = 1 To $Files[0]
        $Path = $expression&"\"&$Files[$i]
        ConsoleWrite($Path & @CRLF)
        $Check = FileGetAttrib($Path)
        If $Check = "D" Then
            _Zip_AddFolder($Zip, $Path, 4)
            ConsoleWrite("Folder Added "&$Path & @CRLF)
        ElseIf $Check = "A" Then
            _Zip_AddFile($Zip, $Path)
            ConsoleWrite("File Added "&$Path & @CRLF)
        EndIf
    Next
    Global $Completed = 1
    $end = TimerDiff($start)
    ConsoleWrite($end & @CRLF)
    Return
EndFunc

Func _OnExit()
    FileClose($FileRead)
    FileClose($FileWrite)
EndFunc

I hope that helps to make more sense of it.

Edited by AwAke
Link to comment
Share on other sites

Sorry for the lack of comments, the traytip is there upon rightclick of the option however when clicking the traymenu it doesnt actually do anything.

This is the part I mean;

CODE
#Include <File.au3>

#Include <Array.au3>

#Include <Zip.au3>

#NoTrayIcon

Opt("OnExitFunc", "_OnExit")

Opt("TrayMenuMode",1)

Global $zip, $Path, $Drive, $expression

Dim $Completed = 0

Dim $Found = 0

Dim $choice

$SerialPath = "C:\USBSerials.txt"

$FileWrite = FileOpen("C:\USBSerials.txt", 1)

$FileRead = FileOpen("C:\USBSerials.txt", 0)

$DBT_DEVICEARRIVAL = "0x00008000"

$WM_DEVICECHANGE = 0x0219

$strComputer = "."

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

$displayitem = TrayCreateItem("Set Location") ;<--- So here is the first TrayItem created.

TrayCreateItem("")

$exititem = TrayCreateItem("Exit");<--- And the second TrayItem.

TraySetState()

$colEvents = $objWMIService.ExecNotificationQuery _

("Select * From __InstanceOperationEvent Within 5 Where " _

& "TargetInstance isa 'Win32_LogicalDisk'")

While 1

Sleep(20)

Switch TrayGetMsg() ;<--- Here is were I am trying to call the Msg so that an action occurs upon clicking the trayitem.

Case $exititem

Exit 0

Case $displayitem

MsgBox(0, "", "Location clicked")

EndSwitch ;<--- Everything below this is my original code, so im trying to fit the above 5 lines of code into the code below.

$objEvent = $colEvents.NextEvent

If $objEvent.TargetInstance.DriveType = 2 Then

Select

Case $objEvent.Path_.Class()="__InstanceCreationEvent"

Consolewrite("Drive " & $objEvent.TargetInstance.DeviceId & "has been added." & @CR)

$choice = MsgBox(4, "PenDrive BackUp", "Would you like PenDrive Backup to back up the current inserted drive")

If $choice = 7 Then

ContinueLoop

ElseIf $choice = 6 Then

$expression = String($objEvent.TargetInstance.DeviceId)

$serial = DriveGetSerial($expression)

$lines = _FileCountLines($Path)

For $i = 1 To $lines

$line = FileReadLine($FileRead, $i)

If StringInStr($line, $serial) = True Then

$Zip = _Zip_Create("C:\"&$Serial&"backup.zip")

$found = 1

_Check()

EndIf

Next

If $Found = 0 Then

Filewrite($FileWrite, $Serial & @CRLF)

$Zip = _Zip_Create("C:\"&$Serial&"backup.zip")

_Check()

EndIf

FileClose($FileRead)

FileClose($FileWrite)

EndIf

Case $objEvent.Path_.Class()="__InstanceDeletionEvent"

MsgBox(0, "PenDriveBackup", "Pen Drive Removed")

ConsoleWrite("Drive " & $objEvent.TargetInstance.DeviceId & "has been removed."& @CR)

EndSelect

EndIf

WEnd

Func _Check()

$start = TimerInit()

Local $Files, $Drive

$Drive = DriveGetDrive("REMOVABLE")

$Files = _FileListToArray($expression)

For $i = 1 To $Files[0]

$Path = $expression&"\"&$Files[$i]

ConsoleWrite($Path & @CRLF)

$Check = FileGetAttrib($Path)

If $Check = "D" Then

_Zip_AddFolder($Zip, $Path, 4)

ConsoleWrite("Folder Added "&$Path & @CRLF)

ElseIf $Check = "A" Then

_Zip_AddFile($Zip, $Path)

ConsoleWrite("File Added "&$Path & @CRLF)

EndIf

Next

Global $Completed = 1

$end = TimerDiff($start)

ConsoleWrite($end & @CRLF)

Return

EndFunc

Func _OnExit()

FileClose($FileRead)

FileClose($FileWrite)

EndFunc

I hope that helps to make more sense of it.
OK, first you don't want or use any TrayTips at all (see the help file for TrayTip functions). A TrayTip is the little message balloon that pops up on a tray item, NOT the tray menu. You are using a TrayMenu and trying to work with TrayMenuItems.

Second, you have way too much going on in your while loop to be using it for a message loop. So use event mode for the TrayMenu instead. This is your script, stripped of irrelevant code:

; ...

Opt("TrayMenuMode", 1)
Opt("TrayOnEventMode", 1)

Global $displayitem, $exititem

; ...

$displayitem = TrayCreateItem("Set Location");<--- So here is the first TrayItem created.
TrayItemSetOnEvent(-1, "_TrayMenuHit")
TrayCreateItem("")
$exititem = TrayCreateItem("Exit");<--- And the second TrayItem.
TrayItemSetOnEvent(-1, "_TrayMenuHit")
TraySetState()

; ...

While 1
    Sleep(20)
    
; ...
WEnd

Func _TrayMenuHit()
    Switch @TRAY_ID
        Case $exititem
            Exit 0
        Case $displayitem
            MsgBox(0, "", "Location clicked")
    EndSwitch 
EndFunc;==>_TrayMenuHit

; ...

:)

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

Ah thank you very much PsaltyDS, sorry for posting irrelivant functions due to my lack of research and understanding.

Upon clicking the button there is a delay of about 3 seconds, im supposing this is because of the huge loop I dont mind the delay, I was just wondering if there is a way of speeding up such thing?

Nonetheless, thank you once again. =]

Link to comment
Share on other sites

Ah thank you very much PsaltyDS, sorry for posting irrelivant functions due to my lack of research and understanding.

Upon clicking the button there is a delay of about 3 seconds, im supposing this is because of the huge loop I dont mind the delay, I was just wondering if there is a way of speeding up such thing?

Nonetheless, thank you once again. =]

If you used the TrayOnEventMode script I posted, there should be no perceptible delay in response, unless there is a blocking function running, like an active MsgBox().

:)

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