String Manipulation in Macros

Previous Topic Next Topic
 
classic Classic list List threaded Threaded
4 messages Options
Reply | Threaded
Open this post in threaded view
|

String Manipulation in Macros

ptandon0
Hi,

I'm using the BatchProcessFolders.txt macro included with imageJ to split channels of 16 bit images. I need to modify the filenames to save the results.

The full filename is passed to function processFile(String path) as a string, but i would like to modify this String to change the filename, something like:

saveAs("Tiff", path.substring(0,path.length()-4)+" - green.tif");

But the macro doesn't seem to have access to String methods, and returns the error "length expected in line 45."

Anyone know a way around this?

Using ImageJ 1.42q.

>ptandon
Reply | Threaded
Open this post in threaded view
|

Re: String Manipulation in Macros

BenTupper
Hi,

Check out...
http://rsb.info.nih.gov/ij/developer/macro/macros.html#strings

I think the following does as you ask.

filename = "/Users/Shared/data/cruise/KN193/examples/ciliate.csv";
//saveAs("Tiff", path.substring(0,path.length()-4)+" - green.tif");
newname = substring(filename, 0, lengthOf(filename)-4) + "-green.csv";
print(newname);


Cheers,
Ben

On Aug 15, 2009, at 1:13 PM, ptandon0 wrote:

> Hi,
>
> I'm using the BatchProcessFolders.txt macro included with imageJ to  
> split
> channels of 16 bit images. I need to modify the filenames to save the
> results.
>
> The full filename is passed to function processFile(String path) as a
> string, but i would like to modify this String to change the filename,
> something like:
>
> saveAs("Tiff", path.substring(0,path.length()-4)+" - green.tif");
>
> But the macro doesn't seem to have access to String methods, and  
> returns the
> error "length expected in line 45."
>
> Anyone know a way around this?
>
> Using ImageJ 1.42q.
>
>> ptandon
> --
> View this message in context: http://n2.nabble.com/String-Manipulation-in-Macros-tp3450486p3450486.html
> Sent from the ImageJ mailing list archive at Nabble.com.

Ben Tupper
Reply | Threaded
Open this post in threaded view
|

Re: String Manipulation in Macros

Michael Schmid
Hi,

when a string argument contains spaces, don't forget to put square  
brackets around it:

saveAs("Tiff", "["+path.substring(0,path.length()-4)+" - green.tif]");

Michael
________________________________________________________________

On 16 Aug 2009, at 03:34, Ben Tupper wrote:

> Hi,
>
> Check out...
> http://rsb.info.nih.gov/ij/developer/macro/macros.html#strings
>
> I think the following does as you ask.
>
> filename = "/Users/Shared/data/cruise/KN193/examples/ciliate.csv";
> //saveAs("Tiff", path.substring(0,path.length()-4)+" - green.tif");
> newname = substring(filename, 0, lengthOf(filename)-4) + "-green.csv";
> print(newname);
>
>
> Cheers,
> Ben
>
> On Aug 15, 2009, at 1:13 PM, ptandon0 wrote:
>
>> Hi,
>>
>> I'm using the BatchProcessFolders.txt macro included with imageJ  
>> to split
>> channels of 16 bit images. I need to modify the filenames to save the
>> results.
>>
>> The full filename is passed to function processFile(String path) as a
>> string, but i would like to modify this String to change the  
>> filename,
>> something like:
>>
>> saveAs("Tiff", path.substring(0,path.length()-4)+" - green.tif");
>>
>> But the macro doesn't seem to have access to String methods, and  
>> returns the
>> error "length expected in line 45."
>>
>> Anyone know a way around this?
>>
>> Using ImageJ 1.42q.
>>
>>> ptandon
>> --
>> View this message in context: http://n2.nabble.com/String- 
>> Manipulation-in-Macros-tp3450486p3450486.html
>> Sent from the ImageJ mailing list archive at Nabble.com.
>
> Ben Tupper
Reply | Threaded
Open this post in threaded view
|

Re: String Manipulation in Macros

dscho
Hi,

On Mon, 17 Aug 2009, Michael Schmid wrote:

> when a string argument contains spaces, don't forget to put square
> brackets around it:
>
> saveAs("Tiff", "["+path.substring(0,path.length()-4)+" - green.tif]");

This might be true for calls to run(<plugin>, <arguments>), but for
saveAs(<format>, <argument>)?

Let's see:

http://pacific.mpi-cbg.de/cgi-bin/gitweb.cgi?p=ImageJA.git;a=blob;f=ij/macro/Functions.java;h=f7b4c55161330c39e0d5020e04677cb70b4f8002;hb=79c981f8f2c069644abcf8d1dca1a91863da85a1#l2357

The definition of the saveAs() function in the macro interpreter does not
parse the argument string.  How about IJ.saveAs()?

http://pacific.mpi-cbg.de/cgi-bin/gitweb.cgi?p=ImageJA.git;a=blob;f=ij/IJ.java;h=81d903695d6df4f3e902acf3a962225fe0928312;hb=79c981f8f2c069644abcf8d1dca1a91863da85a1#l1342

Note that the function with the ImagePlus parameter (which is called by
saveAs() without ImagePlus parameter) is defined a couple of lines below
the linked one.

The only thing that does with the path before handing off to a plugin
determined by the format is updateExtension() (defined below the 2nd
saveAs() function), which does not handle brackets at all.

But when handing off to a plugin, the brackets are added:

http://pacific.mpi-cbg.de/cgi-bin/gitweb.cgi?p=ImageJA.git;a=blob;f=ij/IJ.java;h=81d903695d6df4f3e902acf3a962225fe0928312;hb=79c981f8f2c069644abcf8d1dca1a91863da85a1#l1406

So it appears to me as if your suggestion would actually lead to file
names _including_ brackets.

Let's try:

        run("Blobs (25K)");
        saveAs("Jpeg", "[/tmp/bla with spaces.jpg]");

Uh oh.

        java.lang.IllegalStateException: Output has not been set!

Removing the brackets makes the script work beautiully, spaces in the
filename and all.

Ciao,
Dscho