Work with getFileList



the home directory on a Mac is the one showing the House icon. If you’ve run the macro, you will find a folder there named “TextFiles”. It contains, just for the purpose of demonstration, 14 text files that are named:

vesti export_0.tif_1Veneer_Komposit.txt … to
… vesti export_13.tif_14Veneer_Komposit.txt

Now, if slice 13 of the example stack is currently visible, the message is shown, because there is a text file containing number 13 in the folder named “TextFiles”.

If the currently visible slice of the example stack is 15 then the message will not be shown. To test this, you must change the second line of my macro from

setSlice( 13 );


setSlice( 15 );





changing that line results that there is no message shown.
When I move the slide to slice 13 no message shows either. But it should, because there is a file with number 13 in the directory.




evidently you’ve not perfectly understood my macro which is a stand-alone demo, i.e. every time you start the macro, the slice is set to the value that is defined by “setSlice()”. Consequently you can’t use the slider with my demo macro.

So please realize that the macro is a demo and, as it is written in the code as a comment, its first part is for preparation only and not functional for what you need. This is also indicated by the message “Here comes the beef”.

If you want to use my macro with the slider, you should run it once and, before you run it a again, comment the following two lines of code:

run( “MRI Stack (528K)” );
setSlice( 13 );




Here is a demo version that allows one to use the stack-slider after the first run (without altering the code):

requires( “1.51p” )
// For preparation only
if ( !isOpen( “mri-stack.tif” ) ) {
run( “MRI Stack (528K)” );
setSlice( 13 );
stack = getImageID();
txtPath = getDirectory( “home” ) + “TextFiles/”;
File.makeDirectory( txtPath );
for ( i=1; i<15; i++ ) {
fileName = “vesti export_” + i-1 + “.tif_” + i + “Veneer_Komposit.txt”;
File.saveString( “Just some text”, txtPath + fileName );
showMessage( “Here comes the beef.” );
} else {
stack = getImageID();
// End of preparation
selectImage( stack );
sliceNumber = getSliceNumber();
txtDir = getDirectory( “Choose a Directory” );
fileNameArray = getFileList( txtDir );
for ( i=0; i< fileNameArray.length; i++ ) {
subStringArray = split( fileNameArray[i], “_” );
if ( lengthOf( subStringArray[2] ) == 7 ) { digits = 1; } else { digits = 2; }
number = parseInt( substring( subStringArray[2], 0, digits ) );
if ( sliceNumber == number ) showMessage( “Skip Slice " + sliceNumber + " !” );

Have fun and brilliant teeth




that I understood. But I think I wasn’t able to explain to you what I want the macro to do.

Thank you again for the demo code you wrote. It is working the way it should, but now every time one is changing the slice (with the slider or the arrow keys) one has to re-run the macro. This is not very user friendly.
What I would need is: if the next slice is selected the code should re-run itself. And showing a message if there is a according text file.

I try to and they are :blush:

Best Tobias



I hope you are aware of the fact that peu-á-peu you are changing the task and this is not very agreable for someone who tries to help you.

If what you originally wanted (“It is working the way it should”), now appears to be “not very user friendly” for you, then this is not my fault.

From my point of view, the whole processing should be the other way ’round, i.e. I would write a macro that opens the slice with the lowest number that is not present in the names of the available text files. If this should be the better approach, then I’m sure you will now be able to write the correspondig macro without my help.