Skip to content, Skip to search

Changes

Introduction into Macro Programming

10 bytes added, Yesterday at 10:54
Use HTTPS links where feasible
= Functions =
Most of the time, you will call ''functions'' which implement the actions you want to execute. Functions have names, like variables, but they also have ''parameters'' that you can pass to the functions. ImageJ comes with many [httphttps://imagej.net/developer/macro/functions.html predefined functions] that you can call to perform specific calculations or other operations.
This example writes ''Hello, World!'' to the ''Log'' window:
= Installing macros =
To install keyboard shortcuts or tool icons, you need to [httphttps://imagej.net/developer/macro/macros.html#tools wrap macro code in ''macro'' blocks]:
<source lang="java">
</source>
The icon is defined by a funny-looking string (in this case, ''C000R11ee''). To learn how to define your own icon, please have a look [httphttps://imagej.net/developer/macro/macros.html#icons here].
Many tools open an option dialog upon double-click on the icon. You can do that, too, by choosing a name that ends in ''Action Tool Options'':
== More example macros ==
There are quite a lot of macros on the [httphttps://imagej.net/macros/ ImageJ website], and specifically [httphttps://imagej.net/macros/examples/ example macros] including a few from the Macro workshop at the [http://imagejconf.tudor.lu/archive/imagej-user-and-developer-conference-2010 ImageJ conference 2010]. Since there is no categorized index, you might want to use the search facility on [httphttps://imagej.net/developer/index.html this page].
= Overcoming limitations =
== Macro extensions ==
It is possible to implement code in [[Java]] which extends the macro language via the [httphttps://imagej.net/developer/macro/functions.html#Ext <code>Ext</code> prefix]. However, this cannot be done within the macro itself.
== The call function ==
The [httphttps://imagej.net/developer/macro/functions.html#call <code>call</code> function] can invoke Java methods directly. However, this function only supports calling <code>public static</code> Java methods which accept <code>String</code> only, and return <code>String</code> only. Most times, though, the needed function does not conform to these requirements; this approach is suitable mostly for routines designed to be called from the macro language in this way.
== The eval function ==
The [httphttps://imagej.net/developer/macro/functions.html#eval <code>eval</code> function] can execute [[JavaScript]] (or [[BeanShell]] or [[Python]]) code directly. This is a powerful and flexible approach which can invoke nearly any Java API without needing to write external Java code.
Here is an example which sets the position of a [[3D Viewer]] window:
= Further documentation =
A complete description of the macro language, a reference of the built-in functions, and examples can be found [httphttps://imagej.net/developer/index.html here].
[[Category:Scripting]][[Category:Tutorials]]
Bureaucrat, emailconfirmed, incoming, administrator, uploaders
12,110
edits