Convert macro from windows to mac imageJ

Hi everyone!
I ’m quite a novice at imagej macro programming and I am wondering how to convert this macro (programmed in Windows system) into a Mac version: can anyone solve my doubts??
Thanks a lot and best regardings.
The macro we made for kidney segmentation is:

//SEMIAUTOMATIC STEREOLOGY
//SAVE ROI
run(“Scale…”, “x=2 y=2 z=1.0 width=512 height=512 interpolation=Bilinear average process create”);
waitForUser(“Close the first Stack and press ‘ok’ to continue”);
waitForUser(“After saving the scaled stack, Create the RedGrid, close and re-open T2_Scaled”);
name = getTitle;
run(“Stack ROIs”);
waitForUser(“Select the ROI of the liver”);
run(“ROI Manager…”);
waitForUser(“Control the number of the First and Last slice containing the ROI”);
min_value = getNumber(“First slice containing the ROI”, 0);
max_value = getNumber(“Last slice containing the ROI”, 0);
setSlice(min_value);
path = getDirectory(“image”);
list = getFileList(path);
for(i=min_value;i<=max_value;i++){
wait(200);
setSlice(i+1);
selectWindow(name);
roiManager(“add”);
}
fileName2 = getDirectory(“Select the folder to save ROI as Overlay”);
for (z=0; z<roiManager(“count”); ++z) {
wait(200);
roiManager(“Select”, z);
roiManager(“rename”, “BlackGrid” + “00” + min_value + z)
roiManager(“save selected”, fileName2 + “BlackGrid” + “00” + min_value + z + “.zip”)
}
waitForUser(“Close the scaled stack. Open the RedGrid”);

//SET IMAGE CHARACTERISTICS AND APPLY BLACK GRID
run(“To ROI Manager”);
length = roiManager(“count”);
for (i=0; i<length; i++) {
roiManager(“Set Color”, “black”);
roiManager(“Set Line Width”, 0);
//run(“Threshold…”);
setThreshold(-32768, 500);
setThreshold(-32768, 500, “raw”);
setOption(“BlackBackground”, false);
run(“Convert to Mask”, “method=Default background=Light calculate”);
waitForUser(“Close the ROI Manager: ‘Save as overlay’”);
waitForUser(“Save the ‘image sequence’ as BlackGrid and close the open stack”);
waitForUser(“Open the BlackGrid stack”);

//SEMIAUTOMATIC CROSSES SELECTION OF A USER-DEFINED ROI
path = getDirectory(“image”);
list = getFileList(path);
waitForUser(“Select the directory in which to save the measurements”);
dir = getDirectory(“Select the directory in which to save the measurements”);
waitForUser(“Select the directory containing the ROI”);
dir2 = getDirectory(“Select the directory containing the ROI”);
waitForUser(“By clicking ‘ok’ the procedure will be launched: every slice will be opened and analyzed sequentially”);
for(i=min_value;i<max_value;i++){
wait(200);
open(path+list[i]);
run(“Set Measurements…”, “mean min center stack redirect=None decimal=2”);
run(“Set Scale…”, “distance=0 known=0 pixel=1 unit=pixel”);
run(“ROI Manager…”);
roiManager(“Open”, dir2 + “BlackGrid00” + i + “.zip”);
roiManager(“select”, 0);
if (i <= 9) {
title5 = “BlackGrid000” + i + “.tif”;
selectWindow(title5);
setAutoThreshold(“Default”);
//run(“Threshold…”);
} else {
wait(200);
title6 = “BlackGrid00” + i + “.tif”;
selectWindow(title6);
setAutoThreshold(“Default”);
//run(“Threshold…”);
}
wait(200);
run(“Flatten”);
run(“8-bit”);
run(“Restore Selection”);
setThreshold(0, 1);
run(“Analyze Particles…”, “size=4-32 clear results add to manager”);
run(“Input/Output…”, “jpeg=85 gif=-1 file=.csv use_file”);
if(i <= 9) {
title = “BlackGrid000” + i + “-” + “1.tif”;
selectWindow(title);
run(“Close”);
}
if (i > 9) {
title2 = “BlackGrid00” + i + “-” + “1.tif”;
selectWindow(title2);
run(“Close”);
}
wait(200);
setResult(“Slice number”, roiManager(“count”), i);
roiManager(“measure”);
if (roiManager(“measure”) == 0) {
waitForUser(“ROI not defined”);
ROIManager(“delete”);
} else {
wait(200);
slice = getResult(“Slice”, 1) +i;
setResult(“Slice”, 1, slice);
roiManager(“measure”);
wait(5);
numberOfPoints = roiManager(“count”);
for (k = 0; k < numberOfPoints; k++) {
xM = getResult(“XM”, k);
yM = getResult(“YM”, k);
name = getTitle;
dotIndex = indexOf(name, “.”);
name = substring(name, 0, dotIndex);
}
wait(200);
saveAs(“Results”, dir + name + “.csv”);
selectWindow(“ROI Manager”);
run(“Close”);
selectWindow(“Results”);
run(“Close”);
if(i <= 9) {
title3 = “BlackGrid000” + i + “.tif”;
selectWindow(title3);
run(“Close”);
}
if (i > 9) {
title4 = “BlackGrid00” + i + “.tif”;
selectWindow(title4);
run(“Close”);
//waitForUser(“Close the slice already analyzed and corresponding results. Then click ‘ok’ to open the next slice”);
}
}
}
if (i==max_value) {
waitForUser(“All slices have been analyzed”);
}

//RED-GRID IMAGE SEQUENCE OPENING
waitForUser(“Close the BlackGrid stack. Open in ImageJ the RedGrid stack. Click ‘ok’ to continue”);

//IMPORTING OF THE POINTS INTO ROIMANAGER FROM A .CSV FILE
list = getFileList(path);
fileName = getDirectory(“Select the folder containing measurements”);
for(i=min_value;i<max_value;i++){
if (i==0) {
wait(200);
setSlice(1);
run(“ROI Manager…”);
allText = File.openAsString(fileName + “BlackGrid0001.csv”);
text = split(allText, “\n”);
iM = 0;
iX = 3;
iY = 4;
for (k= 0; k < (text.length/3); k++){
line = split(text[k],",");
if (parseInt(line[iM]) <= 153) {
makePoint(parseInt(line[iX]), parseInt(line[iY]));
roiManager(“Add”);
}
}
} else if (i <= 9) {
wait(200);
setSlice(1+i);
run(“ROI Manager…”);
allText = File.openAsString(fileName + “BlackGrid000” + i + “.csv”);
text = split(allText, “\n”);
iM = 0;
iX = 3;
iY = 4;
for (k= 0; k < (text.length/3); k++){
line = split(text[k],",");
if (parseInt(line[iM]) <= 153) {
makePoint(parseInt(line[iX]), parseInt(line[iY]));
roiManager(“Add”);
}
}
} else {
setSlice(1+i);
run(“ROI Manager…”);
allText = File.openAsString(fileName + “BlackGrid00” + i + “.csv”);
text = split(allText, “\n”);
iM = 0;
iX = 3;
iY = 4;
for (k= 0; k < (text.length/3); k++){
line = split(text[k],",");
if (parseInt(line[iM]) <= 153) {
makePoint(parseInt(line[iX]), parseInt(line[iY]));
roiManager(“Add”);
}
}
}
}

if (i==list.length) {
waitForUser(“All measurements have been imported into the ROIManager”);

Java is multiplatform so there is no “windows” or “mac” version.

As gabriel has said you do not require conversion per se. But if you have the odd file separator from windows hard coded in there you can just use this function:

File.separator

This makes the macro then truely OS independent.

2 Likes

Or simply always use the forward-slash / by default, as this is working on all operating systems including Windows. And much shorter (& better to read) than File.separator.

1 Like

If it only were that easy (sigh, :frowning_face:):