Reading Data from the Output Window with Scripts

In this article we show how to read data printed to the output window with a script, and also how to invoke analyses that do not have a script command but do have a shortcut in the GUI.

Example

We'll use a simple example of a point source illuminating a screen where we wish to extract the max Y value from the spot diagram... something that currently isn't supported via its own scripting command.

How to extract the Max Y value highlighted in the Output window?

The basic premise of the script is to mimic the key presses in the GUI to open the Spot Diagram Analysis (which has a shortcut of Ctrl + F9), then find the location where "Max:" is printed to the output window and read the value three columns over.

Sub Main
    Dim foundIt As Boolean, findThis As String, row As Long, col As Long, maxY As Double

    findThis = "Max:"   'the text we're looking to match in the output window

    SendKeys "^{F9}", True
    'Mimics pressing CTRL + F9 which is the shortcut for the spot diagram
    SendKeys "~", True
    'Mimcs pressing the enter key on the dialog that opens when requesting the analysis
    'Note - this approach works because there is only one analysis surface and so we can just accept the default dialog settings

    Wait(5) 'give output window time to populate with data
    foundIt = FindTextString ( findThis, row, col )
    'returns the row and col number of the first match searching up from the end of the output window
    If foundIt Then
        col = col + 3   'the Max Y value is 3 columns over
        maxY = GetTextNumber ( row, col )
        Print "Max Y value is: " & maxY
    End If
End Sub

On running the script from either the script toolbar, or from the r-click menu choice of the embedded script, we can see that the correct value is pulled into the maxY variable which is printed at the end of the script:

Note that we must run the script from the toolbar shortcut or embedded script r-click context menu for this to work. If the script is ran from the script editor then an error may be generated as the Ctrl+F9 shortcut has no meaning in the script editor and so the spot diagram will not open.

Download the FRED file here: Run+and+Grab+Spot+Diagram+Data+(v22.40).frd

Still need help? Contact Us Contact Us