Loading macro file with macro command JSONFormatDocument fails with an error message

Loading macro file with macro command JSONFormatDocument fails with an error message

2

    Aug 16, 2023#1

    Good day,

    I've been using UltraEdit since I first started working in code back in 2007. I remember having used macros before but it has been a long time.
    I have a set of find and replace actions that I do to clean up a file generated by a website. This seemed like a prime opportunity to use a macro. Sadly every time I save the macro file and subsequently load it, I get the generic "Macro Load" message box window with the text "Error in Macro".

    What makes this confusing is that I am able to run the macro after I record it. I'm just not able to save and load it.

    The recorded macro code is:

    Code: Select all

    InsertMode
    ColumnModeOff
    HexOff
    JSONFormatDocument
    UnixMacToDos
    UltraEditReOn
    Find RegExp "^T"
    Replace All " "
    UltraEditReOn
    Find RegExp "^( +^)\"data\": {^P +\"depth\": ^([0-9]+^)^P +}"
    Replace All "^1\"depth\": ^2"
    UltraEditReOn
    Find RegExp " ^( +^)\"data\": {^P +\"depth\": ^([0-9]+^),^P +\"compkey\": ^([0-9]+^)^P +}"
    Replace All " ^1\"depth\": ^2,^p^1 \"compkey\":^3"
    UltraEditReOn
    Find RegExp " +\"key\": \"[0-9id_.]+\",^P"
    Replace All ""
    UltraEditReOn
    Find RegExp ",^P +\""
    Replace All ", \""
    UltraEditReOn
    Find RegExp "{^P +\""
    Replace All "{\""
    UltraEditReOn
    Find RegExp "^([0-9]^)^P +}"
    Replace All "^1}"
    UltraEditReOn
    Find RegExp "\"expanded\": ^{true^}^{false^}, "
    Replace All ""
    UltraEditReOn
    Find RegExp "}^P +^]"
    Replace All "}]"
    UltraEditReOn
    Find RegExp "^]^P +}"
    Replace All "]}"
    The macro file is also attached.

    So my questions are these:
    1. Is there somewhere with more robust reporting that could tell me what is wrong?
    2. Is there anything obviously wrong with the macro I could quickly change?
    3. Why would a program make a file it then can't read?
    Thank you for your time,
    Nathan
    New3.zip (364 Bytes)   0
    zip of .mac file that won't load

    6,686585
    Grand MasterGrand Master
    6,686585

      Aug 16, 2023#2

      nroberts_SR wrote:Why would a program make a file it then can't read?
      I answer this question first. The reason is a bug in program code of UltraEdit. I could reproduce the issue with UltraEdit for Windows v2023.0.0.50.
      nroberts_SR wrote:Is there somewhere with more robust reporting that could tell me what is wrong?
      No, the issue must be investigated by an UltraEdit developer and fixed.
      nroberts_SR wrote:Is there anything obviously wrong with the macro I could quickly change?
      I looked on the macro code and first thought that the macro command JSONFormatDocument could cause the error on macro load. I am not aware of such a macro command. It is not listed on the help page Edit macro command nor could I find any information about such a macro command in entire help of UltraEdit or in changes.txt in program files folder of UltraEdit. I could not find in my personal records that anything about that macro command was ever public published by UltraEdit, Inc. in the past. But it is indeed in the Command list in Edit/Create Macro dialog window. It looks like this macro command is not yet fully implemented.

      I found in my email archive with issue reports and enhancement request to UltraEdit support an email regarding to missing macro and scripting commands in help of UltraEdit.

      The four not yet in UltraEdit help described macro commands are:
      • JSONCompressDocument
      • JSONFormatDocument
      • XMLCompressDocument
      • XMLFormatDocument
      The four document object scripting commands missing in help are:
      • jsonCompressDocument
      • jsonFormatDocument
      • xmlCompressDocument
      • xmlFormatDocument
      It looks like these four macro/scripting commands are not yet fully implemented, at least the macro command JSONFormatDocument is not fully implemented.

      Please report this issue by email to UltraEdit support for getting it fixed in a future version.

      There is perhaps a scripting solution working. The UltraEdit script code for your macro code would be:

      Code: Select all

      if (UltraEdit.document.length > 0)  // Is any file opened?
      {
         // Define environment for this script.
         UltraEdit.insertMode();
         if (typeof(UltraEdit.columnModeOff) == "function") UltraEdit.columnModeOff();
         else if (typeof(UltraEdit.activeDocument.columnModeOff) == "function") UltraEdit.activeDocument.columnModeOff();
      
         UltraEdit.activeDocument.jsonFormatDocument();
         UltraEdit.activeDocument.unixMacToDos();
      
         // Move caret to top of the active file.
         UltraEdit.activeDocument.top();
      
         UltraEdit.ueReOn();
         UltraEdit.activeDocument.findReplace.mode=0;
         UltraEdit.activeDocument.findReplace.matchCase=false;
         UltraEdit.activeDocument.findReplace.matchWord=false;
         UltraEdit.activeDocument.findReplace.regExp=true;
         UltraEdit.activeDocument.findReplace.searchDown=true;
         if (typeof(UltraEdit.activeDocument.findReplace.searchInColumn) == "boolean")
         {
            UltraEdit.activeDocument.findReplace.searchInColumn=false;
         }
         UltraEdit.activeDocument.findReplace.preserveCase=false;
         UltraEdit.activeDocument.findReplace.replaceAll=true;
         UltraEdit.activeDocument.findReplace.replaceInAllOpen=false;
      
         UltraEdit.activeDocument.findReplace.replace("^t", " ");
         UltraEdit.activeDocument.findReplace.replace("^( +^)\"data\": {^p +\"depth\": ^([0-9]+^)^p +}", "^1\"depth\": ^2");
         UltraEdit.activeDocument.findReplace.replace(" ^( +^)\"data\": {^p +\"depth\": ^([0-9]+^),^p +\"compkey\": ^([0-9]+^)^p +}", " ^1\"depth\": ^2,^p^1 \"compkey\":^3");
         UltraEdit.activeDocument.findReplace.replace(" +\"key\": \"[0-9id_.]+\",^p", "");
         UltraEdit.activeDocument.findReplace.replace(",^p +\"", ", \"");
         UltraEdit.activeDocument.findReplace.replace("{^p +\"", "{\"");
         UltraEdit.activeDocument.findReplace.replace("^([0-9]^)^p +}", "^1}");
         UltraEdit.activeDocument.findReplace.replace("\"expanded\": ^{true^}^{false^}, ", "");
         UltraEdit.activeDocument.findReplace.replace("}^p +^]", "}]");
         UltraEdit.activeDocument.findReplace.replace("^]^p +}", "]}");
      }
      
      This script can be executed on an empty file with UltraEdit for Windows v2023.0.0.50 without any error captured to the output window. The execution of UltraEdit.activeDocument.jsonFormatDocument() seems to work, at least it does not result in a script execution error.

      I suppose that ^T should be ^t which is the documented placeholder for a horizontal tab character for non-regular expression and UltraEdit regular expression finds/replaces. I suppose further that ^P should be ^p which is the documented placeholder for a carriage return + line-feed as long as not using a Unix or Perl regular expression find/replace. I have never seen before these two placeholders with an uppercase letter and I was not aware that ^T and ^P indeed work too, even on running a case-sensitive find or replace. So, I learned something new from your post.
      Best regards from an UC/UE/UES for Windows user from Austria

      2

        Aug 16, 2023#3

        That is a super thorough answer. Thank you. I don't know why it didn't occur to me that the most complicated step could cause a problem, it seems obvious in retrospect.
        Having removed that command from the macro I have since been able to close and load the macro file without an error. 
        If I do that step manually then the rest of the macro runs as expected.
        I will make sure to follow up with the devs so that no one else has to deal with this.