Jump to content


Photo

Actions on taskbar's tray-icon via AHK possible?


  • Please log in to reply
50 replies to this topic

#1 Corvinian

Corvinian
  • Members
  • 4 posts

Posted 17 February 2009 - 05:48 PM

I need to automate temporary disabling COMODO CIS Defense+ during backup run.
There is no commandline switch to do this, but can be done via taskbar tray-icon.

So I need a hint about the proper command(s) to code it myself.
I want to find the tray-icon with text 'COMODO Internet Security' and perform mouse-clicks on it/submenus. Is it possible with AHK?

I tried with AutoSCriptWriter and AU3-Spy, but did not succeed.

Since there's initially no window to focus on and send relative mouseclick coordinates to, my approach does not work :(

I want to do this:
Now I have automated my backups and Defense+ shows dozens of messages for me to decide to allow/disallow them. As I use Cygwin Linux-emulation under Windows, the tools should not universally be allowed, so I must disable Defense+ first or the backups won't run. I can do this manually via the GUI in taskbar 'Defense+ Security Level -> Disabled'.

#2 UweD

UweD
  • Members
  • 34 posts

Posted 18 February 2009 - 03:54 AM

Hi,
it is difficult to understand what you want to do. I suppose you want to "answer" dialogues with AHK?
Then make a list of all Dialogues and what you want to do with them. Use WindowSpy (ahk tray icon right click) and memorize all dialogue's ahk classes (if they are identicall additionla also their Window title)

You can then create a macro which answers those dialogues by sending keystrkes or mouseclick to the buttons or controls you like.
You can use WinWait to wait for a secific dialogue and you can use IfWinExist to see if it is there. Then you can use ControlSend,,KEYS,TARGET (omit second parameter)...

#3 Corvinian

Corvinian
  • Members
  • 4 posts

Posted 18 February 2009 - 12:42 PM

Hi UweD,

thanks for your reply.

I have read the tutorials,FAQs and basically know how to automate dialogs/Windows actions with AHK, but do not know how to operate on non-Windows, like the taskbar tray-icon:

I tested it this way:

IfWinExist COMODO Internet Security
{
    WinActivate
}
else
{
  MsgBox Cannot find Window 'COMODO Internet Security'!!
}

=> Not working, the messagebox in the else-clause is shown.

----
Output of WindowsSpy:
## right click on TrayIcon (the action I want) ##

>>>>>>>>>>( Window Title & Class )<<<<<<<<<<<
COMODO Internet Security
ahk_class #32770

>>>>>>>>>>>>( Mouse Position )<<<<<<<<<<<<<
On Screen:	859, 333  (less often used)
In Active Window:	-421, 105

>>>>>>>>>( Now Under Mouse Cursor )<<<<<<<<

Color:	0xC8D0D4  (Blue=C8 Green=D0 Red=D4)

>>>>>>>>>>( Active Window Position )<<<<<<<<<<
left: 1280     top: 228     width: 788     height: 544

>>>>>>>>>>>( Status Bar Text )<<<<<<<<<<

>>>>>>>>>>>( Visible Window Text )<<<<<<<<<<<
SUMMARY
FIREWALL
DEFENSE+
MISCELLANEOUS
ANTIVIRUS
Common Tasks
View Antivirus Events
Run a Scan
Scan Profiles

[... 150 additional lines deleted]

>>>>>>>>>>>( Hidden Window Text )<<<<<<<<<<<
Proactive Defense
The Defense+ has blocked
suspicious attempt(s) so far.
0
Run Diagnostics

>>>>( TitleMatchMode=slow Visible Text )<<<<

>>>>( TitleMatchMode=slow Hidden Text )<<<<

Can I still automate this with AHK?

To understand this better, I have made a screenshot of the TrayIcon part and the opened menu I want to script:
http://neodor.com/au..._2-Monitors.PNG

The TrayIcon directly next to the windows clock (the shield icon) is the CIS icon I want to automate. Everything on the right side of the windows clock is the second monitor, so my desktop/working area is two panels wide.

#4 mai9

mai9
  • Members
  • 63 posts

Posted 18 February 2009 - 02:44 PM

I am interested in opening a trayicon menu aswell. In my case to safely remove a flash memory. I'll keep an eye on this topic :)

#5 SKAN

SKAN
  • Administrators
  • 9062 posts

Posted 18 February 2009 - 08:27 PM

Tested and works

DetectHiddenWindows, On
TI := [color=red]TrayIcons( "cfp.exe" )[/color]
StringSplit,TIV, TI, |
uID  := RegExReplace( TIV4, "uID: " )
Msg  := RegExReplace( TIV5, "MessageID: " )
hWnd := RegExReplace( TIV6, "hWnd: " )

PostMessage, Msg, uID,0x204,, ahk_id %hWnd% ; Right Click down
PostMessage, Msg, uID,0x205,, ahk_id %hWnd% ; Right Click Up

Dependency: You need Sean's TrayIcons()

#6 SKAN

SKAN
  • Administrators
  • 9062 posts

Posted 18 February 2009 - 09:07 PM

I am interested in opening a trayicon menu aswell. In my case to safely remove a flash memory. I'll keep an eye on this topic :)


Tested in XP SP2 and Works!

#NoTrayIcon
DetectHiddenWindows, On
SafelyRemoveHardware()

SafelyRemoveHardware() {
 TrayInfo := [color=red]TrayIcons()[/color]
 StringReplace, TrayInfo, TrayInfo, `n%A_Space%,, All
 Loop, Parse, TrayInfo, `n
  If ( SubStr(A_LoopField, -21) = "Safely Remove Hardware" ) {
    TI := A_LoopField
    Break
  }
 IfEqual,TI,,Return
 StringSplit,TIV, TI, |
 uID  := RegExReplace( TIV4, "uID: " )
 Msg  := RegExReplace( TIV5, "MessageID: " )
 hWnd := RegExReplace( TIV6, "hWnd: " )
 SendMessage, Msg, uID,0x201,, ahk_id %hWnd% ; Left Click down
 SendMessage, Msg, uID,0x202,, ahk_id %hWnd% ; Left Click Up
 WinWaitActive, Power Meter ahk_class SystemTray_Main ; Wait for Tray Menu
 Send {Down}{Enter} ; Select the First Drive menu Item
 Sleep 1000
 ControlClick,,ahk_class tooltips_class32 ; Dismiss Traytip: "Safe To Remove Hardware"
}

Dependency: You need Sean's TrayIcons()

#7 mai9

mai9
  • Members
  • 63 posts

Posted 18 February 2009 - 10:35 PM

Thanks SKAN. I tested it and it flickers that menu, but it doesn't stay and it seems like it clicks the first item.

I am using WinXP SP2 aswell.

#8 SKAN

SKAN
  • Administrators
  • 9062 posts

Posted 19 February 2009 - 05:43 AM

Does the flash Drive gets removed or not? :roll:

#9 pajenn

pajenn
  • Members
  • 384 posts

Posted 20 February 2009 - 03:24 PM

Thank you for posting the examples SKAN. This may be a dumb question, but by "dependency" do you mean that anyone who uses your examples should "#include" the dependency too, or is there some other (better) way/command to call the linked function?

I'm trying your method out with Ghost-It, a tiny stand-alone system tray based utility for making windows translucent.

But I have 2 issues:

1. There's an unusually long delay (~5 sec) until the penultimate line of my code below is implemented. That is, Ghost-It's tray menu pops up as soon as I hit the hotkey (Ctrl+Win+RightClick), but AHK does not "SEND {DOWN 2}{ENTER}" to the menu for another 5 seconds; I can click 'Un-Ghost' or press down-down-enter to select it faster myself. What's wrong with the code?

#Include TrayIcon.ahk

^#LBUTTON:: ; Ctrl + Win + Left-Click to ghost
IfWinNotExist, ahk_class _GhostIt_Class
  {
    Run, "C:\Program Files\ghostit\GhostIt.exe"
    Sleep, 200
    TI := TrayIcons( "GhostIt.exe" ) 
    StringSplit,TIV, TI, |
    uID  := RegExReplace( TIV4, "uID: " )
    Msg  := RegExReplace( TIV5, "MessageID: " )
    hWnd := RegExReplace( TIV6, "hWnd: " )
  }
SendMessage, Msg, uID,0x203,, ahk_id %hWnd% ; Dbl Left-Click
Return

^#LBUTTON:: ; Ctrl + Win + Right-Click to un-ghost
SendMessage, Msg, uID,0x204,, ahk_id %hWnd% ; Right-Click down
SendMessage, Msg, uID,0x205,, ahk_id %hWnd% ; Right-Click Up
Send {DOWN 2}{ENTER} ; select Un-Ghost from pop-up menu
Return

2. TrayIcon.ahk popup: How do I dismiss it automatically? And for that matter, am I using TrayIcon.ahk correctly by including it at the start of the script, or is there a better way?

I mean, if I run TrayIcon first to look up the values for Ghost-It's uID (10001), Msg (1224) and hWnd (2490766), then all I need is the code below:

DetectHiddenWindows, On

^#LBUTTON::SendMessage, 1224, 10001,0x203,, ahk_id 2490766

^#RBUTTON::
SendMessage, 1224, 10001,0x204,, ahk_id 2490766
SendMessage, 1224, 10001,0x205,, ahk_id 2490766
Send {DOWN 2}{ENTER}
Return

The problem is that hWnd changes each time I restart Ghost-It, so that's why I've included TrayIcon.ahk in the script. But since it wasn't explicitly "included" in the other examples I've seen, I'm wondering if that's just something that's assumed to be obvious, or whether I missed something because I'm new to the forum and new to AHK?

#10 SKAN

SKAN
  • Administrators
  • 9062 posts

Posted 20 February 2009 - 09:10 PM

@pajenn

by "dependency" do you mean that anyone who uses your examples should "#include" the dependency too, or is there some other (better) way/command to call the linked function?


You may copy/paste the TrayIcons() function into your script.

1. There's an unusually long delay (~5 sec) until the penultimate line of my code below is implemented. That is, Ghost-It's tray menu pops up as soon as I hit the hotkey (Ctrl+Win+RightClick), but AHK does not "SEND {DOWN 2}{ENTER}" to the menu for another 5 seconds; I can click 'Un-Ghost' or press down-down-enter to select it faster myself. What's wrong with the code?


:shock: True!! I am unable to understand why this happens.. maybe Ghost-it is able to detect artificial clicks and slows down it!

2. TrayIcon.ahk popup: How do I dismiss it automatically? And for that matter, am I using TrayIcon.ahk correctly by including it at the start of the script, or is there a better way?


In this case, you may use the #Include as the last line of your script to avoid the popup.

I mean, if I run TrayIcon first to look up the values for Ghost-It's uID (10001), Msg (1224) and hWnd (2490766), then all I need is..


Good work! :D.. Use ahk_class instead of hard-coding the hwnd value, like in:

DetectHiddenWindows, On
IfWinNotExist, ahk_class _GhostIt_Class
  Run, "C:\Program Files\ghostit\GhostIt.exe"
Return

^#LButton::SendMessage, 1224, 10001,0x203,, ahk_class _GhostIt_Class

^#RBUTTON:: ; Ctrl + Win + Right-Click to un-ghost
 SendMessage, 1224, 10001,0x204,, ahk_class _GhostIt_Class
 SendMessage, 1224, 10001,0x205,, ahk_class _GhostIt_Class
 Send {DOWN 2}{ENTER}
Return


#11 pajenn

pajenn
  • Members
  • 384 posts

Posted 20 February 2009 - 11:35 PM

thank you :D

#12 Corvinian

Corvinian
  • Members
  • 4 posts

Posted 21 February 2009 - 09:17 AM

Sorry to reply late and thank you SKAN.

I tested the code and there's a popup, and in it the window for "cfp.exe" is shown, too. But all following commands are not executed anymore.

Update: I included the TrayIcon.ahk as you suggested last and it works!! :-)
Great code, now finding out how to select specific menu.

DetectHiddenWindows, On

TI := TrayIcons( "cfp.exe" )
StringSplit,TIV, TI, |
uID  := RegExReplace( TIV4, "uID: " )
Msg  := RegExReplace( TIV5, "MessageID: " )
hWnd := RegExReplace( TIV6, "hWnd: " )

MsgBox uID: %uID%, Msg: %Msg%, hWnd: %hWnd%

SendMessage, Msg, uID,0x204,, ahk_id %hWnd% ; Right Click down
SendMessage, Msg, uID,0x205,, ahk_id %hWnd% ; Right Click Up

SendMessage, Msg, uID,0x200,, Defense+ Security Level ahk_id %hWnd% ; Move mouse to this menu item?
; ABOVE command not working!
Sleep, 300
; SendMessage, Msg, uID,0x205,, ahk_id %hWnd% ; Right Click Up

MsgBox Now Defense+ should be disabled!

ExitApp

Now I am trying myself to find out how to disable the menu item.

Thanks again, AutoHotKey is really impressive :)

#13 SKAN

SKAN
  • Administrators
  • 9062 posts

Posted 21 February 2009 - 09:23 AM

thank you :D


No.. Thank you! I simplified Safely Remove Hardware

#NoTrayIcon
DetectHiddenWindows, On
Return

^F2::SafelyRemoveHardware()

SafelyRemoveHardware() {
 hWnd := WinExist( "Power Meter ahk_class SystemTray_Main" )
 SendMessage, 1226, 1226, 0x201,, ahk_id %hWnd% ; Left Click down
 SendMessage, 1226, 1226, 0x202,, ahk_id %hWnd% ; Left Click Up
 WinWaitActive, ahk_id %hWnd%,,1               ; Wait for SRH Tray left-click-Menu
 ControlSend,,{Down}{Enter},ahk_id %hWnd%      ; Select the first menu Item
 Send !{Esc}                                   ; Activate visible window
}


#14 SKAN

SKAN
  • Administrators
  • 9062 posts

Posted 21 February 2009 - 10:19 AM

1. There's an unusually long delay (~5 sec) until the penultimate line of my code below is implemented. That is, Ghost-It's tray menu pops up as soon as I hit the hotkey (Ctrl+Win+RightClick), but AHK does not "SEND {DOWN 2}{ENTER}" to the menu for another 5 seconds; I can click 'Un-Ghost' or press down-down-enter to select it faster myself. What's wrong with the code?


Oops... Sorry! SendMessage is waiting for a reply for 5 seconds!!!
Replace all SendMessage with PostMessage

#15 SKAN

SKAN
  • Administrators
  • 9062 posts

Posted 21 February 2009 - 10:32 AM

I am trying myself to find out how to disable the menu item.



DetectHiddenWindows, On

^F5::  ; Defense+ Security Level: [color=red]Safe Mode[/color]
ShowComodoTrayMenu()
Sleep 500
Send {Down 2}{Right}{Down [color=red]1[/color]}{Enter}
TrayTip, Defense+ Security Level, Safe Mode
Return

^F6::  ; Defense+ Security Level: [color=red]Disabled[/color]
ShowComodoTrayMenu()
Sleep 500
Send {Down 2}{Right}{Down [color=red]4[/color]}{Enter}
TrayTip, Defense+ Security Level, Disabled
Return

ShowComodoTrayMenu() {
 WinGet, W, List, ahk_class Afx:00400000:0
 hWnd := W%W%
 PostMessage, 10045, 335,0x206,, ahk_id %hWnd% ; Right Click down
 PostMessage, 10045, 335,0x205,, ahk_id %hWnd% ; Right Click Up
}