1. you can LOOP on that code. You have {.for each.} {.for.} and {.while.} commands for that.
for each may be the best choice.
2. just for your information, the "automate" thing is solely to let an external app to ask HFS to run a script.
The quickest way to run some commands is to go in Debug > run script
3. i'm always happy when i see people scripting with HFS, despite its poor language.
4. to get the $name, that is a variable, you do
{.set|name|Frank.}
and then you retrieve it by {.^name.}
Consider this works inside a connection variable space.
If you need to work outside this context, in a system wide context (that i guess it's your case), you need to put a # symbol in front of the name of the variable, that is: #name