Working with DataCells and Attributes

This macro illustrates how to use DataCells and AttributeSet to inspect a given row/column on the screen for text and attributes. In this sample you can see:

  • How to get a collection of DataCells for a given position and length.

  • How to iterate through DataCells to build up a text string

  • How, for comparison, you can also do a similar thing using getText().

  • And finally, how to work with attributes, get a string listing, or determine whether specific ones are set at a given screen location.

var macro = createMacro(function*() {
    'use strict';

    // Obtain the PresentationSpace for interacting with the host
    var ps = session.getPresentationSpace();

    // Declare variables for later use
    var cells;
    var text;
    var attrs;

    // Set the default timeout for "wait" functions
    wait.setDefaultTimeout(10000);

    // Sample macro for working with DataCells and Attributes
    try {
        yield wait.forCursor(new Position(24, 2));
        
        // Get DataCells from the presentation space.
        // Row 19, col 3 is the prompt, 35 characters long
        // "Choose from the following commands:"
        cells = ps.getDataCells({row:19, col:3}, 35);
        text = '';
        
        // You can display text using getText
        yield ui.message("Screen text: " + ps.getText({row:19, col:3}, 35));
        
        // Or you can assemble the text from the DataCells at each position
        for(var index = 0; index < cells.length; index++) {
            text = text.concat(cells[index].getChar());
        }
        // And display the text
        yield ui.message("Cells text: " + text);
        
        // Get the attributes for the first DataCell (cell[0])
        attrs = cells[0].getAttributes();
        
        // Display whether we have any attributes on the data cell
        yield ui.message("Attribute set is empty: " + attrs.isEmpty());
        
        // Display how many attributes are set
        yield ui.message("Number of attributes: " + attrs.size());
        
        // Display which attributes are set
        yield ui.message("Attributes: " + attrs.toString());
        
        // Now display whether the high intensity attribute is set
        yield ui.message("Is high intensity: " + 
                         attrs.contains(Attribute.HIGH_INTENSITY));
        
        // Now display whether the underline attribute is set
        yield ui.message("Is underline: " + 
                         attrs.contains(Attribute.UNDERLINE));
        
        // Now display whether alphanumeric, intensified and pen-detectable attributes are set
        yield ui.message("Is alphanumeric, intensified and pen-detectable: " + 
                         attrs.containsAll([Attribute.ALPHA_NUMERIC, Attribute.HIGH_INTENSITY, Attribute.PEN_DETECTABLE]));

        // Now display whether underline, intensified and pen-detectable attributes are set
        yield ui.message("Is underline, intensified and pen-detectable: " + 
                         attrs.containsAll([Attribute.UNDERLINE, Attribute.HIGH_INTENSITY, Attribute.PEN_DETECTABLE]));
    } catch (error) {
        yield ui.message(error);
    }
    //End Generated Macro
});

// Run the macro
return macro();