Scripting in Application (JavaScript)

In this article: ECMAScript 5.1 which runs in .NET environment and is allowed accessing .NET types and functions.

Namespaces

.NET types are allowed to be accessed from JavaScript, you just need to import namespace before using types and functions defined in it.
For example:
var io = importNamespace("System.IO");
var text = importNamespace("System.Text");
 
var stream = new io.StreamReader("...", text.Encoding.utf8);
......
stream.close();
The following .NET namespaces are imported by default:
  • System as sys.
  • Microsoft.Extensions.Logging as msExtLogging.
  • CarinaStudio as cs.Deprecated in ULogViewer 3.0+
  • CarinaStudio.AppSuite.Controls as csAppControls.Deprecated in ULogViewer 3.0+
CarinaStudio.* namespaces should not be imported manually starting from ULogViewer 3.0. All necessary types will be imported automatically.
⬆️ Back to top

Naming Convention

In order to follow the naming convention of JavaScript, all names of members of .NET types will be transformed into camelCase. For example:
  • HelloWorld -> helloWorld
  • IPAddress -> ipAddress
  • ACSII -> ascii
⬆️ Back to top

Global Variables

There are 3 global variables in script runtime environment no matter what purpose the script for:

app

Allows script accessing the application-level functions. The followings are members of app:
  • executeCommand(command: string, cancellationToken: CancellationToken): intULogViewer 3.0+
  • executeCommand(command: string, action: function, cancellationToken: CancellationToken): intULogViewer 3.0+
    Execute external command and get exit code of command.
    action is a call-back function to let you send data to standard input and read data from standard output/error from process of command, for example:
    app.executeCommand("my_command", function(process, cancellationToken)
    {
        var reader = process.standardOutput;
        var line = reader.readLine();
        while (line != null)
        {
            // Process the read line...
            line = reader.readLine();
        }
    });
  • findCommandPath(command: string, cancellationToken: CancellationToken): string?ULogViewer 3.0+
    Find absolute path to the given command. Will returns null if command cannot be found.
  • getFormattedString(key: string, args: object[]): string
    Get format defined in application resources and generate formatted string with given arguments.
  • getString(key: string): stringULogViewer 3.0+
  • getString(key: string, defaultString: string): string
    Get string resource with given key.
  • getStringNonNull(key: string): stringULogViewer 3.0+
  • getStringNonNull(key: string, defaultString: string): stringULogViewer 3.0+
    Get string resource with given key which makes sure that the string won't be null.
  • isDebugMode: booleanULogViewer 3.0+
    Check whether application is running in debug mode or not.
  • isMainThread: boolean
    Check whether current thread is the main thread of application or not.
  • mainThreadSynchronizationContext: SynchronizationContext
    Get System.Threading.SynchronizationContext of main thread of application.

cancellationToken

System.Threading.CancellationToken for checking whether cancellation of script running has been requested or not. There is no need to check this variable from JavaScript directly, but you can pass this variable to .NET types and functions for cancellation check.
You can use isCancellationRequested() global function directly starting from ULogViewer 3.0.

context

Allows accessing functions according to purpose of script. The actual instance of context is also decided by the purpose of script. The followings are basic members of context:
  • data: IDictionary<string, object>
    A dictionary which stores custom data. The data may be transferred across scripts if scripts belong to same set of same purpose.
  • getString(key: string): stringULogViewer 3.0+
  • getString(key: string, defaultString: string): stringULogViewer 3.0+
    Get the string with given key defined in context by calling prepareStrings(). Will fallback to string defined in application if it cannot be found in context, or fallback to defaultString if string cannot be found in application either.
  • logger: ILogger
    Allow writing log to log buffer and file of ULogViewer. The followings are members of logger:
    • logCritical(message: string)
    • logDebug(message: string)
    • logError(message: string)
    • logInformation(message: string)
    • logTrace(message: string)
    • logWarning(message: string)
    You can use log*() global functions directly starting from ULogViewer 3.0.
  • prepareStrings(preparation: function)ULogViewer 3.0+
  • prepareStrings(cultureName: string, preparation: function)ULogViewer 3.0+
    Setup string table for specific culture. The format of cultureName is languagecode2-country/regioncode2. For ex, en-US. You can also pass null to cultureName to define default string table.
    preparation is a call-back function to setup string table. For example:
    context.prepareStrings(null, function(table)
    {
        table["Message"] = "Hello";
    });
    context.prepareStrings("zh-TW", function(table)
    {
        table["Message"] = "ε“ˆε›‰";
    });
    String tables are dedicated for each context. Therefore, string tables setup through PrepareStrings() can only be accessed by its context.
  • showMultipleItemsSelectionDialog(message: string, items: IList): IListULogViewer 4.0+
  • showMultipleItemsSelectionDialog(message: string, items: IList, defaultItemIndex: int): IListULogViewer 4.0+
    Show dialog to let user select one or more items. The returned value is the list of indices of selected items.
  • showSingleItemSelectionDialog(message: string, items: IList): intULogViewer 4.0+
  • showSingleItemSelectionDialog(message: string, items: IList, defaultItemIndex: int): intULogViewer 4.0+
    Show dialog to let user select one item. The returned value is the index of selected item, or -1 if nothing is selected.
  • showMessageDialog(message: string): MessageDialogResult
  • showMessageDialog(message: string, icon: MessageDialogIcon): MessageDialogResult
  • showMessageDialog(message: string, icon: MessageDialogIcon, buttons: MessageDialogButtons): MessageDialogResult
    Shows message dialog and wait for result selected by user.
    Values defined in MessageDialogIcon:
    • error
    • information (default)
    • question
    • success
    • warning
    Values defined in MessageDialogButtons:
    • ok (default)
    • okCancel
    • yesNo
    • yesNoCancel
    Values defined in MessageDialogResult:
    • cancel
    • no
    • ok
    • yes
  • showTextInputDialog(message: string): string
  • showTextInputDialog(message: string, initText: string): string
    Shows text input dialog and wait for text input by user.
⬆️ Back to top

Global FunctionsULogViewer 3.0+

isCancellationRequested(): bool

Check whether cancellation of script running has been requested or not. It is same as checking cancellationToken.IsCancellationRequested.

logError(obj)

Write log with Error level.

logDebug(obj)

Write log with Debug level.

logInfo(obj)

Write log with Information level.

logTrace(obj)

Write log with Trace level.

logWarning(obj)

Write log with Warning level.
⬆️ Back to top