Create a balloontip using powershell

BernardJBernardJ Member Posts: 57
edited 2012-09-26 in NAV Tips & Tricks
Here is an example of using a balloontip to notify the user of something, not important enough to send the user an e-mail, and to avoid an annoying messagebox which have to be clicked away.

In fact, it's only useful in combination with a timer running on every client, to track some changes in certain tables, but i'm not sure if that is a good idea on let's say 30 clients at the same time...
does anybody have experience with this?

OBJECT Codeunit 50000 balloontip
{
  OBJECT-PROPERTIES
  {
    Date=28-01-12;
    Time=14:46:02;
    Modified=Yes;
    Version List=;
  }
  PROPERTIES
  {
    OnRun=BEGIN
            ShowBalloontip('Notifyicon can show text up to 256 characters', 'Title goes here', 1);
          END;

  }
  CODE
  {

    PROCEDURE ShowBalloontip@1000000003(iTxtMessage@1000000003 : Text[256];iTxtTitle@1000000006 : Text[60];iOptTooltipIcon@1000000005 : 'None,Info,Warning,Error');
    VAR
      lFile@1000000000 : File;
      lAutWshShell@1000000001 : Automation "{F935DC20-1CF0-11D0-ADB9-00C04FD58A0B} 1.0:{72C24DD5-D70A-438B-8A42-98424B88AFB8}:'Windows Script Host Object Model'.WshShell";
      lIntWinState@1000000002 : Integer;
    BEGIN
      lFile.TEXTMODE := TRUE;
      lFile.CREATE('c:\temp\message.ps1');
      lFile.WRITE('[system.reflection.assembly]::loadwithpartialname("System.Windows.Forms")');
      lFile.WRITE('[system.reflection.assembly]::loadwithpartialname("System.Drawing")');
      lFile.WRITE('$icon=[system.drawing.icon]::ExtractAssociatedIcon(("'+APPLICATIONPATH+'fin.exe"))');
      //Use Powershell-icon instead of NAV's:
      //lFile.WRITE('$icon=[system.drawing.icon]::ExtractAssociatedIcon((join-path $pshome powershell.exe))');
      lFile.WRITE('$notify=new-object system.windows.forms.notifyicon');
      lFile.WRITE('$notify.icon=$icon');
      lFile.WRITE('$notify.visible=$true');
      lFile.WRITE('$notify.balloontiptext="'+iTxtMessage+'"');
      lFile.WRITE('$notify.balloontiptitle="'+iTxtTitle+'"');
      lFile.WRITE('$notify.balloontipicon=[system.windows.forms.tooltipicon]::'+FORMAT(iOptTooltipIcon));
      lFile.WRITE('$notify.showballoontip(10000)');
      // get rid of the following two lines if you don't want the tray-icon to disappear after 10 seconds
      // the icon will disappear however as soon as you mouseover it.
      lFile.WRITE('sleep(10)');
      lFile.WRITE('$notify.dispose()');
      lFile.CLOSE;
      CREATE(lAutWshShell);
      lIntWinState:=0; // hide window
      lAutWshShell.Run('powershell.exe -executionPolicy Remotesigned c:\temp\message.ps1',lIntWinState);
      // you'll need to create a timer if you want to delete the message file
      // 1 sec of sleep is not sufficient to give powershell enough time
      //SLEEP(2000);
      //ERASE('c:\temp\message.ps1');
    END;

    BEGIN
    END.
  }
}

Comments

  • fverkelfverkel Member Posts: 66
    Nice, thanks.

    We might use it for notifying the user, when he sends multiple e-mails from Navision.
    Sending price lists, reminders etc. to customers. Then after each e-mail a balloontip could show, showing which customer has been e-mailed.
    Just like when you get a balloontip when you send a print to the printer queue.

    Tip: instead of
    lFile.CREATE('c:\temp\message.ps1');
    you could use
    lFile.CREATE(DELCHR(ENVIRON('TEMP'),'>','\') + '\' + 'NAV_balloontip.ps1');

    And instead of deleting the file in function ShowBalloontip you could delete it in CodeUnit 1 in funtion LoginEnd and/or LoginStart.
    That's not quite ideal, but works fine for me.
    Remember that users can have two Navision-clients open at the same time.

    FileRec: Datatype Record, Subtype File.
    FileRec.RESET;
    // First filter and search on a different oath, because otherwise Navision would read from the cache.
    FileRec.SETRANGE(Path, 'C:\');
    IF FileRec.FIND('-') THEN ;
    FileRec.SETRANGE(Path, ENVIRON('TEMP'));
    FileRec.SETRANGE("Is a file", TRUE);
    FileRec.SETFILTER(Name, 'Nav_*');
    IF FileRec.FIND('-') THEN BEGIN
      REPEAT
        IF FileRec.Date <> TODAY THEN
          IF ERASE(FileRec.Path + '\' + FileRec.Name) THEN ;
      UNTIL FileRec.NEXT = 0;
    END;
    
    Keep It Simple and Stupid (KISS), but never oversimplify.
  • SavatageSavatage Member Posts: 7,142
    Nobody uses the 'ol ToolTop property anymore? Just us I guess :lol:
    http://msdn.microsoft.com/en-us/library/dd301088.aspx
  • BernardJBernardJ Member Posts: 57
    fverkel wrote:
    We might use it for notifying the user, when he sends multiple e-mails from Navision.
    Sending price lists, reminders etc. to customers. Then after each e-mail a balloontip could show, showing which customer has been e-mailed.
    Just like when you get a balloontip when you send a print to the printer queue.
    Glad to hear you found a purpose for it, I guess I better forget that whole timer-thing... and thanks for the hints on temp files
    Savatage wrote:
    Nobody uses the 'ol ToolTop property anymore? Just us I guess :lol:
    This script isn't showing a tooltip on a control, but a code-controlled balloon showing up as a tray icon
  • amr_wafaamr_wafa Member Posts: 23
    That was a great idea! executing PowerShell scripts from NAV never came to my mind.

    Also we can enable clicking on the Balloontip to open NAV with whatever parameters we want.
    OBJECT Codeunit 50000 balloontip
    {
      OBJECT-PROPERTIES
      {
        Date=09/26/12;
        Time=11:51:27 PM;
        Modified=Yes;
        Version List=;
      }
      PROPERTIES
      {
        OnRun=BEGIN
                ShowBalloontip('Notifyicon can show text up to 256 characters', 'Title goes here', 1);
              END;
    
      }
      CODE
      {
    
        PROCEDURE ShowBalloontip@1000000003(iTxtMessage@1000000003 : Text[256];iTxtTitle@1000000006 : Text[60];iOptTooltipIcon@1000000005 : 'None,Info,Warning,Error');
        VAR
          lFile@1000000000 : File;
          lAutWshShell@1000000001 : Automation "{F935DC20-1CF0-11D0-ADB9-00C04FD58A0B} 1.0:{72C24DD5-D70A-438B-8A42-98424B88AFB8}:'Windows Script Host Object Model'.WshShell";
          lIntWinState@1000000002 : Integer;
        BEGIN
          lFile.TEXTMODE := TRUE;
          lFile.CREATE('c:\message.ps1');
          lFile.WRITE('[system.reflection.assembly]::loadwithpartialname("System.Windows.Forms")');
          lFile.WRITE('[system.reflection.assembly]::loadwithpartialname("System.Drawing")');
          lFile.WRITE('[system.reflection.assembly]::loadwithpartialname("System.Diagnostics")');
          //If already registered, remove
          lFile.WRITE('Unregister-Event -SourceIdentifier BalloonClicked_event -ea silentlycontinue');
          lFile.WRITE('$icon=[system.drawing.icon]::ExtractAssociatedIcon(("'+APPLICATIONPATH+'fin.exe"))');
          lFile.WRITE('$notify=new-object system.windows.forms.notifyicon');
          lFile.WRITE('$notify.icon=$icon');
          lFile.WRITE('$notify.visible=$true');
          lFile.WRITE('$notify.balloontiptext="'+iTxtMessage+'"');
          lFile.WRITE('$notify.balloontiptitle="'+iTxtTitle+'"');
          lFile.WRITE('$notify.balloontipicon=[system.windows.forms.tooltipicon]::'+FORMAT(iOptTooltipIcon));
          //we can use this command to open applications upon clicking the Balloontip
          lFile.WRITE('register-objectevent $notify BalloonTipClicked BalloonClicked_event `');
          lFile.WRITE('-Action {[System.Diagnostics.Process]::Start("Finsql")}');
          //Also command is there to check if the user Closed the Balloontip
    
          lFile.WRITE('$notify.showballoontip(600000)');
          lFile.WRITE('sleep(4)');
          lFile.WRITE('$notify.dispose()');
          lFile.CLOSE;
          IF ISCLEAR(lAutWshShell) THEN
            CREATE(lAutWshShell);
    
          lIntWinState:=0; // hide window
          lAutWshShell.Run('powershell.exe -sta -executionPolicy Unrestricted c:\message.ps1',lIntWinState);
        END;
    
        BEGIN
        END.
      }
    }
    
    

    @fverkel : this can be useful for not only notify the user but also to make him easily open NAV, and I think also we can open a specific form (not sure about that)

    notes that i added the -sta parameter, otherwise the BalloonTipClicked event will not work

    check this website to see what else you can do :

    http://msdn.microsoft.com/en-us/library/9szb3e6y.aspx

    Thanks man.
Sign In or Register to comment.