How hta Changed the Way I Deliver Builds - Part 3 - Setting Variables Such as Client or Project

OK, so now we have the basics of our hta file, time to build what we need. The first thing I needed to get my head around was how to pass variables for 'client' and 'project' from the hta to MicroStation. Gotta love Google and after a bit of digging found I could set variables in the hta and have MicroStation pick them up like it would environment variables. The only real issue I had was what was the best way to do this.

My initial reaction was to set the variables straight to system environment variables in the same way MicroStation uses 'username' and others. Only issue i has was that I couldn't get the hta to write to the system without closing it completely. A little more Googling and I found the solution. Hta's allow you to set the variables in a number of ways. System, user, process etc:

http://oreilly.com/pub/h/1107

My Eureka moment was using the variables as 'process' variables. This solved a number of issues. By having them as process variables meant that unless you started MicroStation from the hta then the variables would not be picked up and the build wouldn't work (there is a nice way to add some config errors we'll go through later). So, we end up with the line:

Set oEnv = WshShell.Environment("process")

Problem solved, so now for the drop downs.

There are 2 main ways you can have a drop down menu in a hta. The first is to have the content in the hta iiself. This is fine if you only have a small list to edit for something like a client list. The other way is to have the drop down populated from a text file. The spsi_cad.hta does this both ways. The client list is build in the hta while the project list for each client is build from a text file once the client name has been selected.

For client the code looks like:

<P>Client 
<select onChange=clnt_vars size="1" name="ClientChooser" >
<option value=" "> </option>
<option value="RIO">RIO</option>
<option value="BHPBIO">BHPBIO</option>
<option value="GINDALBIE">GINDALBIE</option>
<option value="RAPLEY">RAPLEY</option>
<option value="WOR_PSNS">WORLEY PARSONS</option>
<option value="SPS">SPS</option>
<option value="TRAINING">TRAINING</option>
</select>
<font size=2><script type="text/vbscript">Document.write(oEnv("Client"))</script></font></P>

What this does is display the client names set by 'option value'. Once a client name is selected, the 'onchange' runs the vbscripts 'clnt_vars' to set the cleint variable:

Sub clnt_vars 

Dim sItem

' ------ SCRIPT CONFIGURATION ------

strVarName = "Client"
strVarValue = Eval("ClientChooser.value") 

' ------ END CONFIGURATION ---------

oEnv(strVarName) = Replace(strVarValue, "\", "/")


location.reload(true)

End Sub 

'location.reload(true)' reloads the hta file and the project list for that client will then be populated for selection under the project drop down. Upon selecting the client name, the line:

<font size=2><script type="text/vbscript">Document.write(oEnv("Client"))</script></font></P>

Then writes the same to the end of the drop down so you know what the current client selection is.

The project drop down is a 2 step approach. By selecting the client name the code looks for a text file that contains a list of projects that can then be selected in the project drop down. The list will only be populated is you have the correct client selected with a list of projects in the text file. 

The way it looks for a client list is to call up 'Proj_num':

<script type="text/vbscript">

Proj_num

</script>

This runs each time the hta window is reloaded, thus the need for:

location.reload(true)

The code for 'Proj_num' looks for a text file with the name <client>num.txt getting the client name once it has been selected from the client list. You will need a blank text file called num.txt or the hta will error out as it cannot find a file.

Once the list has been populated and you select a project, that project number or name will then be populated as a variable for use by MicroStation once started:

Sub proj_vars 

Dim sItem

' ------ SCRIPT CONFIGURATION ------
strVarName = "Project"
strVarValue = Eval("ProjectChooser.value")


' ------ END CONFIGURATION --------

oEnv(strVarName) = Replace(strVarValue, "\", "/")



location.reload(true)

End Sub 

To make it nice and clean, I clear the project drop down selection using:

Sub ClearListbox
For Each objOption in ProjectChooser.Options
objOption.RemoveNode
Next 
End Sub

Note that all this code has <script language="VBScript"> at the top and </script> at the end. This tells the hta that this section is vbscript and note html. Forget to do this and the hta will error out as it looks for html code natively.

Lastly, I also publish the selected project to the hta by using:

<font size=2><script type="text/vbscript">Document.write(oEnv("Project"))</script></font>

If you have a long client list you could have the clients picked up from a text file as well. Very easy to change using the code shown under project.

Next time we'll look at starting packages from the hta and what to look out for. We'll also have a look at how the build is activated and how files can be copied to the desktop from the hta.

All the best.

Anonymous