Jump to content

Sky Slate Blueberry Blackcurrant Watermelon Strawberry Orange Banana Apple Emerald Chocolate
Photo

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


  • Please log in to reply
51 replies to this topic
pajenn
  • Members
  • 391 posts
  • Last active: Feb 06 2015 07:57 AM
  • Joined: 07 Feb 2009

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


Works perfectly now. Thank you. Your effeorts are very much appreciated.

Hardware: fast laptop with SSD
Software: Win 7 Home Premium 64-bit, android for phone and tablet


Corvinian
  • Members
  • 4 posts
  • Last active: Feb 24 2009 05:42 PM
  • Joined: 15 Feb 2009
Hi SKAN, it works fine now.

Yesterday there was an update for COMODO Internet Security (CIS) and an additional menu-item 'Antivirus Security Level' was inserted before 'Firewall Security Level'.

So I made those slight modifications (and changing the Hotkeys, the Ctrl-F5/6 were not working on my system) to work with the most recent CIS-version below:

DetectHiddenWindows, On

; was ^F5::
^F8::  ; Defense+ Security Level: Safe Mode
ShowComodoTrayMenu()
Sleep 50
Send {Down 3}{Right}{Down 1}{Enter}
TrayTip, Defense+ Security Level, Safe Mode
Return

; was ^F6::
^F7::  ; Defense+ Security Level: Disabled
ShowComodoTrayMenu()
Sleep 50
Send {Down 3}{Right}{Down 4}{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
}


Working like a charm!!

Thanks, learned something new and have appetite for more now! :-)

mai9
  • Members
  • 63 posts
  • Last active: Aug 15 2011 04:23 PM
  • Joined: 17 Mar 2008

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

well, it seems it makes it remove the first item on the list, but I'd like to choose which one to remove!

Sean
  • Members
  • 2462 posts
  • Last active: Feb 07 2012 04:00 AM
  • Joined: 12 Feb 2007
You can retrieve the info of PopupMenu through MN_GETHMENU.
<!-- m -->http://www.autohotke...topic21451.html<!-- m -->

SKAN
  • Administrators
  • 9115 posts
  • Last active:
  • Joined: 26 Dec 2005
Dear Sean, Many Thanks for the Tip as well as the Code. :D

Safely Remove Hardware, with a Hotkey

#SingleInstance, Force
SetBatchLines -1
DetectHiddenWindows, On
CoordMode, Mouse, Screen
Loop 24 ; Assign Hotkeys for Drive Letters C thru Z
  HotKey % "+^" Chr(A_Index+66), EjectDrive 
Return

EjectDrive:
 Critical
 Drv := SubStr( A_ThisHotkey, 0 ) ":", nStr := ""
 DriveGet, DriveType, Type, %Drv%
 IfNotEqual, DriveType, Removable, Return
 hWnd := WinExist( "ahk_class SystemTray_Main" )
 MouseGetPos, X, Y
 MouseMove, A_ScreenWidth, A_ScreenHeight, 0
 PostMessage, 1226, 1226, 0x201,, ahk_id %hWnd% ; Left Click down
 PostMessage, 1226, 1226, 0x202,, ahk_id %hWnd% ; Left Click Up
 WinWaitActive, ahk_id %hWnd%,,5                ; Wait for SRH Tray left-click-Menu
 ; MN_GETHMENU : Code for retrieving popup menu text adapted from Sean's following post
 ;   Get Info from Context Menu: www.autohotkey.com/forum/viewtopic.php?p=137692#137692
 SendMessage, 0x1E1, 0,0,, ahk_class #32768
 hMenu := ErrorLevel
 Loop, % DllCall( "GetMenuItemCount", UInt,hMenu ) {
 idx := A_Index-1,  idn := DllCall( "GetMenuItemID", UInt,hMenu, Int,idx )
 nSize := DllCall( "GetMenuString", UInt,hMenu, Int,idx, Int,0, Int,0, UInt,0x400 ) + 1
 VarSetCapacity( mStr,nSize )
 DllCall( "GetMenuString", UInt,hMenu, Int,idx, Str,mStr, Int,nSize, UInt,0x400 )
 If InStr( mStr,Drv ) {
    ControlSend,,{Down %A_Index%}{Enter},ahk_id %hWnd%
    Break
   }
 } MouseMove, X, Y, 0
Return

Run the script and press Shift+Control+<Drive Letter> // Tested in XP & Vista

pajenn
  • Members
  • 391 posts
  • Last active: Feb 06 2015 07:57 AM
  • Joined: 07 Feb 2009

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

well, it seems it makes it remove the first item on the list, but I'd like to choose which one to remove!


another solution might be downloading USB Disk Ejector (free), and adding an AHK hotkey for it.

If it fails to remove the device, you could program AHK to try Unlocker or WhoLockMe on the flash drive to find out what process is keeping it locked, and then shut the offending program down manually. For example, if someone had left an iso image mounted or a document open from the flash drive, these unlockers would probably tell you. (Some AntiVirus programs may block them though).

Hardware: fast laptop with SSD
Software: Win 7 Home Premium 64-bit, android for phone and tablet


mai9
  • Members
  • 63 posts
  • Last active: Aug 15 2011 04:23 PM
  • Joined: 17 Mar 2008
thanks, I'll be checking your suggestions :)

AndrewJ
  • Guests
  • Last active:
  • Joined: --
I've tried to follow the various script iterations and make the appropriate changes in order to get it to work with uTorrent, but have failed. :(

Can someone give me a small working piece of code to right-click uTorrent's tray icon so that I can then select a particular context menu item?

Thanks a lot in advance! :)

SKAN
  • Administrators
  • 9115 posts
  • Last active:
  • Joined: 26 Dec 2005

a small working piece of code to right-click uTorrent's tray icon


DetectHiddenWindows, On
Process, Exist, utorrent.exe
WinGet, W, List, ahk_pid %ErrorLevel%
Loop %W%
 {
 WinGetClass, Class, % "ahk_id" W%A_Index%
 If InStr( Class, "µTorrent" ) {
   hWnd := W%A_Index%
   Break
 }}
PostMessage, 0x8001, 0,0x204,, ahk_id %hWnd% ; Right Click down
PostMessage, 0x8001, 0,0x205,, ahk_id %hWnd% ; Right Click Up


AndrewJ
  • Guests
  • Last active:
  • Joined: --
Thanks a ton SKAN, that worked perfectly! :D

RandomHeart
  • Members
  • 22 posts
  • Last active: Mar 17 2012 08:11 AM
  • Joined: 24 Mar 2009
I am trying to get this to work with another application. Can you tell me please how you find out which message number to use with PostMessage. And also here^

"If InStr( Class, "µTorrent" ) {"

the class of which window is µTorrent?

RandomHeart
  • Members
  • 22 posts
  • Last active: Mar 17 2012 08:11 AM
  • Joined: 24 Mar 2009
dealed with the windows class, and also managed to retrieve tray context menus of some applications. But with one program it stubbornly refuses to work, although I seem to have found the right message number with winspector. Can someone help me please?

SKAN
  • Administrators
  • 9115 posts
  • Last active:
  • Joined: 26 Dec 2005

with one program it stubbornly refuses to work, although I seem to have found the right message number with winspector.


What program?

pajenn
  • Members
  • 391 posts
  • Last active: Feb 06 2015 07:57 AM
  • Joined: 07 Feb 2009
I prefer this function from the previous page for USB flash drive ejecting:

SafelyRemoveHardware() 
{ TrayInfo := TrayIcons()
  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},ahk_id %hWnd%
}

It's now in my %\AutoHotkey\Lib directory.

However, I dropped the last line and preceding {ENTER}. Now it brings up the eject menu rather than ejecting the first item. For me that's better because the first 'ejetable' is often my network connection:

Posted Image

Furthermore, sometimes I get one of these:
Posted Image

When that happens I'd like to automatically ControlClick the OK Button1, and Run, "C:\Program Files\Unlocker\Unlocker.exe" "I:\", to find the open program:
Posted Image

But the problem is I haven't figured out how to get AHK to detect which drive (e.g. drive I: in above example), I chose to eject from the tray menu in the first step, so I'm not able to automatically launch the Run Unlocker command.

Hardware: fast laptop with SSD
Software: Win 7 Home Premium 64-bit, android for phone and tablet


SKAN
  • Administrators
  • 9115 posts
  • Last active:
  • Joined: 26 Dec 2005

However, I dropped the last line and preceding {ENTER}. Now it brings up the eject menu rather than ejecting the first item. For me that's better because the first 'ejetable' is often my network connection:

Posted Image


The following is my current version.
Eg. To safely remove J:, the hotkey will be Shift+Control+J

;#NoTrayIcon
#SingleInstance, Force
SetBatchLines -1
DetectHiddenWindows, On
CoordMode, Mouse, Screen
Loop 24 ; Assign Hotkeys for Drive Letters C thru Z
  HotKey % "+^" Chr(A_Index+66), EjectDrive 
Return

EjectDrive:
 Critical
 Drv := SubStr( A_ThisHotkey, 0 ) ":", nStr := ""
 DriveGet, DriveType, Type, %Drv%
 IfNotEqual, DriveType, Removable, Return
 hWnd := WinExist( "ahk_class SystemTray_Main" )
 MouseGetPos, X, Y
 MouseMove, A_ScreenWidth, A_ScreenHeight, 0
 PostMessage, 1226, 1226, 0x201,, ahk_id %hWnd% ; Left Click down
 PostMessage, 1226, 1226, 0x202,, ahk_id %hWnd% ; Left Click Up
 WinWaitActive, ahk_id %hWnd%,,5                ; Wait for SRH Tray left-click-Menu
 ; MN_GETHMENU : Code for retrieving popup menu text adapted from Sean's following post
 ;   Get Info from Context Menu: www.autohotkey.com/forum/viewtopic.php?p=137692#137692
 SendMessage, 0x1E1, 0,0,, ahk_class #32768
 hMenu := ErrorLevel
 Loop, % DllCall( "GetMenuItemCount", UInt,hMenu ) {
 idx := A_Index-1,  idn := DllCall( "GetMenuItemID", UInt,hMenu, Int,idx )
 nSize := DllCall( "GetMenuString", UInt,hMenu, Int,idx, Int,0, Int,0, UInt,0x400 ) + 1
 VarSetCapacity( mStr,nSize )
 DllCall( "GetMenuString", UInt,hMenu, Int,idx, Str,mStr, Int,nSize, UInt,0x400 )
 If InStr( mStr,Drv ) {
    ControlSend,,{Down %A_Index%}{Enter},ahk_id %hWnd%
    Break
   }
 } MouseMove, X, Y, 0
Return

But the problem is I haven't figured out how to get AHK to detect which drive (e.g. drive I: in above example), I chose to eject from the tray menu in the first step, so I'm not able to automatically launch the Run Unlocker command.


If you use my above code, the drive letter is stored in variable DRV. I guess it will be easy for you to automate unlocker with it.
kWo4Lk1.png