Hello Roland,
Loops are a very good way of simplifying what you are doing in your zscript. They aren’t difficult to use. They are basically like this:
[Loop,/*number of repeats*/ ,
/*commands to repeat go here*/
]/* end of loop*/
It’s not always necessary to know exactly how many times the loop repeats - you can put in a large number and then include a test so that the loop ends when it’s done its work.
So, to be sure that we loop through all the subtools someone might have, we’ll use 300 for our loop. (I don’t know what the maximum possible number of subtools is but 300 is probably more than people will ever use.) We then need a test so that we don’t keep looping for 300 times when there are only 5 subtools.
The SelectDown button is only available whilst there is a subtool to select, so if it’s not available we know we’re at the last subtool. We can test this by using the [IsEnabled] command. Just using the command won’t be enough though; what we want is to be able to go on looping if it’s enabled and stop if it’s not. The way to do this is to use the [If command. The [If command is very useful for testing and means we can put ‘if this test is true do this, otherwise do that’ into our code. Here is how it’s done:
[If,/*test for true or false*/ ,
/*commands if true*/
,
/*commands if false*/
] /*end of If command*/
So, to select the next subtool if there is one and exit the loop if there isn’t we do this:
[Loop,300,
[If, [IsEnabled,Tool:SubTool:SelectDown],
[ISet,Tool:Geometry:SDiv,1]
[IPress,Tool:SubTool:SelectDown]
,/*else no more subtools*/
[ISet,Tool:Geometry:SDiv,1]
[LoopExit]/*exit loop*/
]/*end of If command*/
]/*end of Loop command*/
One other thing to consider with subtools is whether the selected subtool is the top one or not. If the selected subtool is the bottom one then the code won’t do anything at all!
Unfortunately subtools are a little tricky to code for in this respect because they are only ever numbered 0 - 7. These are the subtools visible in the palette, and what’s visible depends on the position of the scrollbar. So subtool number 0 can be any of several different subtools. The simplest way of making sure we are at the top subtool is to use a variation of the code above, using SelectUp instead of SelectDown:
/*code to get to Top subtool*/
[Loop,300,
[If, [IsEnabled,Tool:SubTool:SelectUp],
[IPress,Tool:SubTool:SelectUp]
,/*else at top subtool*/
[LoopExit]/*exit loop*/
]/*end of If command*/
]/*end of Loop command*/
I hope this helps with your zscripting! I am currently adding information on zscripting to the wiki and plan to post some examples and other stuff here before long. The idea is to help people such as yourself develop their zscripting skills. 
Cheers,