|
This
example describes a simple script created with the Mira
Pro
Script module. A slightly more advanced script is given in
Example 2.
First, let's consider the simplest script—one
that prints the familiar "Hello World" in a window. Here is a complete
script to do that:
Printf("Hello World")
Just click the Execute button in Mira and you
immediately get the message printed in a Mira Text Editor Window (the window
is created if it did not already exist). We could get fancy but this does
the trick. Now let's consider a script that does something more interesting,
something related to images.
A Simple Script to List Image Information
In
this script we interactively collect image files the loop over all images
and list some information from the image header. This involves mainly just a
short loop to get the values from each opened image.
The
image at left shows the source script for the
LoadImages()
function [ larger
view ] in the Mira Script Editor. This function is used to interactively open 1 to n images
from files.
We will not describe the function but note that the picture shows the
appearance of a typical script containing values, keywords, comments, and
other elements. The Mira Script Editor is a full
syntax-highlighting editor for Lua scripts. Since Lua scripts are plain text files,
syntax highlighting is a necessity. By default, green text
is used for comments in the script. Similarly,
blue
indicates language syntax, such as while or
if. The colors, fonts, and other attributes are
user-configurable.
The LoadImages() function is
not part of the distributed Pro Script library, but it is included in source
code format. It must be presented to the script using
the Include() command. To use it in the script,
you just invoke it, like this:
S = LoadImages(n)
The argument n sets the
maximum number of images to load. This is optional; if omitted, one image is
loaded. The LoadImages() function creates an
instance of the CImageSet
class, S and returns it to the caller. The calling script learns the
type of object S (and thus how to use S) by
determining the type of object returned by
LoadImages(). Subsequently using the class object
S, the script can then access all of the images.
The script below shows how LoadImages() is
used to collect images, then a loop is used to list information for each one
in turn. This script also opens a Mira Text Editor window, T to show
the results., The loop lists a 50 character file name and the width and
height for each mage.
S = LoadImages()
--
loops over images until done
T = CTextView:new("Image
Info")
for
i=1,S:Count() do
I = S:GetImage(i)
w=I:Cols()
; h=I:Rows() ; s=I:Path(50)
T:Printf("%s [%d,%d]\r\n", s, w, h )
end
In this script, we used an "invisible" definition of a very
important object named
I. Where is I defined? The answer is
that it is implicitly defined as the type of thing returned by S:GetImage(); Lua will understand that I is an object of type CImage. The
CImage class provides
a large number of methods for working with images, including methods that
access the image information we want to list.
In these few lines of script code, we interactively loaded any number of images,
gathered some information about them, created a Mira Text Editor window, and
listed the image information using C-language style text formatting. |