Scripts to greatly increase a block and replace a placeholder with words listed in another file

Scripts to greatly increase a block and replace a placeholder with words listed in another file

6
NewbieNewbie
6

    Feb 21, 2012#1

    Hi,

    does anyone have some of those scripts or maybe have idea how to make them?

    1) block of text multiplication n times:

    e.g.
    something1 something2 something3
    something4 something5 something6


    result:
    something1 something2 something3
    something4 something5 something6

    something1 something2 something3
    something4 something5 something6


    ..n times

    2) put list of strings instead of x:

    e.g.
    put next strings:
    0x0abcd
    0x0efgh

    ...

    into text:
    something1 something2 something3
    something4
    x something5
    something6 something7

    something1 something2 something3
    something4
    x something5
    something6 something7


    result:
    something1 something2 something3
    something4
    0x0abcd something5
    something6 something7

    something1 something2 something3
    something4
    0x0efgh something5
    something6 something7


    ...

    Thanks!

    6,602548
    Grand MasterGrand Master
    6,602548

      Feb 23, 2012#2

      For 1) use this:

      Code: Select all

      if (UltraEdit.document.length > 0)
      {
         if (UltraEdit.activeDocument.isSel())
         {
            // Define the environment for the script.
            UltraEdit.insertMode();
            UltraEdit.columnModeOff();
            UltraEdit.activeDocument.hexOff();
            // Use a user clipboard instead of a string variable to make
            // sure that block multiplication works also for Unicode files.
            UltraEdit.selectClipboard(9);
            UltraEdit.activeDocument.copy();
            var nCount = UltraEdit.getValue("How often to copy the selected block?",1);
            UltraEdit.activeDocument.endSelect();
            while (nCount--) UltraEdit.activeDocument.paste();
            UltraEdit.clearClipboard();
            UltraEdit.selectClipboard(0);
         }
      }
      I can't code a script for 2) because it is not defined where to get the strings to insert from (same file, script itself, other file) and where they should be inserted respectively what to replace, character x anywhere found?

      6
      NewbieNewbie
      6

        Feb 24, 2012#3

        For second script maybe it would be the best to pick up values from another file (let's call it file 2) and put them into active one.

        6,602548
        Grand MasterGrand Master
        6,602548

          Feb 26, 2012#4

          Here is a general script for your second general request.

          Code: Select all

          if (UltraEdit.document.length > 1) // At least 2 files must be open.
          {
             // Define the environment for the script.
             UltraEdit.insertMode();
             UltraEdit.columnModeOff();
             UltraEdit.activeDocument.hexOff();
          
             // The active file is the file to modify with the
             // strings stored line by line in the other opened file.
             var nListFile = (UltraEdit.activeDocument.path == UltraEdit.document[0].path) ? 1 : 0;
             // Select everything in list file with DOS terminated lines.
             UltraEdit.document[nListFile].selectAll();
             if (UltraEdit.document[nListFile].isSel())
             {
                // Get all lines in the file into an array of strings with the line termination.
                var asStrings = UltraEdit.document[nListFile].selection.split("\r\n");
                // Discard the selection in the list file by moving caret to top of file.
                UltraEdit.document[nListFile].top();
          
                // Now the strings can be inserted into active document with any method.
                // Because of the very general question a simple, case sensitive but non
                // regular expression Find in a loop searching for word "x" is used to
                // insert the strings loaded from the file by replacing every "x".
                // To do this it is necessary to select the Find engine and initialize
                // the parameters for the Find command.
                UltraEdit.ueReOn();
                UltraEdit.activeDocument.findReplace.mode=0;
                UltraEdit.activeDocument.findReplace.matchCase=true;
                UltraEdit.activeDocument.findReplace.matchWord=true;
                UltraEdit.activeDocument.findReplace.regExp=false;
                UltraEdit.activeDocument.findReplace.searchDown=true;
                if (typeof(UltraEdit.activeDocument.findReplace.searchInColumn) == "boolean")
                {
                   UltraEdit.activeDocument.findReplace.searchInColumn=false;
                }
                // Move caret in active file to top of the file.
                UltraEdit.activeDocument.top();
          
                // The following loop is run for every string in the array.
                for (var nStringIndex = 0; nStringIndex < asStrings.length; nStringIndex++ )
                {
                   // Ignore empty strings caused by empty lines in the list file.
                   if (asStrings[nStringIndex].length == 0) continue;
                   // If no more "x" found anymore, exit the loop.
                   if (!UltraEdit.activeDocument.findReplace.find("x")) break;
                   // The found string is selected and is replaced by next string from the array.
                   UltraEdit.activeDocument.write(asStrings[nStringIndex]);
                }
                UltraEdit.activeDocument.top();
             }
          }

          6
          NewbieNewbie
          6

            Feb 27, 2012#5

            Thanks Mofi. You're the king!!!