Skip to content

Commit

Permalink
Improve macro_runner to return error
Browse files Browse the repository at this point in the history
  • Loading branch information
oeway committed Apr 8, 2024
1 parent 29749be commit 881d906
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 7 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/build-imagej.yml
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ jobs:
if: github.ref == 'refs/heads/master'
with:
token: ${{ secrets.GITHUB_TOKEN }}
tag: 1.53m
tag: 1.53m1
name: ImageJ.JS
files: imagej-js-dist/
allow_override: true
39 changes: 33 additions & 6 deletions src/main/java/ij/plugin/Macro_Runner.java
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,17 @@
macros and scripts opened using the Plugins/Macros/Run command. */
public class Macro_Runner implements PlugIn {
private static String filePath;
private static boolean returnError = false;

public Macro_Runner() {
this();
}

public Macro_Runner(boolean returnError) {
this.returnError = returnError;
this();
}

/** Opens and runs the specified macro file (.txt or .ijm) or script file (.js, .bsh or .py)
on the current thread. Displays a file open dialog if <code>name</code>
is an empty string. The macro or script is assumed to be in the ImageJ
Expand Down Expand Up @@ -123,8 +133,13 @@ public String runMacroFile(String name, String arg) {
}
if (IJ.debugMode) IJ.log("runMacro: "+path+" ("+name+")");
if (!exists || f==null) {
IJ.error("RunMacro", "Macro or script not found:\n \n"+path);
return null;
if (returnError){
return "Macro or script not found:\n \n"+path;
}
else{
IJ.error("RunMacro", "Macro or script not found:\n \n"+path);
return null;
}
}
filePath = path;
try {
Expand All @@ -146,8 +161,14 @@ else if (name.endsWith(".py"))
return runMacro(macro, arg);
}
catch (Exception e) {
if (!Macro.MACRO_CANCELED.equals(e.getMessage()))
IJ.error(e.getMessage());
if (!Macro.MACRO_CANCELED.equals(e.getMessage())){
if (returnError){
return e.getMessage();
}
else{
IJ.error(e.getMessage());
}
}
return null;
}
}
Expand Down Expand Up @@ -187,7 +208,10 @@ public static String runMacroFromJar(String name, String arg) {
ClassLoader pcl = IJ.getClassLoader();
InputStream is = pcl.getResourceAsStream(name);
if (is==null) {
IJ.error("Macro Runner", "Unable to load \""+name+"\" from jar file");
if (returnError)
return "Unable to load \""+name+"\" from jar file";
else
IJ.error("Macro Runner", "Unable to load \""+name+"\" from jar file");
return null;
}
InputStreamReader isr = new InputStreamReader(is);
Expand All @@ -199,7 +223,10 @@ public static String runMacroFromJar(String name, String arg) {
macro = sb.toString();
is.close();
} catch (IOException e) {
IJ.error("Macro Runner", ""+e);
if (returnError)
return ""+e;
else
IJ.error("Macro Runner", ""+e);
}
if (macro!=null)
return (new Macro_Runner()).runMacro(macro, arg);
Expand Down

0 comments on commit 881d906

Please sign in to comment.