Scripting Techniques
The built-in scripting engine is a powerful feature that allows you to automate your text-editing operations. As you can see from the examples posted by users in the scripting section in our user-to-user forum, there are a great variety of ways to use scripting. Below are a few techniques to keep "on-hand" that can get you started using the scripting engine.
Note: Some techniques may require knowledge of programming concepts such as variables, if/then statements, and loops.
The below script creates a new (empty) file in the editor.
The below script writes to the active document.
The below script writes to the output window.
Click here to learn more about writing to the output window.
The below script selects the active line then jumps to the top of the file and pastes it.
UltraEdit.activeDocument.copy();
UltraEdit.activeDocument.top();
UltraEdit.activeDocument.paste();
The below script selects the word next to the cursor and the line that follow (below) the word.
UltraEdit.activeDocument.key("CTRL+RIGHT ARROW");
UltraEdit.activeDocument.key("DOWN ARROW");
UltraEdit.activeDocument.key("DOWN ARROW");
UltraEdit.activeDocument.endSelect();
This script places the selected text into a variable called text.
This script places the selected text in a variable and replaces all spaces with an underscore.
Example input:
Hello world welcome to scripting
Converts to:
Hello_world_welcome_to_scripting
str = str.replace(/ /g, "_");
Note: The "replace" text is enclosed within the backslashes.
This script takes a selection of lines and parses them into an array. Notice the line terminator variable is defined as \r\n. This may need to be changed depending on the file type you are using.
var str = UltraEdit.activeDocument.selection;
var resultArr = new Array();
resultArr = str.split(lineTerminator);
After parsing the lines into an array, you can loop through the array to perform actions to each value in the array. For example, the code below will loop through the array and write the value to the output window in the following format:
Value: [number] "[Text]"
UltraEdit.outputWindow.write("Value: " + i + " \"" + resultArr[i] + "\"\r\n");
}
The following script will append (write) multiple lines into a single line.
If you have a file such as:
abc
123
def
345
It will produce:
abc123def345
var stringArray = new Array(); //create array to hold string values
var arrayLength = 0; //array length
var lineTerminator = "\r\n"; //line terminator character you may need to change this depending on the file type
//Get user selection
strings = UltraEdit.activeDocument.selection;
//split string at line terminator characters
stringArray = strings.split(lineTerminator);
arrayLength = stringArray.length
for (var x = 0; x < arrayLength; ++x) {
UltraEdit.activeDocument.write(stringArray[x]);
}
This script performs a search for the selected text.
UltraEdit.activeDocument.findReplace.regExp = false;
UltraEdit.activeDocument.findReplace.find(text);
This script will replace all occurrences of "abc" with "ABC" in a specific selection.
UltraEdit.activeDocument.findReplace.selectText=true;
UltraEdit.activeDocument.findReplace.replaceAll=false;
UltraEdit.activeDocument.findReplace.replace("abc","ABC");
This script is similar to the above script except that it defines the selection from the top of the file, down through the end of the 4th line (beginning of line 5 of the file).
UltraEdit.activeDocument.gotoLineSelect(5,1);
UltraEdit.activeDocument.findReplace.regExp=false;
UltraEdit.activeDocument.findReplace.selectText=true;
UltraEdit.activeDocument.findReplace.replaceAll=false;
UltraEdit.activeDocument.findReplace.replace("abc","ABC");
The below script uses the UltraEdit Find/Replace to replace all instances of specific find string in a file. This example replaces & with & - the HTML entity.
UltraEdit.activeDocument.findReplace.replaceAll=true;
UltraEdit.activeDocument.findReplace.matchCase=true;
UltraEdit.activeDocument.findReplace.replace("&", "&");
The below script finds the third instance of the text ("abc") in a given file.
var x = 3;
UltraEdit.activeDocument.findReplace.regExp = false;
for (i=1;i<=x;++i) {
UltraEdit.activeDocument.findReplace.find(text);
}
Note: The above script assumes there are at least 3 instances of "abc". If the file does not include at least 3, you can add additional logic to test how many instances occur in the file.
The script below will tell you the amount of files that are open in the editor. The number is written to the output window.
UltraEdit.outputWindow.write("number " + num);
The following script will position the cursor at line number 2, column 12.
var colNum = 12;
UltraEdit.activeDocument.gotoLine(lineNum, colNum);
The following script selects all text in the file, copies it, then positions the cursor back where it was prior to the select all/copy.
var colNum = UltraEdit.activeDocument.currentColumnNum;
UltraEdit.activeDocument.selectAll();
UltraEdit.activeDocument.copy();
UltraEdit.activeDocument.gotoLine(lineNum, colNum);
The script below will traverse through all open files in the editor and will add a "comment". This is similar to the header.js script available for download from our site.
var index;
for (index = 0; index < num; index++) {
UltraEdit.document[index].top();
UltraEdit.document[index].write("// Edited by IDM");
}
You can also use this to perform an action such as Trim Trailing Spaces for all open files.
The following script will check to see if HEX mode is enabled, and if so it will toggle out of HEX mode.
if (hexActive) UltraEdit.activeDocument.hexOff();
Got a script you want to publish for other users? Send us your scripts...













