From be27d729e4583e4aeb3e9d72280bd7924d881d7d Mon Sep 17 00:00:00 2001 From: Wei Ouyang Date: Mon, 8 Apr 2024 16:53:29 -0700 Subject: [PATCH] Do not popup if returnError --- src/main/java/ij/IJ.java | 4 ++-- src/main/java/ij/gui/Plot.java | 2 +- src/main/java/ij/plugin/Macro_Runner.java | 11 +++++++++-- 3 files changed, 12 insertions(+), 5 deletions(-) diff --git a/src/main/java/ij/IJ.java b/src/main/java/ij/IJ.java index a73a05334..62dd45055 100644 --- a/src/main/java/ij/IJ.java +++ b/src/main/java/ij/IJ.java @@ -143,7 +143,7 @@ public static void setDebugMode(boolean b) { /** Runs the macro contained in the string macro on the current thread. Returns any string value returned by the macro, null if the macro does not return a value, or - "[aborted]" if the macro was aborted due to an error. The + "[aborted]..." if the macro was aborted due to an error. The equivalent macro function is eval(). */ public static String runMacro(String macro) { return runMacro(macro, ""); @@ -153,7 +153,7 @@ public static String runMacro(String macro) { on the current thread. The optional string argument can be retrieved in the called macro using the getArgument() macro function. Returns any string value returned by the macro, null - if the macro does not return a value, or "[aborted]" if the + if the macro does not return a value, or "[aborted]..." if the macro was aborted due to an error. */ public static String runMacro(final String macro, final String arg) { if(EventQueue.isDispatchThread()){ diff --git a/src/main/java/ij/gui/Plot.java b/src/main/java/ij/gui/Plot.java index 85245dee7..e79e41a7f 100644 --- a/src/main/java/ij/gui/Plot.java +++ b/src/main/java/ij/gui/Plot.java @@ -3321,7 +3321,7 @@ void drawShape(PlotObject plotObject, int x, int y, int size, int pointIndex) { sb.append(plotObject.macroCode); if (!drawingLegend ||!sb.toString().contains("d2s") ) {// a graphical symbol won't contain "d2s" .. String rtn = IJ.runMacro(sb.toString());//.. so it can go to the legend - if ("[aborted]".equals(rtn)) + if (rtn.startsWith("[aborted]")) plotObject.macroCode = null; } WindowManager.setTempCurrentImage(null); diff --git a/src/main/java/ij/plugin/Macro_Runner.java b/src/main/java/ij/plugin/Macro_Runner.java index 80425636f..014427b44 100644 --- a/src/main/java/ij/plugin/Macro_Runner.java +++ b/src/main/java/ij/plugin/Macro_Runner.java @@ -173,7 +173,7 @@ else if (name.endsWith(".py")) /** Runs the specified macro on the current thread. Macros can retrieve the optional string argument by calling the getArgument() macro function. Returns the string value returned by the macro, null if the macro does not - return a value, or "[aborted]" if the macro was aborted due to an error. */ + return a value, or "[aborted]..." if the macro was aborted due to an error. */ public String runMacro(String macro, String arg) { Interpreter interp = new Interpreter(); try { @@ -184,6 +184,13 @@ public String runMacro(String macro, String arg) { IJ.showProgress(1.0); ImagePlus imp = WindowManager.getCurrentImage(); if (imp!=null) imp.unlock(); + if (returnError){ + CharArrayWriter caw = new CharArrayWriter(); + PrintWriter pw = new PrintWriter(caw); + e.printStackTrace(pw); + String s = caw.toString(); + return "[aborted] Error:" + s; + } String msg = e.getMessage(); if (e instanceof RuntimeException && msg!=null && e.getMessage().equals(Macro.MACRO_CANCELED)) return "[aborted]"; @@ -194,7 +201,7 @@ public String runMacro(String macro, String arg) { /** Runs the specified macro from a JAR file in the plugins folder, passing it the specified argument. Returns the String value returned - by the macro, null if the macro does not return a value, or "[aborted]" + by the macro, null if the macro does not return a value, or "[aborted]..." if the macro was aborted due to an error. The macro can reside anywhere in the plugins folder, in or out of a JAR file, so name conflicts are possible. To avoid name conflicts, it is a good idea to incorporate the plugin