Forum Discussion

Morgan's avatar
Morgan
Frequent Contributor
14 years ago

Selecting the application in the tray

I just used the following script located here to select one of our products in the tray which launches a menu item that I can then select an option.  This seems to work beautifully.  However, I notice that if the icon I need is in a different place or there are other unexpected icons in the tray (i.e. security alerts), TC cannot locate the icon it needs to initially.  If I find it for it manually the first go around, TC then has no issue locating it in subsequent tests.



Any suggestions to make this work consistently?  (I am using VBscript)



Thanks!

Morgan

4 Replies

  • Hi,



    Below is the script, that I am currently use (JScript):




    var LOCK_ICON  // global variable that will contain reference to system tray (will be initialized in "FindIconInSystemTray" function)




    function ClickIconInSystemTray(sIconTitle, bIsLeftMouseButton, bIsDoubleClick, bRShiftPressed)

    {

      if(typeof(bIsLeftMouseButton) != "boolean")

        bIsLeftMouseButton = false;

        

      if(typeof(bIsDoubleClick) != "boolean")

        bIsDoubleClick = false;





      if(typeof(bRShiftPressed) != "boolean")

        bRShiftPressed = false;

        

      if(FindIconInSystemTray(sIconTitle) == -1)

      {

        Log.Message("Can't click icon in the system tray, because of icon with '" + sIconTitle + "' title does not exist here!");

        return false;

      }





      try

      {

        if(bRShiftPressed)

        {

          Win32API.keybd_event(Win32API.VK_RSHIFT, 0, Win32API.KEYEVENTF_EXTENDEDKEY, 0);

          BuiltIn.Delay(500);

        }

        

        if(bIsLeftMouseButton)

        {

          if(bIsDoubleClick)

            ICON_TRAY.DblClickItem(sIconTitle + "*", true, skNoShift);

          else ICON_TRAY.ClickItem(sIconTitle + "*", true, skNoShift);

        }

        else

        {

          if(bIsDoubleClick)

            ICON_TRAY.DblClickItemR(sIconTitle + "*", true, skNoShift);

          else ICON_TRAY.ClickItemR(sIconTitle + "*", true, skNoShift);

        }

        

        if(bRShiftPressed)

        {

          BuiltIn.Delay(500);

          Win32API.keybd_event(Win32API.VK_RSHIFT, 0, Win32API.KEYEVENTF_EXTENDEDKEY | Win32API.KEYEVENTF_KEYUP, 0);

        }

      }

      catch(e)

      {

        Log.Message("Can't click icon in the system tray!", e.description);

        return false;

      }





      return true;

    }




    function FindIconInSystemTray(icon_title)   // return index of required icon in the system tray



      var oSysTray = null;

      var start = getTime();

      do

      {

        oSysTray = Sys.Process("Explorer").WaitWindow("Shell_TrayWnd", "*", 1, 1000).WaitWindow("TrayNotifyWnd", "*", 1, 1000);

        if(! oSysTray.Exists)

          continue;

          

        ICON_TRAY = oSysTray.WaitWindow("SysPager", "*", 1, 1000).WaitWindow("ToolbarWindow32", "*Notification Area", 1, 1000);

        if(! ICON_TRAY.Exists)

        {

          oSysTray.Refresh();

          BuiltIn.Delay(100);

          continue;

        }

        

        for(var s=0; s<ICON_TRAY.wButtonCount; s++)

        {

          if(ICON_TRAY.wButtonText(s, true).indexOf(icon_title) != -1)

            return s;

        }

        

        ICON_TRAY.Refresh();

        BuiltIn.Delay(100);

      }

      while(getTime() - start < 10000)

      

      return -1;

    }
  • Morgan's avatar
    Morgan
    Frequent Contributor
    Hi John,



    Thank you for the response!  Do you think you could translate what exactly you are doing here?  I'm using VBScript and still relatively new to it.



    Thanks!

    Morgan
  • Hello Morgan,


    The script you are using (the one from the "How do I right-click the application icon in the tray?" topic) should not depend on the icon's position. It uses the ClickItemR and PopupMenu.Click actions to click the item by its name.

    Please send us your TestComplete project along with results of its failed execution.

  • Morgan's avatar
    Morgan
    Frequent Contributor
    I was able to work around it.  The problem appeared to occur only when the 'hide inactive icons' flag on the toolbar properties was marked.  I added a section in the beginning of the script to make sure that was unchecked and then it seems to work correctly.  I'll let you know if I have any further problems with it!



    Thanks,

    Morgan