Basic Host Interaction

This sample illustrates basic host interaction, including:

  • Sending data to the host

  • Waiting for screens to display

  • Using the yield keyword to wait for asynchronous functions

  • Reading text from the screen

  • Displaying basic information to the user

  • Handling error basics

All macros have the following objects available by default:

  1. session - Main entry point for access to the host. Can connect, disconnect and provides access to the PresentationSpace.

    The PresentationSpace object obtained from the session represents the screen and provides many common capabilities such as getting and setting the cursor location, sending data to the host and reading from the screen.

  2. wait - Provides a simple way to wait for various host states before continuing to send more data or read from the screen.

  3. UI - Provides basic user interface capabilities. Display data to the user or prompt them for information.

// Create a new macro function
var macro = createMacro(function*(){
  'use strict';

  // All macros have the following objects available by default:
  // 1. session - Main entry point for access to the host. Can connect, disconnect and provides access to the PresentationSpace. 
  //    The PresentationSpace object obtained from the session represents the screen and provides many common capabilities such as getting and setting the
  //    cursor location, sending data to the host and reading from the screen.
  // 2. wait - Provides a simple way to wait for various host states before continuing to send more data or read from the screen.
  // 3. ui - Provides basic User Interaction capabilities. Display data to the user or prompt them for information.

  // Declare a variable for reading and displaying some screen data.
  // As a best practice all variables should be declared near the top of a function.
  var numberOfAccounts = 0;

  // Start by obtaining the PresentationSpace object, which provides many common screen operations.
  var ps = session.getPresentationSpace();

  try {
    // Can set and get the cursor location
    ps.setCursorPosition(new Position(24, 2));

    // Use the sendKeys function to send characters to the host
    ps.sendKeys('cics');

    // SendKeys is also used to send host keys such as PA and PF keys.  
    // See "Control Keys" in the documentation for all available options
    ps.sendKeys(ControlKey.ENTER);

    // Wait for the cursor to be at the correct position. 
    // The wait object provides various functions for waiting for certain states to occur
    // so that you can proceed to either send more keys or read data from the screen.
    yield wait.forCursor(new Position(24, 2));

    // You can mix characters and control keys in one sendKeys call. 
    ps.sendKeys('data' + ControlKey.TAB + ControlKey.TAB + 'more data' + ControlKey.ENTER);

    // The "yield" keyword must be used in front of all "wait" and "ui" function calls.
    // It tells the browser to pause execution of the macro until the 
    // (asynchronous) wait function returns.  Consult the documentation for which functions
    // require the yield keyword.
    yield wait.forCursor(new Position(10, 26));
    ps.sendKeys('accounts' + ControlKey.ENTER);

    // Can also wait for text to appear at certain areas on the screen
    yield wait.forText('ACCOUNTS', new Position(3, 36)) ;
    ps.sendKeys('1' + ControlKey.ENTER);

    // All wait functions will timeout if the criteria is not met within a time limit.
    // Can increase timeouts with an optional parameter in the wait functions (in milliseconds)
    // All timeouts are specified in milliseconds and the default value is 10 seconds (10000ms).
    yield wait.forCursor(new Position(1, 1), 15000);
    ps.sendKeys('A' + ControlKey.ENTER);

     // PS provides the getText function for reading text from the screen
    numberOfAccounts = ps.getText(new Position(12, 3), 5);

    // Use the ui object to display some data from the screen
    ui.message('Number of active accounts: ' + numberOfAccounts);
   
    // The try / catch allows all errors to be caught and reported in a central location
  } catch (error) {
    // Again we use the ui object to display a message that an error occurred
    yield ui.message('Error: ' + error.message);
  }
  //End Generated Macro
});

// Run the macro and return the results to the Macro Runner
// The return statement is required as the application leverages 
// this to know if the macro succeeded and when it is finished
return macro();