I am just learning JavaScript with UltraEdit and had a few questions. This is what I would like to accomplish with the script:
1. Have a predefined list of strings.
2. Tell JavaScript to find those strings in the document.
3. Create a "report" in the output widow (or second best, in a new file) telling which line(s) each word appears on.
Is this even possible with JavaScript in UE? After reading some of the other posts I am getting the feeling that UE is somewhat limited in what we can do...
var x
var myString = new Array()
myString[0] = /Hi/g
myString[1] = /Hello/g
for (x in myString)
{
UltraEdit.activeDocument.findReplace.find(myString[x]);
}
UltraEdit.activeDocument.top();
UltraEdit.activeDocument.selectWord();
var line = UltraEdit.activeDocument.selection;
UltraEdit.outputWindow.write(line)
How would I go about getting the line number and make it loop through the whole document?
TIA for your help!
BTW it seems like this code should work, but it doesn't:
//This script requires UltraEdit v13.10 or UEStudio v6.30 or any later.
//Get user input
var findStr;
var x = 0;
var lineNum;
var y
var myStrings = new Array()
myStrings[0] = "hi"
myStrings[1] = "hello"
//Clear the output window, make it visible and disable status information.
UltraEdit.outputWindow.showStatus=false;
UltraEdit.outputWindow.clear();
if (UltraEdit.outputWindow.visible == false) {
UltraEdit.outputWindow.showWindow(true);
}
for (y in myStrings)
{
findStr = myStrings[y];
//Make sure we start at the beginning of the file for all values
UltraEdit.activeDocument.top();
myFindFunc(findStr);
}
function myFindFunc()
{
UltraEdit.outputWindow.write("");
UltraEdit.outputWindow.write("--- Search String ---");
UltraEdit.outputWindow.write("You searched for \"" + findStr + "\"");
UltraEdit.outputWindow.write("");
UltraEdit.outputWindow.write("--- Line Numbers ---");
UltraEdit.activeDocument.findReplace.find(findStr);
//loop to end of file
while (!(UltraEdit.activeDocument.isEof())) {
if (UltraEdit.activeDocument.isFound()) {
//get the line number that findStr is found on
lineNum = UltraEdit.activeDocument.currentLineNum;
UltraEdit.outputWindow.write("Found \"" + findStr + "\" on line: " + lineNum);
//increment count
++x;
}
else
{
UltraEdit.activeDocument.bottom();
break;
}
UltraEdit.activeDocument.findReplace.find(findStr);
}
UltraEdit.outputWindow.write("");
UltraEdit.outputWindow.write("--- Total Orders ---");
UltraEdit.outputWindow.write("Total for \"" + findStr + "\" is: " + x );
UltraEdit.outputWindow.write("");
x = 0
}
There... thanks for the help! I get a "report" of the following:
--- Search String ---
You searched for "hi"
--- Line Numbers ---
Found "hi" on line: 1
Found "hi" on line: 2
--- Total Orders ---
Total for "hi" is: 2
--- Search String ---
You searched for "hello"
--- Line Numbers ---
Found "hello" on line: 9
Found "hello" on line: 11
--- Total Orders ---
Total for "hello" is: 2
Now what I would like to do is make it so when I click on "Found "hello" on line: 11" it will actually go to line 11.
UltraEdit Help wrote:Output Window command (View menu)
...
Double clicking on a line containing the filename and line number will cause UltraEdit to attempt to open the file specified at the line number. If the filename is fully qualified the filename will be located and the number after this will be used for the line number. If the filename is not fully qualified UltraEdit will attempt to determine the filename from the first word in the line that contains a period. UltraEdit will then open the specified file in the directory of the active file. If the file does not exist in the directory of the active file, UltraEdit will try and open the file from the Project Directory if it is specified.
// extract filename from path using regexp and match method.
var fileName = UltraEdit.activeDocument.path.replace(/^.*\\/,"");
UltraEdit.outputWindow.write("Found "" + findStr + "" in file "+fileName+" on line: " + lineNum);
(this will work for windows path with backslashes)
resulting in: Found "Hello" in file alignEqual-v13.10.js on line: 4
//This script requires UltraEdit v13.10 or UEStudio v6.30 or any later.
var findStr;
var x = 0;
var lineNum;
var y
var myStrings = new Array()
myStrings[0] ="hi"
myStrings[1] ="hello"
//Clear the output window, make it visible and disable status information.
UltraEdit.outputWindow.showStatus=false;
UltraEdit.outputWindow.clear();
if (UltraEdit.outputWindow.visible == false) {
UltraEdit.outputWindow.showWindow(true);
}
for (y in myStrings)
{
findStr = myStrings[y];
UltraEdit.activeDocument.top();
myFindFunc(findStr);
}
function myFindFunc()
{
UltraEdit.outputWindow.write("");
UltraEdit.outputWindow.write("--- Search String ---");
UltraEdit.outputWindow.write("You searched for \"" + findStr + "\"");
UltraEdit.outputWindow.write("");
UltraEdit.outputWindow.write("--- Line Numbers ---");
UltraEdit.activeDocument.findReplace.find(findStr);
//loop to end of file
while (!(UltraEdit.activeDocument.isEof())) {
if (UltraEdit.activeDocument.isFound()) {
//get the line number that findStr is found on
lineNum = UltraEdit.activeDocument.currentLineNum;
var fileName = UltraEdit.activeDocument.path.replace(/^.*\\/,"");
UltraEdit.outputWindow.write("Found \"" + findStr + "\" in file "+fileName+" on line: " + lineNum);
//increment count
++x;
}
else
{
UltraEdit.activeDocument.bottom();
break;
}
UltraEdit.activeDocument.findReplace.find(findStr);
}
UltraEdit.outputWindow.write("");
UltraEdit.outputWindow.write("--- Total Orders ---");
UltraEdit.outputWindow.write("Total for \"" + findStr + "\" is: " + x );
UltraEdit.outputWindow.write("");
x = 0
}
The last thing I would like to do is make the array reside in another file somewhere (probably in the same folder as the script). Each line in this file would represent one string that I would want the script to search for. So at the end of the day we could just add, remove, or change the strings in that file and not even touch the script file.
//This script requires UltraEdit v13.10 or UEStudio v6.30 or any later.
var findStr;
var x = 0;
var lineNum;
var y;
//Clear the output window, make it visible and disable status information.
UltraEdit.outputWindow.showStatus=false;
UltraEdit.outputWindow.clear();
if (UltraEdit.outputWindow.visible === false) {
UltraEdit.outputWindow.showWindow(true);
}
var workDocIx = getActiveDocumentIndex(); /* save index of current doc before opening parm file */
var myStrings = myGetParms(); /* get parms from external file - returned as array */
UltraEdit.document[workDocIx].setActive(); /* restore active document */
for (y in myStrings)
{
findStr = myStrings[y];
UltraEdit.activeDocument.top();
myFindFunc(findStr);
}
function myFindFunc(findStr)
{
UltraEdit.outputWindow.write("");
UltraEdit.outputWindow.write("--- Search String ---");
UltraEdit.outputWindow.write("You searched for \"" + findStr + "\"");
UltraEdit.outputWindow.write("");
UltraEdit.outputWindow.write("--- Line Numbers ---");
UltraEdit.activeDocument.findReplace.find(findStr);
//loop to end of file
while (!(UltraEdit.activeDocument.isEof())) {
if (UltraEdit.activeDocument.isFound()) {
//get the line number that findStr is found on
lineNum = UltraEdit.activeDocument.currentLineNum;
var fileName = UltraEdit.activeDocument.path.replace(/^.*\\/,"");
UltraEdit.outputWindow.write("Found \"" + findStr + "\" in file "+fileName+" on line: " + lineNum);
//increment count
++x;
}
else
{
UltraEdit.activeDocument.bottom();
break;
}
UltraEdit.activeDocument.findReplace.find(findStr);
}
UltraEdit.outputWindow.write("");
UltraEdit.outputWindow.write("--- Total Orders ---");
UltraEdit.outputWindow.write("Total for \"" + findStr + "\" is: " + x );
UltraEdit.outputWindow.write("");
x = 0;
}
/* Get parameters (search strings) from external file. Lines in external file starting with
# in column 1 are considered comment lines. Returns an array.
*/
function myGetParms() {
var parms = new Array();
UltraEdit.open("C:\\Program Files\\IDM Computer Solutions\\scripts\\parms.txt"); /* Path and file name of parameter file. Remember \\ in windows path instead of \ */
UltraEdit.ueReOn(); /* Let's use UE's own regexp syntax */
UltraEdit.activeDocument.findReplace.regExp = true;
// Just in case: Go to top of document
UltraEdit.activeDocument.top();
while ( UltraEdit.activeDocument.findReplace.find("%[~#]*$") ) { /* select all lines NOT starting with # */
parms.push(UltraEdit.activeDocument.selection); /* push found parameter to array */
}
UltraEdit.closeFile(UltraEdit.activeDocument.path,2); /* close parameter file */
return parms; /* return array with parameters */
}
/* Find the tab index of the active document */
function getActiveDocumentIndex() {
var tabindex = -1; /* start value */
for (i = 0; i < UltraEdit.document.length; i++)
{
if (UltraEdit.activeDocument.path==UltraEdit.document[i].path) {
tabindex = i;
break;
}
}
return tabindex;
}
Look for UltraEdit.open in myGetParms() and adjust to your own parameter file.
Each line in the parameter file is a search item. Lines starting with # are considered comments - example:
//This script requires UltraEdit v13.10 or UEStudio v6.30 or any later.
var findStr;
var x = 0;
var lineNum;
var y;
//Clear the output window, make it visible and disable status information.
UltraEdit.outputWindow.showStatus=false;
UltraEdit.outputWindow.clear();
if (UltraEdit.outputWindow.visible === false) {
UltraEdit.outputWindow.showWindow(true);
}
var workDocIx = getActiveDocumentIndex(); /* save index of current doc before opening parm file */
var myStrings = myGetParms(); /* get parms from external file - returned as array */
UltraEdit.document[workDocIx].setActive(); /* restore active document */
for (y in myStrings)
{
findStr = myStrings[y];
UltraEdit.activeDocument.top();
myFindFunc(findStr);
}
function myFindFunc(findStr)
{
UltraEdit.outputWindow.write("");
UltraEdit.outputWindow.write("--- Search String ---");
UltraEdit.outputWindow.write("You searched for \"" + findStr + "\"");
UltraEdit.outputWindow.write("");
UltraEdit.outputWindow.write("--- Line Numbers ---");
UltraEdit.activeDocument.findReplace.find(findStr);
//loop to end of file
while (!(UltraEdit.activeDocument.isEof())) {
if (UltraEdit.activeDocument.isFound()) {
//get the line number that findStr is found on
lineNum = UltraEdit.activeDocument.currentLineNum;
var fileName = UltraEdit.activeDocument.path.replace(/^.*\\/,"");
UltraEdit.outputWindow.write("Found \"" + findStr + "\" in file "+fileName+" on line: " + lineNum);
//increment count
++x;
}
else
{
UltraEdit.activeDocument.bottom();
break;
}
UltraEdit.activeDocument.findReplace.find(findStr);
}
UltraEdit.outputWindow.write("");
UltraEdit.outputWindow.write("--- Total Orders ---");
UltraEdit.outputWindow.write("Total for \"" + findStr + "\" is: " + x );
UltraEdit.outputWindow.write("");
x = 0;
}
/* Get parameters (search strings) from external file. Lines in external file starting with
# in column 1 are considered comment lines. Returns an array.
*/
function myGetParms() {
var parms = new Array();
UltraEdit.open("C:\\Documents and Settings\\Andrew Larsen.ANDREW-LAPTOP\\Desktop\\javascript_in_UE\\Strings.txt"); /* Path and file name of parameter file. Remember \\ in windows path instead of \ */
UltraEdit.ueReOn(); /* Let's use UE's own regexp syntax */
UltraEdit.activeDocument.findReplace.regExp = true;
// Just in case: Go to top of document
UltraEdit.activeDocument.top();
while ( UltraEdit.activeDocument.findReplace.find("%[~#]*$") ) { /* select all lines NOT starting with # */
parms.push(UltraEdit.activeDocument.selection); /* push found parameter to array */
}
UltraEdit.closeFile(UltraEdit.activeDocument.path,2); /* close parameter file */
return parms; /* return array with parameters */
}
/* Find the tab index of the active document */
function getActiveDocumentIndex() {
var tabindex = -1; /* start value */
for (i = 0; i < UltraEdit.document.length; i++)
{
if (UltraEdit.activeDocument.path==UltraEdit.document[i].path) {
tabindex = i;
break;
}
}
return tabindex;
}
Yes this is great, but it has one little problem. It doesn't loop through the whole file. It always takes the last string out, so if I put 3 strings in it will only find the first two. How would I fix this?
First is there any way to make it so I don't have to have that extra blank line at the end of the file in the strings file (the file that has the strings we are looking for)? Could I do it by changing the reg. exp., or I was thinking about adding some code to make it go to the end of the file and add a "enter". Is this possible?
Also, I was wondering if there was some sort of "include" or link tag to reference other javascript files?
Here is the code I have:
[code:]
//This script requires UltraEdit v13.10 or UEStudio v6.30 or any later.
var findStr;
var x = 0;
var lineNum;
var y;
//Clear the output window, make it visible and disable status information.
UltraEdit.outputWindow.showStatus=false;
UltraEdit.outputWindow.clear();
if (UltraEdit.outputWindow.visible === false) {
UltraEdit.outputWindow.showWindow(true);
}
var strFolder = "C:\\Documents and Settings\\Andrew Larsen.ANDREW-LAPTOP\\Desktop\\Javascript_In_UltraEdit\\Strings.txt"
var workDocIx = getActiveDocumentIndex(); /* save index of current doc before opening parm file */
var myStrings = myGetParms(); /* get parms from external file - returned as array */
UltraEdit.document[workDocIx].setActive(); /* restore active document */
for (y in myStrings)
{
findStr = myStrings[y];
UltraEdit.activeDocument.top();
myFindFunc(findStr);
}
function myFindFunc(findStr)
{
UltraEdit.outputWindow.write("");
UltraEdit.outputWindow.write("--- Search String ---");
UltraEdit.outputWindow.write("You searched for \"" + findStr + "\"");
UltraEdit.outputWindow.write("");
UltraEdit.outputWindow.write("--- Line Numbers ---");
UltraEdit.activeDocument.findReplace.find(findStr);
//loop to end of file
while (!(UltraEdit.activeDocument.isEof())) {
if (UltraEdit.activeDocument.isFound()) {
//get the line number that findStr is found on
lineNum = UltraEdit.activeDocument.currentLineNum;
var fileName = UltraEdit.activeDocument.path.replace(/^.*\\/,"");
UltraEdit.outputWindow.write("Found \"" + findStr + "\" in file "+fileName+" on line: " + lineNum);
//increment count
++x;
}
else
{
UltraEdit.activeDocument.bottom();
break;
}
UltraEdit.activeDocument.findReplace.find(findStr);
}
UltraEdit.outputWindow.write("");
UltraEdit.outputWindow.write("--- Total Orders ---");
UltraEdit.outputWindow.write("Total for \"" + findStr + "\" is: " + x );
UltraEdit.outputWindow.write("");
x = 0;
}
/* Get parameters (search strings) from external file. Lines in external file starting with
# in column 1 are considered comment lines. Returns an array.
*/
function myGetParms() {
var parms = new Array();
UltraEdit.open(strFolder); /* Path and file name of parameter file. Remember \\ in windows path instead of \ */
UltraEdit.ueReOn(); /* Let's use UE's own regexp syntax */
UltraEdit.activeDocument.findReplace.regExp = true;
// Just in case: Go to top of document
UltraEdit.activeDocument.top();
while ( UltraEdit.activeDocument.findReplace.find("%[~#]*$") ) { /* select all lines NOT starting with # */
parms.push(UltraEdit.activeDocument.selection); /* push found parameter to array */
}
UltraEdit.closeFile(UltraEdit.activeDocument.path,2); /* close parameter file */
return parms; /* return array with parameters */
}
/* Find the tab index of the active document */
function getActiveDocumentIndex() {
var tabindex = -1; /* start value */
for (i = 0; i < UltraEdit.document.length; i++)
{
if (UltraEdit.activeDocument.path==UltraEdit.document.path) {
tabindex = i;
break;
}
}
return tabindex;
}
[/code]
andy wrote:First is there any way to make it so I don't have to have that extra blank line at the end of the file in the strings file (the file that has the strings we are looking for)? Could I do it by changing the reg. exp., or I was thinking about adding some code to make it go to the end of the file and add a "enter". Is this possible?
This is a classic. A line should only be considered a line if it ends with line terminators (i.e. \r\n for DOS). Otherwise it is a string. (I think I have this quote from Mofi Therefore as Bego pointed out we had to add line terminators to the last "line" so the "end of line anchor" in the regex ($) still works for the last line.
But if you really dislike the last line ending with line terminators, then use this regex instead: %[~#]?+
as it doesn't rely on "end of line anchor" and ? matches everything but newline.
andy wrote:Also, I was wondering if there was some sort of "include" or link tag to reference other javascript files?