That is a very good attempt! The error message is because I did not test the code.
Only after seeing your code, it struck me that you actually have encoded in your title the sub-stack you want to make! That allows for a simpler loop; but first let’s explain what is wrong with that next-last line:
commandString+=" image"+win+"=["+imageTitles["+win+"]+"– None --] ";
The literal text is made bold here. What was supposed to be there was the text
commandString += “image15=[-- None --]”;
should your stack have had that many slices. The 15 was supposed to come from the variable
Your loop begins at win=1 but you already added that text to the commandString, so you need to start that loop at win=2 and continue until the last but one, as the last should add that image 15=[–None–] text. This way you built a very long texts string with the pattern imageNNN=[windowtitleNNN] " in it and ending with " image15=[-- None --]" . So much for debugging the code.
So there is another way of solving this, analog to that other question, especially since you already started using an array of length 5, and the number of the stack that you want to put your slices in, is encoded in position 6 of the window title. The image of the window with title “MAX_C4_something” goes in stack number four, right?
You can ask ImageJ for the ID of an image, or a stack. This ID can later be used to reference this image, or stack. You can store the IDs in an array, which you address by position 6.
In a for loop of 1..5,
create a new image
store its ID in stackID[1..5],
For each of the elements (window titles) of the allTitles array
select the window
copy the pixel contents of the window,
Single out position 6 of the window title
Use that as a digital number to address in stackID,
based on position 6, select the appropriate stack by its imageID
add a slice to the stack
paste the clipboard.
Remove the first stack slice which does not hold a pasted image.