6 import ij.plugin.filter.*;
7 import ij.macro.Interpreter;
9 import java.awt.image.*;
11 import java.util.Locale;
12 import java.util.Hashtable;
14 /** The class contains static methods that perform macro operations. */
17 public static final String MACRO_CANCELED = "Macro canceled";
19 // A table of Thread as keys and String as values, so
20 // Macro options are local to each calling thread.
21 static private Hashtable table = new Hashtable();
24 public static boolean open(String path) {
25 if (path==null || path.equals("")) {
26 Opener o = new Opener();
29 Opener o = new Opener();
30 ImagePlus img = o.openImage(path);
37 public static boolean saveAs(String path) {
38 ImagePlus imp = WindowManager.getCurrentImage();
41 FileSaver fs = new FileSaver(imp);
42 if (path==null || path.equals(""))
43 return fs.saveAsTiff();
44 if (imp.getStackSize()>1)
45 return fs.saveAsTiffStack(path);
47 return fs.saveAsTiff(path);
50 public static String getName(String path) {
51 int i = path.lastIndexOf('/');
53 i = path.lastIndexOf('\\');
55 return path.substring(i+1);
60 public static String getDir(String path) {
61 int i = path.lastIndexOf('/');
63 i = path.lastIndexOf('\\');
65 return path.substring(0, i+1);
70 /** Aborts the currently running macro or any plugin using IJ.run(). */
71 public static void abort() {
73 //IJ.log("Abort: "+Thread.currentThread().getName());
74 if (Thread.currentThread().getName().endsWith("Macro$")) {
75 table.remove(Thread.currentThread());
76 throw new RuntimeException(MACRO_CANCELED);
80 /** If a command started using run(name, options) is running,
81 and the current thread is the same thread,
82 returns the options string, otherwise, returns null.
83 @see ij.gui.GenericDialog
86 public static String getOptions() {
87 //IJ.log("getOptions: "+Thread.currentThread().hashCode()); //ts
88 if (Thread.currentThread().getName().startsWith("Run$_")) {
89 Object options = table.get(Thread.currentThread());
90 return options==null?null:options+" ";
95 /** Define a set of Macro options for the current Thread. */
96 public static void setOptions(String options) {
97 //IJ.log("setOptions: "+Thread.currentThread().hashCode()+" "+options); //ts
98 if (options==null || options.equals(""))
99 table.remove(Thread.currentThread());
101 table.put(Thread.currentThread(), options);
104 /** Define a set of Macro options for a Thread. */
105 public static void setOptions(Thread thread, String options) {
107 throw new RuntimeException("Need a non-null thread instance");
109 table.remove(thread);
111 table.put(thread, options);
114 public static String getValue(String options, String key, String defaultValue) {
119 index = options.indexOf(key, ++index);
120 if (index<0) return defaultValue;
121 } while (index!=0&&options.charAt(index-1)!=' ');
122 options = options.substring(index+key.length(), options.length());
123 if (options.charAt(0)=='\'') {
124 index = options.indexOf("'",1);
128 return options.substring(1, index);
129 } else if (options.charAt(0)=='[') {
130 index = options.indexOf("]",1);
134 return options.substring(1, index);
136 //if (options.indexOf('=')==-1) {
137 // options = options.trim();
138 // IJ.log("getValue: "+key+" |"+options+"|");
139 // if (options.length()>0)
142 // return defaultValue;
144 index = options.indexOf(" ");
148 return options.substring(0, index);
152 public static String trimKey(String key) {
153 int index = key.indexOf(" ");
155 key = key.substring(0,index);
156 index = key.indexOf(":");
158 key = key.substring(0,index);
159 key = key.toLowerCase(Locale.US);
163 public static void setLocalVariable(String key,String value) {
164 Interpreter i=Interpreter.getInstance();
167 i.setLocalVariable(key,value);