Toast Notification Window

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

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

Post Reply
dtaylor
Junior Coder
Posts: 46
Joined: Mon Aug 08, 2016 2:42 am

Toast Notification Window

Post by dtaylor » Wed Aug 31, 2016 4:49 am

My first code share, so be kind. :lol:

This will pop up a notification window in the bottom right corner of the screen (like Toast). I use it to display notifications of what is going on in a script. It is especially useful when a script has a big loop processing many records. I use this to display information like "Processing record 5 of 300" and increment the current record number and update the notification.

To use:
  1. Save both scripts to your default script directory (save Main Script as Toast.scp & save Secondary Script as _Toast.scp)
    Note: I always start called function subroutines with an underscore "_".
  2. Run the Main Script (Toast.scp), which will call the secondary script via the include.
I tried to include as many comments inside the script as possible so everyone should be able to figure out what it's doing.

I hope someone finds this useful.

Thanks!
dtaylor


Main Script:
Save as script as "Toast"

Code: Select all

Include>%SCRIPT_DIR%\_Toast.scp

//show initial Toast window (no Title passed, so use Script Name)
GoSub>Toast,Toast Text (Null Title -use Script Name)
Wait>5

//update existing Toast window
GoSub>Toast,UPDATED EXISTING Toast Text,UPDATED EXISTING Toast Title
Wait>5
CloseDialog>ToastDialog

Wait>2

//show new Toast after closing other Toast windows
GoSub>Toast,ALL NEW Toast Text,ALL NEW Toast Title
Wait>5
CloseDialog>ToastDialog
Secondary Script (LIB file called via Include from Main Script above):
Save this script and name it "_Toast".

Code: Select all

/*
Custom Toast Window
Author: D Taylor
Date:   08/30/2016

--------------------------------------------------
Notes:
--------------------------------------------------
*If no Title is passed to the subroutine, the Script Name is used for the Title.

*The 1st time this subroutine is called from a script, it will pop up the window (like toast).
*Any additional calls will update the current displayed text & title in place (no toast pop-up).

*You can open and close the dialog window as needed during the script to notify the user what is happening.

I use it to display what's going on for steps or loops that take a lot of time.
I like to use it in loops by showing that processing is on record x of xx (ex: Now processing 5 of 300)


--------------------------------------------------
Calling Syntax:
--------------------------------------------------
Include>%SCRIPT_DIR%\LIB\_Toast.scp

Gosub>Toast,Toast Text[,Toast Title]
Remark>script steps...
Remark> -OR-
Remark>Wait>5
CloseDialog>ToastDialog


--------------------------------------------------
Sample:
--------------------------------------------------
Include>%SCRIPT_DIR%\LIB\_Toast.scp

//show initial Toast window (no Title passed, so use Script Name)
GoSub>Toast,Toast Text (Null Title -use Script Name)
Wait>5

//update existing Toast window
GoSub>Toast,UPDATED EXISTING Toast Text,UPDATED EXISTING Toast Title
Wait>5
CloseDialog>ToastDialog

Wait>2

//show new Toast after closing other Toast windows
GoSub>Toast,ALL NEW Toast Text,ALL NEW Toast Title
Wait>5
CloseDialog>ToastDialog
--------------------------------------------------
*/


SRT>Toast

Remark>if 1st time executed, need to configure the Dialog
If>flagToastConfigured<>TRUE

Dialog>ToastDialog
object ToastDialog: TForm
  Left = 594
  Top = 248
  HelpContext = 5000
  BorderIcons = []
  BorderStyle = bsSingle
  Caption = 'ToastTitle'
  ClientHeight = 62
  ClientWidth = 515
  Color = clBtnFace
  Font.Charset = DEFAULT_CHARSET
  Font.Color = clWindowText
  Font.Height = -14
  Font.Name = 'MS Sans Serif'
  Font.Style = []
  FormStyle = fsStayOnTop
  OldCreateOrder = False
  Position = poScreenCenter
  ShowHint = True
  OnTaskBar = False
  PixelsPerInch = 120
  TextHeight = 16
  object ToastLabel: TLabel
    Left = 10
    Top = 12
    Width = 492
    Height = 36
    Margins.Left = 4
    Margins.Top = 4
    Margins.Right = 4
    Margins.Bottom = 4
    Alignment = taCenter
    AutoSize = False
    Caption = 'Toast Body Text'
    Font.Charset = DEFAULT_CHARSET
    Font.Color = clWindowText
    Font.Height = -23
    Font.Name = 'MS Sans Serif'
    Font.Style = []
    ParentFont = False
  end
end
EndDialog>ToastDialog
EndIf

  //get screen size for use in calculating Toast position
  GetScreenRes>iScreenWidth,iScreenHeight
  
  //set toast size
  Let>iToastWidth=515
  Let>iToastHeight=62
  
  //calculate screen location where Toast is to be displayed
  Let>ToastX={%iScreenWidth%-%iToastWidth%-15}
  Let>ToastY={%iScreenHeight%-%iToastHeight%-90}
  
  //check whether title for Toast was passed - otherwise use Script Name
  If>Toast_Var_2={"Toast_Var_2"}
    Remark>title not passed, use Script Name
    Let>ToastTitle=%SCRIPT_NAME%
  Else
    Remark>title passed, use it
    Let>ToastTitle=Toast_Var_2
  EndIf

  //set Toast display dialog parameters
  SetDialogProperty>ToastDialog,,Position,poDesigned
  SetDialogProperty>ToastDialog,,ClientHeight,%iToastHeight%
  SetDialogProperty>ToastDialog,,ClientWidth,%iToastWidth%
  SetDialogProperty>ToastDialog,,Left,%ToastX%
  SetDialogProperty>ToastDialog,,Top,%ToastY%
  SetDialogProperty>ToastDialog,,Caption,%ToastTitle%

  //set Toast display text (label) parameters
  SetDialogProperty>ToastDialog,ToastLabel,Height,%iToastHeight%
  SetDialogProperty>ToastDialog,ToastLabel,Width,%iToastWidth%
  SetDialogProperty>ToastDialog,ToastLabel,Caption,%Toast_Var_1%
  
  //select starting location for Toast window
  If>flagToastConfigured=TRUE
    Remark>Toast has already popped, just display at same location
    SetDialogProperty>ToastDialog,,Top,%ToastY%
    Let>ToastPos=%ToastY%
  Else
    Remark>1st display of Toast, so place it below screen & make it pop into location
    SetDialogProperty>ToastDialog,,Top,%iScreenHeight%
    Let>ToastPos=%iScreenHeight%
  EndIf

  //set flag that Toast has popped
  Let>flagToastConfigured=TRUE
  
  //show Toast
  Show>ToastDialog
  
  //animate toast (1st execution only)
  Label>MoveToast
  If>ToastPos>%ToastY%
    Let>ToastPos=%ToastPos%-2
    MoveWindow>%ToastTitle%,%ToastX%,%ToastPos%
    Wait>.0005
    Goto>MoveToast
  EndIf
END>Toast

User avatar
Meryl
Staff
Posts: 124
Joined: Wed Sep 19, 2012 1:53 pm
Location: Texas
Contact:

Re: Toast Notification Window

Post by Meryl » Tue Sep 06, 2016 2:10 pm

Thanks for sharing, dtaylor!

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