Skip to content


Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Fetching contributors…

Cannot retrieve contributors at this time

executable file 107 lines (94 sloc) 3.715 kb
''' - helper methods for finding and running macros'''
# CellProfiler is distributed under the GNU General Public License.
# See the accompanying file LICENSE for details.
# Copyright (c) 2003-2009 Massachusetts Institute of Technology
# Copyright (c) 2009-2012 Broad Institute
# Please see the AUTHORS file for credits.
# Website:
import sys
import bioformats
import cellprofiler.utilities.jutil as J
from cellprofiler.preferences import get_headless
def get_commands():
'''Return a list of the available command strings'''
def fn():
hashtable = J.static_call('ij/Menus', 'getCommands',
if hashtable is None:
# This is a little bogus, but works - trick IJ into initializing
hashtable = J.static_call('ij/Menus', 'getCommands',
if hashtable is None:
return []
keys =, "keys", "()Ljava/util/Enumeration;")
keys = J.jenumeration_to_string_list(keys)
values =, "values", "()Ljava/util/Collection;")
values = [J.to_string(x) for x in J.iterate_java(, 'iterator', "()Ljava/util/Iterator;"))]
class CommandList(list):
def __init__(self):
super(CommandList, self).__init__(keys)
self.values = values
return CommandList()
return J.run_in_main_thread(fn, True)
def execute_command(command, options = None):
'''Execute the named command within ImageJ'''
def fn(command=command, options=options):
if options is None:
J.static_call("ij/IJ", "run", "(Ljava/lang/String;)V", command)
J.static_call("ij/IJ", "run",
command, options)
J.run_in_main_thread(fn, True)
def execute_macro(macro_text):
'''Execute a macro in ImageJ
macro_text - the macro program to be run
def fn(macro_text = macro_text):
interp = J.make_instance("ij/macro/Interpreter","()V"), "run","(Ljava/lang/String;)V", macro_text)
J.run_in_main_thread(fn, True)
def show_imagej():
'''Show the ImageJ user interface'''
ij_obj = J.static_call("ij/IJ", "getInstance", "()Lij/ImageJ;")
if ij_obj is None:
ij_obj = J.make_instance("ij/ImageJ", "()V"), "setVisible", "(Z)V", True), "toFront", "()V")
def get_user_loader():
'''The class loader used to load user plugins'''
return J.static_call("ij/IJ", "getClassLoader", "()Ljava/lang/ClassLoader;")
def get_plugin(classname):
'''Return an instance of the named plugin'''
if classname.startswith("ij."):
cls = J.class_for_name(classname)
cls = J.class_for_name(classname, get_user_loader())
cls = J.get_class_wrapper(cls, True)
constructor = J.get_constructor_wrapper(cls.getConstructor(None))
return constructor.newInstance(None)
if __name__=="__main__":
import sys
commands = get_commands()
print "Commands: "
for command in commands:
print "\t" + command
if len(sys.argv) == 2:
elif len(sys.argv) > 2:
execute_command(sys.argv[1], sys.argv[2])
Jump to Line
Something went wrong with that request. Please try again.