Tapatalk

Script to run Artistic Style as user tool on all open files

Script to run Artistic Style as user tool on all open files

3
NewbieNewbie
3

    Apr 13, 2012#1

    Hi all!

    Here there is my macro.

    Code: Select all

    InsertMode
    ColumnModeOff
    HexOff
    TrimTrailingSpaces
    UltraEditReOn
    SelectAll
    Copy
    NewFile
    Paste
    SaveAs "C:\Temp\AStyle.tmp"
    CloseFile
    RunTool "FormattoreAstyleCMD"
    Open "C:\Temp\AStyle.tmp"
    SelectAll
    Copy
    CloseFile
    Paste
    This macro is used on an active opened file for calling the C-beautifier. Now I would like to extend it for beautifying ALL OPENED files in Ultraedit.
    Could someone help me?

    Thanks
    Riccardo

    6,685587
    Grand MasterGrand Master
    6,685587

      Apr 13, 2012#2

      A quickly written script for your requirement putting all file contents together into a single file, run the tool and overwrite the file contents with the reformatted text from Artistic Style. I have tested the script with UE v18.00.0.1034 without running the tool. Process of script can be watched in output window if currently visible.

      Please note that the script may fail if C:\Temp\AStyle.tmp is already open on script start. You can add extra code to handle this situation or simply have never opened this file on script start.

      Code: Select all

      var nFileCount = UltraEdit.document.length;
      if (nFileCount > 0) {
         UltraEdit.insertMode();
         if (typeof(UltraEdit.columnModeOff) == "function") UltraEdit.columnModeOff();
         else if (typeof(UltraEdit.activeDocument.columnModeOff) == "function") UltraEdit.activeDocument.columnModeOff();
         UltraEdit.newFile();
         UltraEdit.selectClipboard(9);  // Using a user clipboard is much faster
         UltraEdit.clearClipboard();    // than using a Javascript string for some
         var sContents = "";            // unknown reason on collecting the file
         var nFileNum = 0;              // contents and writing them to new file.
         do {
            if (UltraEdit.document[nFileNum].hexMode == true) continue;
            UltraEdit.outputWindow.write("Getting contents of file " + UltraEdit.document[nFileNum].path);
            UltraEdit.document[nFileNum].selectAll();
            // sContents += "\r\n//#!# File " + nFileNum + "\r\n" + UltraEdit.document[nFileNum].selection;
            UltraEdit.clipboardContent += "\r\n//#!# File " + nFileNum + "\r\n";
            UltraEdit.document[nFileNum].copyAppend();
         }
         while(++nFileNum < nFileCount);
         UltraEdit.outputWindow.write("Storing all file contents in C:\\Temp\\AStyle.tmp");
         // UltraEdit.activeDocument.write(sContents);
         UltraEdit.activeDocument.paste();
         UltraEdit.clearClipboard();
         UltraEdit.selectClipboard(0);
         UltraEdit.saveAs("C:\\Temp\\AStyle.tmp");
         UltraEdit.closeFile(UltraEdit.activeDocument.path,2);
         UltraEdit.outputWindow.write("Run user tool FormattoreAstyleCMD");
         UltraEdit.runTool("FormattoreAstyleCMD");
         UltraEdit.outputWindow.write("Reopen file C:\\Temp\\AStyle.tmp");
         UltraEdit.open("C:\\Temp\\AStyle.tmp");
         UltraEdit.activeDocument.selectAll();
         if (UltraEdit.activeDocument.isSel()) {
            UltraEdit.outputWindow.write("Splitting up reformatted file contents");
            var asContents = UltraEdit.activeDocument.selection.split("\r\n//#!# File ");
            asContents.shift();  // First string is always an empty string.
            for (var nIndex = 0; nIndex < asContents.length; nIndex++) {
               nFileNum = parseInt(asContents[nIndex],10);
               UltraEdit.outputWindow.write("Updating file contents of " + UltraEdit.document[nFileNum].path);
               var nStart = asContents[nIndex].indexOf('\n');
               sContents = asContents[nIndex].substr(++nStart);
               UltraEdit.document[nFileNum].write(sContents);
               UltraEdit.document[nFileNum].top();
            }
            UltraEdit.closeFile(UltraEdit.activeDocument.path,2);
         }
      }

      3
      NewbieNewbie
      3

        Apr 13, 2012#3

        Thanks a lot.
        Is it possible to call from a js script the macro? The macro is called "Astyle", but the following script doesn't work.

        Code: Select all

        // Get the num of open documents.
        var num_of_docs = UltraEdit.document.length;
        
        // Enumerate through all open documents and add the header.
        var index;
        
        for (index = 0; index < num_of_docs; index++) {
          UltraEdit.document[index].setActive()
          PlayMacro 1 "Astyle"
        } //end for loop 

        6,685587
        Grand MasterGrand Master
        6,685587

          Apr 13, 2012#4

          No, it is not possible to run from within a script a macro. And why should it be possible taking into account that all commands available for macros are available also for scripts as scripting commands.

          I have coded a completely different solution because I'm quite sure that this solution is much faster than running Artistic Style once per file.

          There is the topic How do you run a Macro on open files? This quite simple solution usually works, but not for your task because of creating new files and opening files during macro execution resulting in making all the time other files active. Of course it would be possible to code the macro to take switching of active file into account. But the solution will be definitely slower than my script while the result should be the same.

          3
          NewbieNewbie
          3

            Apr 17, 2012#5

            Thanks, your script works nicely!

            Riccardo