Script Example: execute menu entry of system tray icon/app

Example scripts and tips (replaces Old Scripts & Tips archive)

Moderators: Dorian (MJT support), JRL, Phil Pendlebury

Post Reply
Dick99999
Pro Scripter
Posts: 84
Joined: Thu Nov 27, 2008 10:25 am
Location: Netherlands

Script Example: execute menu entry of system tray icon/app

Post by Dick99999 » Thu Jun 27, 2013 7:28 am

System tray icons most times display a menu when clicked. This sample script executes a script-selected menu item after displaying the menu of a script-selected icon. The version below does this for LastPass, a password vault.

The user selects an application requiring a password (i.e. Skype, Remote Desktop, Share mapping). The user then hits ALT F1 (or the assigned hot key). The script then:
- activates the tray area,
- supplies a letter L (the case for the LP icon/exe) to activate the icon
- checks whether the icon is that of LP . (There may be other L-icons)
- if not the right L-icon, tries to find the LP icon
- displays the menu of LP
- checks whether the user is loged on
- executes the Fill Application menu item
and LastPAss for App fills out the user credentials for the application.

Code: Select all

// Activates a menu item of the popup menu when clicking on a system tray icon
//
//SetFocus>Skype*
//SetFocus>Notepad*
// save active window that requets a LastPass (LP) tray icon interaction
GetActiveWindow>appActiveWin,appWinX,appWinY,appWinH,appWinW
GetCursorPos>appMouseX,appMouseY
//
// some setups: use keyboard to activate systray, wait for pop menu delay,
let>sysTrayByKey=1
let>waitMenuPopup=0.2
// name of prog in the system tray to interact with, and menu item wanted
let>trayAppName=lastapp.exe
let>trayMenuSignature=Fill Application
MidStr>trayAppName,1,1,trayLetter
// we dont use regular expr, and use handles not window names
Let>WIN_REGEX=0
Let>WIN_USEHANDLE=1
//
// set focus on system tray by prefered method (chamge sysTrayByKey)
if>sysTrayByKey=0
    // focus on system tray and LastPass menu by system API
    LibFunc>User32,FindWindowA,h1,Shell_TrayWnd,
    LibFunc>User32,FindWindowExA,h2,h1,0,TrayNotifyWnd,
    LibFunc>User32,FindWindowExA,h3,h2,0,SysPager,
    LibFunc>User32,FindWindowExA,h4,h3,0,ToolbarWindow32,User Promoted Notification Area
    //activate sys tray, Change to first letter of icon to activate: lastpass
    SetFocus>h4
else>
    // focus system tray by keyboard keys win b
    Press LWinKey
    wait>0.1
    Send>b
    wait>0.1
    Release LWinKey
endif>
//
// activate wanted tray icon (Lastpass, LP) menu by letter l and an Enter key. If not LP repeat max 4 times
let>tryNum=0
**BREAKPOINT**
While>tryNum<4
   let>tryNum=tryNum+1
   let>nTimesLetter=0
   While>nTimesLetter<tryNum
      let>nTimesLetter=nTimesLetter+1
      Send>trayLetter
      wait>0.1
   EndWhile
   // a simulated click (by Enter) on LP tray icon should open menu now, kan take a while i.e. USB icon
   Press Enter
   wait>waitMenuPopup
   // find menu window of active tray icon (supposingly LP) popup menu in system tray
   // #32768 seems to be the standard name for any popup menu in the system tray
   let>popupAtom={"#"+"32768"}
   LibFunc>User32,FindWindowA,hCurPopMenu,popupAtom,
   // may be we got a tray icon with the same letter, one that did not respons
   if>hCurPopMenu<>0
      // get process handling the popup, should be appName we  want (LP)
      GetWindowProcess>hCurPopMenu,nProcID,strProcName
      Position>trayAppName,strProcName,1,appNamePos,FALSE
   else>
      // no popup, cannot be the right icon, but it is starting with the same letter
      let>appNamePos=0
   endif>
   if>appNamePos>0
      // OK found the right one (LP), end of try loop
      let>tryNum=99
   else>
      // not the right icon, another start with the same letter,
      // It is better to move that icon up in the system tray
      // first set focus OFF system tray and try next one with that letter
      // don't know a better de-focus, mow alt tab and mouse to 0,0. haave to get rid of menu
      // and mouse on icon, if not the first matched icon remains the choosen one
      Press ALT
      Press Tab
      wait>0.1
      Release Tab
      Release ALT
      MouseMove>0,0
   endif>
EndWhile
//
// OK, right menu of try icon opened, check whther logon on alreday by checking menu lengths
if>appNamePos>0
    // get menu window x,y,w,h and text within thst window
    Let>WF_TYPE=1
    GetWindowPos>hCurPopMenu,curMenuXPos,curMenuYPos
    GetWindowSize>hCurPopMenu,curMenuWidth,curMenuHeight
    GetTextInit>
    GetTextInRect>curMenuXPos,curMenuYPos,{%curMenuXPos%+%curMenuWidth%},{%curMenuYPos%+%curMenuHeight%},menuText
    Position>trayMenuSignature,menuText,1,signatPos,FALSE
    // May be an alternative for getText:assume loged on if menu height > 100 (greater num of menu choices)
    //if>curMenuHeight>100
    if>signatPos>0
       // if already loged on, select 'fill aplication' (ond menu item from bottom)
       // and move mouse back to last active app, click to choose thast window for fill in
       // this is all application (LastPass) specific
       press Up
       wait>0.1
       press Up
       wait>0.1
      // and go execute fill in password menu
       press Enter
       wait>0.2
       // now click on window that was active and lastpass will fill in
       MouseMove>appMouseX,appMouseY
       wait>0.1
       LClick
       exit>1
    else>
       // not loged in LP, execute login menu item. first from top of menu (LP specific)
       press Down
       wait>0.1
       press Enter
       exit>1
    Endif>
Else>
    Let>MSG_STAYONTOP=0
    Let>MSG_CENTERED=0
    Let>MSG_XPOS=200
    Let>MSG_YPOS=200
    MessageModal>Tray icon %trayAppName% not running? %CRLF%If there are more systray icons with the letter %trayLetter%, it's much better to move the wanted one around the fifth icon.
Endif>
//
exit>1
//
//
// gets much info about windows, insert whre needed for debugging
let>doGetIt=1
if>doGetIt=1
Let>WIN_USEHANDLE=1
GetFocusedObject>hFocusObj
Let>WIN_USEHANDLE=0
Let>GAW_TYPE=0
GetActiveWindow>curActiveWinName,curWinX,curWinY,curWinH,curWinW
Let>WIN_USEHANDLE=1
GetActiveWindow>curActiveWinHandle,curWinX,curWinY,curWinH,curWinW
Let>GAW_TYPE=1
GetActiveWindow>curActiveWinChildHandle,curWinX,curWinY,curWinH,curWinW
GetWindowParent>curActiveWinHandle,1,hParent
Let>WIN_USEHANDLE=1
Let>WIN_REGEX=0
GetWindowProcess>curActiveWinHandle,nProcID,strProcName
GetWindowList>allWindows
Let>WIN_USEHANDLE=1
FindWindowWithText>Fill Application,0,strTitle
Let>WIN_USEHANDLE=1
Let>WIN_REGEX=0
GetWindowText>curActiveWinHandle,strText
Let>WF_TYPE=1
GetListItem>curActiveWinHandle,popupAtom,1,Exit,0,0,0,nIndex,hReturn
**BREAKPOINT**
endif>


Post Reply
cron
Sign up to our newsletter for free automation tips, tricks & discounts