How to Create a Shortcut on the Canvas using JScript

Shortcuts on the Canvas are a quick way to start your favorite programs. In this post I’ll show you how to use Jscripts in order to create a shortcut on the Canvas. I’ll also show you how you can find out if the shortcut is already on the canvas in case you want to add it only if it is not already on the canvas.

Every now and then I enter the administration pages of this WordPress blog to see what people are searching for. There are a few searches realted to creating shortcuts both for Lawson Smart Client and Lawson Smart Office. Now I’m not sure what the person was actually looking for, but don’t hesitate to ask a question on this post if you still have questions or if this wasn’t what you were looking for.

The following Jscript will add a button and a button handler. When the button is clicked a Shortcut will be added to the user’s canvas, in the default location.

import System;
import System.Windows;
import System.Windows.Controls;
import Mango.UI.Services;
import Mango.Widgets;
import MForms;

package MForms.JScript {
   class ButtonExample {

      var content, debug, button;
      var controller;

      public function Init(element: Object, args: Object, controller : Object, debug : Object) {
         this.controller = controller;
         this.debug = debug;
         content = controller.RenderEngine.Content;

         var button : Button = new Button();
         button.Content = "Add shortcut";
         button.Name = "addShortcutButton";
         Grid.SetColumnSpan(button, 10);
         Grid.SetRowSpan(button, 3);
         Grid.SetColumn(button,50);
         Grid.SetRow(button,1);
         content.Children.Add(button);

         this.button = button;
         button.add_Click(OnClick);
         controller.add_Requested(OnRequested);
      }

      public function OnClick(sender: Object, e: RoutedEventArgs) {
      try{
           MessageBox.Show("Button 1 script");
           WidgetServices.Current.CreateCanvasShortcut("internal://log","LSO Log");
         }catch(ex){
            MessageBox.Show(ex.Message);
         }
      }

      public function OnRequested(sender: Object, e: RequestEventArgs) {
         button.remove_Click(OnClick);
         controller.remove_Requested(OnRequested);
      }
   }
}

There are a few other methods that you can use in the WidgetServices in order to create a shortcut but the one above is the easiest one to use.

Now if you want to check if the widget is already on the Canvas you can use this method:

public function IsOnCanvas(urlString: String){
  var widgets = WidgetServices.Current.GetWidgetInstancesByScheme(WidgetServices.ShortcutWidgetDefinitionScheme);
  for (var i = 0; i < widgets.Count; i++) {
    var widget : IWidget = widgets[i];
    var shotcutWidget : ShortcutNote = widget;
    if (shotcutWidget.Shortcut.Link == urlString){
       return true;
    }
  }
 return false;
}

The full example looks like this:

import System;
import System.Windows;
import System.Windows.Controls;
import Mango.UI.Services;
import Mango.Widgets;
import MForms;

package MForms.JScript {
   class ButtonExample {

      var content, debug, button;
      var controller;

      public function Init(element: Object, args: Object, controller : Object, debug : Object) {
         this.controller = controller;
         this.debug = debug;
         content = controller.RenderEngine.Content;

         var button : Button = new Button();
         button.Content = "Add shortcut";
         button.Name = "addShortcutButton";
         Grid.SetColumnSpan(button, 10);
         Grid.SetRowSpan(button, 3);
         Grid.SetColumn(button,50);
         Grid.SetRow(button,1);
         content.Children.Add(button);

         this.button = button;
         button.add_Click(OnClick);
         controller.add_Requested(OnRequested);
      }

      public function OnClick(sender: Object, e: RoutedEventArgs) {
      try{
           MessageBox.Show("Button 1 script");
           var urlString="internal://log";
           if(!IsOnCanvas(urlString)){
              WidgetServices.Current.CreateCanvasShortcut(urlString,"LSO Log");
           }else{
              MessageBox.Show("Shortcut with URl "+urlString+ " is already on Canvas");
           }
         }catch(ex){
            MessageBox.Show(ex.Message);
         }
      }

      public function IsOnCanvas(urlString: String){
         var widgets = WidgetServices.Current.GetWidgetInstancesByScheme(WidgetServices.ShortcutWidgetDefinitionScheme);
          for (var i = 0; i < widgets.Count; i++) {
           var widget : IWidget = widgets[i];
           var shotcutWidget : ShortcutNote = widget;
           if (shotcutWidget.Shortcut.Link == urlString){
             return true;
           }
         }
       return false;
      }

      public function OnRequested(sender: Object, e: RequestEventArgs) {
         button.remove_Click(OnClick);
         controller.remove_Requested(OnRequested);
      }
   }
}

5 thoughts on “How to Create a Shortcut on the Canvas using JScript

  1. Charles

    Is it possible to call an external program (eg. C:/tmp/CmdLineParamApp.exe) through Mform Jscript, with some parameters P1 and P2?
    P1 and P2 could be the values get from M3 program screen.
    Appreciate if anyone can advise.

    Reply
    1. karinpb Post author

      Hi,
      You can call a program. That is standard .Net. You launch anything with: System.Diagnostics.Process.Start(new System.Diagnostics.ProcessStartInfo(“C:/tmp/CmdLineParamApp.exe”)).
      Read more on Stackoverflow.

      Reply
  2. Sam N

    Hi Karin,

    How do you launch a short cut from JScript. Your script shows us how to create a shortcut. But, how can we launch the same shortcut or another shortcut using JScript?

    Thank you.
    Sam .

    Reply
  3. Sam N

    To be specific the links that we add to the shortcuts in the panels. How do we access these links using JScript?

    Reply
    1. karinpb Post author

      Hi Sam,
      I’m not sure if you mean the shortcuts in the toolbox area or on the panel if you have a personalization?

      For links to web pages or other Smart Office links you use the DashboadTaskService.Current.LaunchTask(“mforms://001”); But I don’t know how to the the link from the panel without checking the code and I’m on vacation so I don’t have access to it.

      If you with shortcuts mean those that execute a related option you have to use the IInstanceController but I need to check with norpe for details.

      Reply

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s