VERIFIED SOLUTION i
X

Scripting Samurai: Getting Started with the MapBasic window

UPDATED: July 4, 2017


User-added image
Scripting Samurai: Getting Started with the MapBasic window 
 
User-added imageAre you an inquisitive type of person? Do you sometimes lift the hood of your car and gaze at the engine in wonder, curious to know what all the components are and how they almost magically combine together to safely deliver you from A to B? Well this article will show you how can lift the hood on MapInfo Pro, understand some of the magic that is happening, and even jump in and take control of the MapInfo engine providing a powerful way to control and manipulate the application, all without getting a spot of grease on your hands!

For most Users, the MapBasic Window is a seldom used component of the MapInfo Pro interface hidden away out of sight.

In the 32 bit versions, you can find it under Options >> Show MapBasic Window

In the 64 bit versions, you can find it on the HOME tab, in the Tool Windows gallery

User-added image 

Once opened up, you'll see commands and strings of instructions reflected within the window as you execute different commands, tools and menu options in the MapInfo Pro environment. Each of the actions within the User Interface (UI) will result in a command or multiple commands being shown in the MapBasic Window - tracking the commands and relating them to the action in the User Interface provides an insight on the logic within the MapInfo Engine used to undertake common tasks such as opening and manipulating a series of datasets. 

By undertaking a number of consecutive activities then reviewing the generated script in the MapBasic Window we will be able to gain a greater understanding into how the magic of the MapInfo engine executes the requests from the User Interface. For example, open the MapBasic window but leave it minimized in the work session, now try some simple actions such as:
  • Open a table or a workspace
  • Run a simple SQL Select statement
  • Close all the tables
Now restore the MapBasic window and look at the contents, the exact commands may vary however you should see something similar to this:

User-added image

If we look at the syntax of the commands we can see that they reflect the three steps executed via the UI listed above; open a table, execute a query (& then open the results in a new browser), finally close all tables. This is a simple example however it gives us a sense of the level of information that can be revealed via the MapBasic window.

We can also interact with the MapInfo Pro engine via the MapBasic Window, this can be a real time saver to help automate repetitive tasks and functions. For example click in the MapBasic Window from the earlier exercise and place the cursor at the end of the first line ("Run Application..."), with the cursor in place press the [Enter] key to execute this command. You should see the workspace (or table) reopened. By chaining together multiple commands in this manner we can create basic scripts to automate tasks without use of the GUI components of the application.

Some Examples

Here are 3 things you can do within the MapBasic window that will help you understand the power that comes from working directly with the MapInfo Pro engine: 

1. Create a table of rectangles from a table of coordinates

Requires an open, mappable table "tablename" with four fields that represent two opposite corners of the rectangle object. Open the MapBasic window and type:


     Update tablename set obj = mbr(createline(x1,y1,x2,y2))

2. Set or Update the name of an open Map window

This command allows you to set or modify the name for an open Map window. Click on the title bar of the map window (to ensure it is the active or front window), then click in the MapBasic window and use the following command:


     Set Window FrontWindow() Title "Place the new window title here"

3. Need to open a TAB file with an alias (a temporary name assigned just for the session whilst the file is open)? Here's how you can go about it, open the MapBasic window and use the following command:

     Open table "TableName" as "Alias name" interactive

What else? Many of the 100's of MapBasic functions available can be executed in the MapBasic Window. To check if a particular function is accessible via the MapBasic window, look up the function in the MapBasic Reference Guide and look for the following statement under the Purpose description:

"You can call this function from the MapBasic window in MapInfo Pro."

User-added image

If you see this statement then you're able to use the function within the MapBasic window. 

New in the 64 bit versions

The MapBasic window has some improved functionality in the 64 bit versions of MapInfo Pro. There is automatic syntax highlighting of MapBasic statements and functions.

User-added image

The syntax highlighting is optional. You can turn it off an on with a preference. There is also a preference for the behavior of the Enter key. 

User-added image

So go ahead, lift the hood, watch the magic happen and then jump in to try it out for yourself!

User-added image
Article by Ashley Crane 

When not writing articles for the MapInfo Pro journal, Ashley enjoys hearing about how Pitney Bowes customers are using our solutions to solve real world business problems and is on a journey to make and consume the perfect cup of coffee.

Downloads

  • No Downloads