Tapatalk

Loading macro file with macro command JSONFormatDocument fails with an error message (fixed)

Loading macro file with macro command JSONFormatDocument fails with an error message (fixed)

2

PostAug 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,825625
Grand MasterGrand Master
6,825625

PostAug 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.

2

PostAug 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.

6,825625
Grand MasterGrand Master
6,825625

Post18:28 - Feb 08#4

The file changes.txt as installed with UltraEdit 2025.2.0.8 has the history item:
  • Fixed: Cannot load a macro containing JSONFormatDocument
I verified that information by downloading the ZIP file New3.zip from first post, extracting it and loading the macro file New3.mac. That worked as also playing the macro (on an empty file). I also created a new macro with the macro code posted in first post, saved the macro into a new macro file, exited UltraEdit, restarted UltraEdit, successfully loaded the macro file with just before created macro and played it also successfully on an empty file.

The issue with a macro file cannot be loaded on containing a macro with the command JSONFormatDocument is definitely fixed with UltraEdit for Windows and UEStudio 2025.2.0.

But I am always interested in reproducing an error before rating a bug as fixed. I loaded the macro file New3.mac and the macro file created with UltraEdit 2025.2.0.8 with a macro with the posted macro code also with several older versions of UltraEdit. There was always shown an error message on the attempt to load one of the two macro files up to UltraEdit/UEStudio version 2024.3.0.15 which is the last public released version 2024.3. But both macro files could be loaded successfully and the macro inside each macro file also played successfully on an empty file with UltraEdit/UEStudio 2025.0.0.24 which is the first public released version 2025.0. It looks like the issue was indeed already fixed with v2025.0.0.24 of UE/UES as the macro file loading and macro execution worked also with all newer UltraEdit versions than 2025.0.0.24 too.