Scripts

Scripts allow us to write programs for execution within Manifold using a variety of built in and supported languages.    Scripts can be edited when they are opened in a Script window.   The Info pane will report the language used in a script window.  In addition, JavaScript, IronPython and IronRuby can be edited and executed from the Command window, using the Command window as a REPL (Read-Eval-Print Loop) console.   Add-ins to extend Manifold with new commands can also be written using scripts.   Using Manifold Commander, scripts can be automated, for example, launched every day at a given time, to automate GIS, DBMS, and ETL tasks.

 

To create a script:

 

  1. Right-click into the Project pane and choose Create - New Script to create a new script, or choose File - Create - New Script.
  2. Specify a name for the new script.
  3. Choose the scripting language from the pull down menu.
  4. Press Create Script.
  5. Double-click the new script to open it in the Command Window.
  6. The new script will open with sample text appropriate for the language selected.
  7. Write the script as desired.
  8. The script is automatically saved as we edit.  Make a copy before editing an existing script.

 

We can also write a temporary script in a limited selection of languages that are installed, which will appear as choices of <scripting language> in the View - New Command Window command.  See the discussion in the View - New Command Window topic.

 

To write a temporary script:

 

  1. Choose View - New Command Window - <scripting language>
  2. Write the script as desired.
  3. If we want to save what we've written, we can copy the text and paste it into a script component we create.

 

New scripts created within the Command Window disappear when the Command Window is closed.   To save the script, choose Edit - Save as Script.

 

For tips on editing the text of scripts in the Command Window, see the Editing Queries, Scripts and Comments topic.

 

Running a script:

 

 

Must read info:   All programmers should review the API Documentation online.    The API Documentation provides total details on the API and also provides hundreds of examples, many of which are provided side-by-side in three versions, in C#, in VBScript and in IronPython, for example:

 

The above example connects to an IMG file and retrieves a table that contains image data. The file format can be ERDAS IMG, ENVI IMG, IDRISI IMG or any other supported technology that matches the filename.

 

To create a script:

 

 

Running a script:

 

 

For tips on editing the text of scripts in the Command Window, see the Editing Queries, Scripts and Comments topic.

Languages

Manifold allows scripting in ten different scripting languages by supporting them for use within Script windows.

 

Five scripting  languages are always available for use in every Manifold installation without requiring any additional installation of any kind, either because they are built in to Manifold (Javascript) or because they are automatically available as a result of Microsoft facilities that are required for any Manifold installation.

 

Two additional languages, IronPython and IronRuby, are automatically supported by the Command Window when they are installed (installation is easy).   Three  more languages, F#, PerlScript and PythonScript, are supported when they are installed.   One more language, Google's V8 implementation of Javascript, is easy to install and use as well.

 

Always available languages:

 

 

The always available languages are provide by Microsoft COM/.NET, which are required for Manifold installation.

 

Additional languages built into the Command Window:

 

 

IronPython and IronRuby are supported by the Command Window just like the built in JavaScript engine, but they are not part of  Manifold installations.   IronPython and IronRuby are installed separately.   See the example below for a typical IronPython installation procedure.

 

Additional Supported Languages:

 

 

Manifold understands and supports the above languages for scripting using script components.

 

When using the New Script dialog to create a new script, a  COM, or .NET annotation indicates the type of each language:

 

 

Default Scripts

Following are examples of the default "Hello, World!" scripts which are created when we create a new script using the various languages used by Manifold.   The default scripts write the "Hello, World!" text to a log window that is automatically opened when the script is executed.  In addition to illustrations the code is included below as text to enable copy and pasting from this documentation.

C#

Code:

 

// C#

 

class Script

{

 

static Manifold.Context Manifold;

static void Main()

{

Manifold.Application.Log("Hello, World!");

Manifold.Application.OpenLog();

}

 

}

 

F#

Code:

 

// F#

//

// Note: running script requires F# and F# PowerPack (FSharp.Compiler.CodeDom.dll)

 

module Script

    let mutable Manifold: Manifold.Context = null

    let Main() =

        Manifold.Application.Log("Hello, World!")

        Manifold.Application.OpenLog()

 

IronPython

Code:

 

# IronPython

#

# Note: running script requires IronPython (IronPython.dll)

 

def Main():

Manifold.Application.Log("Hello, World!")

Manifold.Application.OpenLog()

 

IronRuby

Code:

 

# IronRuby

#

# Note: running script requires IronRuby (IronRuby.dll)

 

def Main()

manifold.application.log('Hello, World!')

manifold.application.open_log()

end

 

JScript

Code:

 

// JScript

 

var Main = function()

{

Manifold.Application.Log("Hello, World!");

Manifold.Application.OpenLog();

}

 

JScript.NET

Code:

 

// JScript.NET

 

class Script

{

 

static var Manifold: Manifold.Context;

static function Main()

{

Manifold.Application.Log("Hello, World!");

Manifold.Application.OpenLog();

}

 

}

 

PerlScript

Code:

 

# PerlScript

#

# Note: running script requires ActivePerl

 

sub Main

{

$Manifold->Application->Log("Hello, World!");

$Manifold->Application->OpenLog();

}

 

PythonScript

Code:

 

# PythonScript

#

# Note: running script requires ActivePython

 

def Main():

Manifold.Application.Log("Hello, World!")

Manifold.Application.OpenLog()

 

VB.NET

Code:

 

' VB.NET

 

Class Script

 

Shared Manifold As Manifold.Context

Shared Sub Main()

Manifold.Application.Log("Hello, World!")

Manifold.Application.OpenLog()

End Sub

 

End Class

 

VBScript

Code:

 

' VBScript

 

Sub Main

Manifold.Application.Log "Hello, World!"

Manifold.Application.OpenLog

End Sub

 

Inline Scripts

Queries can include inline scripts using the SCRIPT statement.  Inline scripts provide a way to use script functions in a single self-contained query component.

 

Example: Add a computed field that uses an inline script to set insert date for each record:

 

ALTER TABLE t (

  ADD insertdate DATETIME

    WITH

[[

 

SCRIPT funcs ENGINE 'c#' [[

  class Script

  {

    static System.DateTime F() { return System.DateTime.Now; }

  }

]];

FUNCTION currentdate() DATETIME AS SCRIPT INLINE funcs ENTRY 'Script.F';

 

]]

    AS [[ currentdate() ]]

);

 

After running the above query, existing records in the table will get the current datetime.   If we wait a minute or so to allow the datetime to visibly change and then insert a new record,  either manually or using INSERT, it will get the new, current datetime value.

Example: Installing IronPython 2.7.9

Microsoft has moved IronPython to github as an open source project.   Download the .msi from the IronPython 2.7.9 github page.

 

To add IronPython to Manifold Release 9:

 

 

 

IronPython.dll

IronPython.Modules.dll

Microsoft.Dynamic.dll

Microsoft.Scripting.dll

 

 

 

It is highly likely that future builds of Manifold may automatically detect an IronPython installation and automatically use that without the need to Copy and Paste the above .dll files into the Manifold installation's  extras folder.

Notes

Agnosticism - Manifold is completely agnostic about programming languages, as can be seen by the wide range of languages which are directly supported for scripting in Manifold.   The Manifold Command Window by default supports both IronPython and IronRuby to provide stylish alternatives to JavaScript.  C#, JScript, JScript.NET, VB.NET and VBScript are supported and are always available because of the Microsoft infrastructure required for a Manifold installation.   Manifold also supports F#, PerlScript and PythonScript, which are easy to install if desired.   Google's V8 implementation of Javascript is also easy to install and use.

 

API Documentation

See the Manifold API Documentation website.

 

See Also

Queries

 

Editing Queries, Scripts and Comments

 

Manifold Commander

 

Tools - Add-ins

 

File - Create - New Script

 

Command Window

 

Example: Create and Run a JScript.NET Script - How to create and run simple JScript.NET scripts.

 

Example: VBScript to Create Locations from a Table - Use VBScript to take a table where each record has a name, scale, latitude and longitude and for each record create a Location component in the project.

 

SCRIPT