Scripter is an Active-X Automation scripting program that uses a Visual Basic-like model. This means that objects are "drilled down" to get to the commands. If you are completely new to scripting, you may be interested in getting a Visual Basic book or looking into online Visual Basic tutorials/classes to give you an understanding of how Visual Basic works. Alternatively, you may be more comfortable with jumping right in and writing a script.
Before writing a script, you will want to know the steps you want to perform in the user-interface. Almost anything you can do in the program, you can do in a script. The workflow will be very similar. Once you know the sequence of steps you want to perform, you can write the script. The best ways to start writing the script are to 1) use the Help menu, and 2) look at the example scripts.
If you click Help | MapViewer Automation Help from within Scripter, or Home | Help | Automation from within MapViewer, the help file will open. In the Contents tab, open the MapViewer Automation book. Under this book, there are three additional books that are very helpful: Object Overview, Objects, and Methods and Properties.
Specifically, within the Object Overview book, the Object Hierarchy Graphic page is extremely helpful. To see how, let's take the following example. Say you want to create a hatch map and change the gradient colors that are used.
- Open the Object Overview book.
- Click on Object Hierarchy Graphic.
- On the Object Hierarchy page, click on HatchMap under Application | Documents | Plot Document | Layers | Layer | Map Type.
- The HatchMap Object page displays. This shows all of the methods and properties that apply to that object.
- Copy and paste the Example script at the bottom into Scripter. This portion of the script creates the hatch map. This will be the base from which we can add more lines to adjust properties and perform other actions. Note: any line that begins with a single quotation mark is a comment.
- Scroll up in the help on the HatchMap Object page and click on FgColorSpectrum in the Properties list.
- This opens the FgColorSpectrum Property page. Copy and paste the Example from here into your script, just before the End Sub line at the bottom.
If you start your script with the property and want to work backwards to then put together the script to create the map the property applies to, you can look below the example(s) on a property page, in the Used by section, for links to the pages for the objects that use that property.
Similarly, if you're on an object page (click the Back button to get to the HatchMap one) and the action you want to perform isn't listed (like adding a legend), look above the Properties section for a description of what object this one is derived from. The HatchMap object is derived from the Map object, so all of the methods and properties of the Map object apply to the HatchMap object.
One of the methods of the Map object is CreateLegend, so you can use this in your script to add a legend to your hatch map. We'll do that in just a moment.
To see how the Objects book is helpful, let's continue on from the previous example. Since we know we can add a legend to our hatch map, let's do so, and then let's display the class names instead of the data ranges in the legend.
- Open the Objects book.
- Click on the Legend Object.
- The Legend Object page displays. Again, this shows all of the methods and properties that apply to that object.
- The majority of the script is redundant to what we already have in our script, since we already have a hatch map created, so just copy the last section, which adds the legend, and paste it into your script above End Sub.
- Back in the help, click on ShowClassName in the Properties list.
- This opens the ShowClassName Property page. Copy and paste the example from here into your script, just before the End Sub line.
To see how the Methods and Properties book is helpful, let's again add to our existing example. Let's say you now want to export your map.
- Open the Methods and Properties book.
- Scroll down and click on Export - Automation.
- On the Export - Automation page, scroll down to the example. Since we already have the Plot set in our script, we just need to copy the second half of the example (starting with the 'Export the map as a bitmap comment). Paste this into your script before End Sub.
Following these steps, the example script looks like this:
Sub main 'Creates an instance of the MapViewer Application 'object and assigns it to the variable named "mvApp" Set mvApp = CreateObject("MapViewer.Application") 'Makes the application main window visible mvApp.Visible = True 'Assigns the location of the samples folder 'to the variable "InPath" InPath$ = mvApp.ApplicationFolder + "samples\" 'Creates a map document object, and assigns it 'to the variable named "Plot" Set Plot = mvApp.Documents.Add(DocType:=mvDocPlot) 'Creates a hatch map object and assigns it to the 'variable named "HatchMap" Set HatchMap = Plot.CreateHatchMap( BoundaryFileName:=InPath$ + _ "co2010.gsb", DataFileName:=InPath$ + "co2010.dat", PIDCol:=1, _ VarCol:=5, NumClasses:=4) HatchMap.FgColorSpectrum.LoadFromList("Flame") 'Create a map legend and assign it to the 'variable named "Legend" Set Legend = HatchMap.CreateLegend( Left:=-2, Top:=6 ) 'ShowClassHits 'Show the counts for each class in the legend for the hatch map layer Legend.ShowClassHits("Layer #1") = True 'Export the map as a bitmap Plot.Export(FileName:=mvApp.ApplicationFolder + "Samples\export.tif", _ Options:="Width=972, Height=422") End Sub
Click Script | Run to run the script. It should run as-is, but you can update it as desired to fit your specific needs.
Example scripts are also very useful. A number of standalone sample scripts can be found in the Samples\Scripts folder in the MapViewer installation directory. You can also find more sample scripts on the MapViewer Automation knowledge base.
- Debugging with Scripter - This is a webinar about working with Scripter.
- Scripter and Surfer: Samples, Gridding, Contouring, and Simple Loops - This is a webinar about working with Scripter and writing simple scripts. It is presented for Surfer but can be extrapolated to MapViewer.
Updated December 17, 2018