Edit Script Dialog

The Edit Script dialog is used in conjunction with the Script function to write, edit and test the JavaScript source code.

The Edit Script dialog is displayed when the cell containing the Script function is entered, and is used to create, edit and test the JavaScript source code. By default, the first time that the Edit Script dialog is opened, a sample script and arguments are loaded (the sample script is located at C:\ProgramData\Cognex\In-Sight\In-Sight Explorer 5.x.x\Scripts). The sample script may be used as a template for creating custom scripts.

Note: For more information on editing and writing scripts, see Script and Scripting Functions.

Edit Script Dialog Panes and Controls

There are two methods to display the Edit Script dialog:

  • Add a Script function to an empty cell.

  • Double-click an existing Script function.

The Edit Script dialog is composed of four different panes: Arguments Textbox, Code Editor, Console Output and Results.

Note: Each of the panes within the dialog can be docked to the sides of the dialog. The position of the panes will be saved when the Edit Script dialog is closed by pressing the OK button.

In addition to the panes, the Edit Script dialog contains the following menu items and controls:

  • Script: Allows access to various actions to modify the script:
    • Test Script: Performs the same action as the Test Script button.
    • Import Script: Launches an Open dialog, which will display any saved script files (denoted by a .js file extension). Importing a script will replace the current script in the Code Editor.
    • Export Script: Launches a Save dialog, which allows a script file to be saved (with a .js file extension), or to write-over a previously saved script.
    • Revert: Restores the script to its state prior to the Edit Script dialog being opened.
    • Close: Closes the Edit Script dialog and prompts to save changes before closing the dialog, if changes have been made.
  • Edit: Allows various actions to modify the source code in the Code Editor:
    • Undo: Undoes the last change made to the script in the Code Editor.
    • Redo: Redoes the last change that was undone to the script in the Code Editor.
    • Copy: Copies the selected text in the Code Editor to the clipboard.
    • Cut: Cuts the selected text in the Code Editor and copies it to the clipboard.
    • Paste: Pastes the content in the clipboard to the Code Editor.
    • Find: Launches the Find tab of the Find and Replace dialog, to locate text in the code.
    • Replace: Launches the Replace tab of the Find and Replace dialog, to locate text in the code, and then replace them with other text.
    • Go To: Launches the Go To Line dialog, which is used to jump to a specific line number in the code.
  • Test Script: Applies the current JavaScript source code to the cell containing the function, and executes the cell and any of its dependencies.
  • OK: Applies the current JavaScript source code to the cell containing the function, executes the cell and any of its dependencies, and closes the dialog.
  • Cancel: Restores the cell to its state, prior to the Edit Script dialog being opened.

Arguments Textbox

The Arguments pane displays the input arguments to the Script function, which can be edited directly in the textbox. The script arguments are saved when the script is saved.

Code Editor

The Code Editor displays the JavaScript source code, and is used to create and edit scripts. Provided within the editor is syntax highlighting, line numbers and basic text editing functionality (undo/redo, copy/cut/paste, etc.).

Note:
  • If no text is selected and the cut operation is invoked, the entire line where the cursor is located will be cut.
  • The JavaScript language supports and utilizes Unicode characters, and Unicode characters are supported in both the Code Editor and the Console Output. Therefore, most international characters can be inserted into JavaScript code. However, when a Script function returns a string to the Spreadsheet, it will only correctly represent those characters that are supported by the currently selected language (set in the Options dialog).

Shortcuts

While in the Code Editor, the following shortcuts are available:

  • Ctrl + A: Selects all of the text in the code.
  • Ctrl + C: Copy the selected text.
  • Ctrl + F: Launches the Find tab of the Find and Replace dialog, to locate instances of values in the code.
  • Ctrl + G: Launches the Go To Line dialog, which is used to jump to a specific line number in the code.
  • Ctrl + H: Launches the Replace tab of the Find and Replace dialog, to locate instances of values in the code, and then replace them with a substitute value.
  • Ctrl + I: Launches the Quick Search icon; begin typing and the cursor will move to the next found instance of the typed characters in the code.
  • Ctrl + L: Deletes the line where the cursor is located.
  • Ctrl + P: Pastes the copied text.
  • Ctrl + T: Swaps the character where the cursor is located with the character after it.
  • Ctrl + W: Selects the word beside the cursor location.
  • Ctrl + X: Cuts the selected text and copies it to the clipboard.
  • Ctrl + Z: Undoes the last action performed.

Console Output

The Console Output pane displays the logged messages sent to the console object for all of the scripts for the currently selected In-Sight vision system or Emulator.

Note:
  • To display the logged messages in the Spreadsheet View, select the Script Console Output option from the View menu.
  • To increase performance, close the Script Console Output window when the vision system is Online.

Results

The Results pane displays the last value that was returned by the script's run() function. Pressing the Test Script button will refresh the displayed value.