Script to reformat a not well formatted XML block pasted into a new file

Script to reformat a not well formatted XML block pasted into a new file

2

    Apr 21, 2015#1

    Is there a method to change the current View as File Type from within a script like it can be done manually via View -> View as (Highlighting File Type)?

    6,687587
    Grand MasterGrand Master
    6,687587

      Apr 22, 2015#2

      Sorry, but there is no scripting or macro command in currently latest UE for Windows v22.0.0.58 to change active syntax highlighting.

      Why do you need that?

      It is customizable which syntax highlighting is applied to which file type.

      And since UE for Windows v19.00 it is also possible to select the syntax highlighting to apply directly in status bar at bottom of the main application window with two clicks, except the basic status bar is used.
      Best regards from an UC/UE/UES for Windows user from Austria

      2

        Apr 22, 2015#3

        Because I want to write a script that will take an open document (not necessarily a saved document with an extension) that contains html encoded xml, unescape the xml entities then view the file as xml followed by calling xmlConvertToCRLF. The only portion of this script I'm missing is changing the "view as" property of the current UltraEdit window. I'll just continue as normal and use my keyboard shortcut to change the view as property to xml manually after running the script in its current state.

        6,687587
        Grand MasterGrand Master
        6,687587

          Apr 22, 2015#4

          Okay, thanks for letting us know why you think it is necessary to change the syntax highlighting for your task.

          As I wrote at "XML convert to CR/LFs" disabled for read-only files, it is since UE v17.10 always possible to use the command XML Convert to CR/LFs independent on file type and syntax highlighting.

          I played 2 minutes with this command on a not syntax highlighted file and detected following.
          • Using XML Convert to CR/LFs on a file syntax highlighted with any language containing XML_LANG in first line results in correct inserting line terminators and additionally re-indenting all lines according to XML indent/unindent settings as defined within UltraEdit taking the tab stop/indent value settings into account as configured for the active file according to its file extension.
          • Using XML Convert to CR/LFs on a file not syntax highlighted at all results in correct inserting line terminators, but the lines are not re-indented and therefore no line has leading spaces or tabs. But when manually hitting key TAB, the line is indented according to tab stop and indent value settings configured for the type of file according to its file extension.
          So the problem on usage of UltraEdit.activeDocument.xmlConvertToCRLF(); on a file not syntax highlighted as XML file is that UltraEdit does not use the indent/unindent settings as defined internally for XML highlighted files.

          I will report this by email to IDM support. In my opinion the command XML convert to CR/LFs should use always the indent/unindent settings as defined internally in UltraEdit for XML highlighted files independent on file is not syntax highlighted at all or syntax highlighted with a language not containing XML_LANG. Just in case of file, on which XML convert to CR/LFs is executed (usually active), is syntax highlighted with a language containing XML_LANG, the indent/unindent strings as defined for this syntax highlighting language should be used if there are such strings defined in the wordfile at all overriding the built-in defaults.

          A workaround for this issue for your use case would be following script code:

          Code: Select all

          // Something must be selected in active file, either entire file or just a single block.
          if (UltraEdit.activeDocument.isSel())
          {
             UltraEdit.insertMode();
             if (typeof(UltraEdit.columnModeOff) == "function") UltraEdit.columnModeOff();
             else if (typeof(UltraEdit.activeDocument.columnModeOff) == "function") UltraEdit.activeDocument.columnModeOff();
          
             // Determine document index, line terminator type and encoding of active file.
             var nDocIndex = UltraEdit.activeDocumentIdx;
             var nLineTerm = UltraEdit.activeDocument.lineTerminator;
             var nEncoding = UltraEdit.activeDocument.encoding;
          
             // Copy the selection to user clipboard 9.
             UltraEdit.selectClipboard(9);
             UltraEdit.activeDocument.copy();
          
             // Create a new file and convert it to ASCII with DOS line terminators.
             // Encoding and line terminator type for new files can be configured.
             UltraEdit.newFile();
             UltraEdit.activeDocument.unicodeToASCII();
             UltraEdit.activeDocument.unixMacToDos();
          
             // Convert the file to line terminator type of source file.
             if (nLineTerm == 1) UltraEdit.activeDocument.dosToUnix();
             else if (nLineTerm == 2) UltraEdit.activeDocument.dosToMac();
          
             // Convert the file to Unicode if source file is also a Unicode file.
             // Detected are UTF-8, UTF-16 LE and UTF-16 BE, but not ASCII Escaped Unicode.
             if ((nEncoding == 65001) || (nEncoding == 1200) || (nEncoding == 1201))
             {
                UltraEdit.activeDocument.ASCIIToUnicode();
             }
          
             // Paste the copied XML block into new file and save this file with
             // a fixed name and path overwriting existing file with that name.
             // Important is the file extension XML for this file to activate XML
             // syntax highlighting required for correct re-indenting the XML block.
             UltraEdit.activeDocument.paste();
             UltraEdit.saveAs("C:\\Temp\\XmlConvertTmp.xml");
          
             // Convert the XML block to a pretty printed XML block.
             // Then cut reformatted block to user clipboard 9.
             UltraEdit.activeDocument.xmlConvertToCRLF();
             UltraEdit.activeDocument.selectAll();
             UltraEdit.activeDocument.cut();
          
             // Save empty file and close it. The command to delete
             // active file is not available as scripting command.
             UltraEdit.closeFile(UltraEdit.activeDocument.path,1);
          
             // Make source file again the active file, paste over the selection
             // the reformatted XML block, clear user clipboard 9 and switch back
             // to the system clipboard.
             UltraEdit.document[nDocIndex].setActive();
             UltraEdit.activeDocument.paste();
             UltraEdit.clearClipboard();
             UltraEdit.selectClipboard(0);
          }
          "C:\\Temp\\XmlConvertTmp.xml" should be modified to whatever you want in the script code above.
          Best regards from an UC/UE/UES for Windows user from Austria