String type in Javascript macro?



I have the following code:

print("possibleImageName: " + possibleImageName);
var a = possibleImageName.split(".");
print("a: " + a + " typeof a " + (typeof a));
print("a.length: " + a.length);

With the following output:

possibleImageName: 1039607_hnsccTMAB_nuclei_One.svs
a: [Ljava.lang.String;@c0133e typeof a object
a.length: 0

However, in a browser console, I get the expected behavior of

["1039607_hnsccTMAB_nuclei_One", "svs"]

Why is a ending up as a java string, and what do I do about it? I tried using [inline].toString[/inline], but it didn’t help.

BTW, what is the tag for inline code? I’ve hunted around for a mention of what markup this forum uses, and I can’t find any. I also looked around at the Discourse site, and despite the long FAQ, no mention of markup. As best as I can tell Discourse uses Markdown, which is not what this forum seems to use.


I think pretty much all of the GitHub-flavored markdown is supported. In particular, inline code. :smile:

Long ago I put some tips in the weclome post… which is actually surprisingly hard to find even though it’s pinned. So I also put this info into the FAQ - thanks for pointing that out!


Where are you actually running this code? In the Script Editor, with an up-to-date Fiji?

When I run this in the script editor, as javascript, I get the preferred output where the array contents are printed, with a length of 2.

a: [Ljava.lang.String;@c0133e typeof a object

So, this actually is “correct” - e.g. the naivething to do. It’s printing the actual toString of the array, being the class (string array) + object hash reference.

I tried using [inline].toString[/inline], but it didn’t help.

toString is called implicitly when concatenating objects and strings like this… what you would actually want is Arrays.toString on the array, which nicely prints the contents of the array. I suspect many environments (like the Fiji script editor) automatically are calling Arrays.toString

a.length: 0

I suspect this is from running in a bare-bones JavaScript environment that isn’t deducing that your object is an actual Java object with a length field. For example, see this post on SO which includes a workaround.


From Discourse’s About page:

###Flexible Formatting
Mix and match Markdown, BBCode, and HTML formatting in posts.

From a blog post of Jeff Atwood, co-creator of Discourse:

In Discourse, you can mix three forms of markup interchangeably:

  • Markdown
  • HTML (safe subset)
  • BBCode (subset)

And see also this thread on Discourse meta for discussion.


Thanks. I thought I did a full text search on that page for “markdown” and “bbcode” but I guess I failed somehow.


I’m editing my file in geany and running using Plugins>Macros>Run.

I’m not really concerned with the length. I’m trying to see if the name has “svs” as the extension, which is why I’m splitting on “.” and looking at the last element of the array. I’m sure there are other ways to do it.

What do I need to do to have possibleImageName be a string, and be able to split it on “.” and get an array of strings?

(Until ImageJ or Fiji gets a “real” editor that looks better and has syntax highlighting, I would rather use a real code editor. If I get to be using ImageJ more, I might try to hack up an editor plugin using one of the many Java code editor widgets available.)


Thanks. I was trying to use bbcode with [inline]…[/inline] (found with web search), but wasn’t working. Backticks working. I think I’ve seen @ctrueden do something where the code gets highlighted. What does that?


ImageJ2’s Script Editor does have syntax highlighting, and even autocompletion for some languages now thanks to RSTALanguageSupport. But it will never be as good as a dedicated programmer’s IDE.

If you are really serious about that, let us know and we can point you at all the relevant code.

You put three backticks followed by the language name; e.g.:

print("Hello world!");

Renders as:

print("Hello world!");

I’ve actually been editing your posts to make the code more readable that way—so you could edit your own posts just to see how I did it. :wink:


Oh, I’ve been using Plugins>Macros>Edit. I just tried the script editor, and it is indeed much better. BUT, now I’m really confused. I’m getting different run behavior running from the Run menu item in the script editor and using Plugins>Macros>Run. Does the script run in different engines? I’m getting a library error running in the script editor that I’m NOT getting in the other way.

Indeed, the code:

possibleImageName = "1039607_hnsccTMAB_nuclei_One.svs";
print("possibleImageName: " + possibleImageName);
var a = possibleImageName.split(".");
print("a: " + a + " typeof a " + (typeof a));
print("a.length: " + a.length);
for (index in a){print(index + ": " + a[index]);}

has the expected behavior when run from the script editor.


File(currentFolder + SINGLES_OUTPUT_DIRECTORY_NAME).mkdir();

is giving ReferenceError: "File" is not defined. Despite having importPackage(Packages.ij.File);

I do not get that error when run from the Fiji Plugins menu.

What do you suggest?


That is the wrong package. It does not exist. You need:




And yes, they use different engines. The Plugins :arrow_forward: Macros :arrow_forward: Run command is from ImageJ 1.x and performs auto-imports of common packages, which is why your File reference works. ImageJ2 intentionally does not support auto-imports.


OK! That gives me a more consistent basis to work from. It’s a little confusing that there is both IimageJ and non-ImageJ Fiji stuff in the app at the same time. Maybe sometime down the road the two will merge.


ImageJ2 tries to override that sort of ImageJ 1.x functionality where possible. But it is extra work in the imagej-legacy layer. Feel free to file an issue stating that Plugins :arrow_forward: Macros :arrow_forward: Run should invoke the Script Editor, not the IJ1 macro editor.

The ultimate goal is to be completely free of ImageJ 1.x code.


just to save folks having to look
4 indents
3 back ticks to open and 3 back ticks to close code block.