|
Programmatic Execution |
|
See Also: |
There are a number of ways for a host application that executes APG scripts to pass data into an APG script, and retrieve data from the APG script after it has executed:
APGScript.Values collection.
Within the APG script,
this data can be accessed via the Script.Values collection.
APGen.RunArgs() or
APGScript.RunArgs(). Within the APG
script, the arguments can be accessed via the
Script.Arguments collection.
APGen.Values
collection. This collection is shared between all
APG scripts
that are executed
by the same APGen
object.
APGScript.Globals object to
manipulate an APG script's global variables and call its functions.
The host application can set and retrieve named values in the APGScript.Values collection using code
like this:
' Open the APG script product.apg
Dim oAPGen, oAPGScript
Set oAPGen =
CreateObject("APGen")
Set oAPGScript =
oAPGen.OpenScript("product.apg")
' Set data in the APGScript.Values collection
oAPGScript("Product_id") = nProduct_id
' Run the APG script
oAPGScript.Run
' Extract returned values from the APGScript.Values collection
Dim sProductStatus
sProductStatus =
oAPGScript("ProductStatus")
...
The APG script can retrieve and set the the named values using Script.Values:
<%# ' ------------------- product.apg
------------------
' Retrieve the product ID passed in
Dim nProductID
nProductID =
Script("Product_id")
' Run some database queries to obtain product information and
' find product status.
Dim rsProduct ' This recordset is
populated
...
' Product information is displayed in HTML
...
' Store
the product status to the Script.Values collection, so the calling program
' can use the data.
Script("ProductStatus") =
rsProduct("status").value
#%>
The host application can pass in arguments using APGen.RunArgs() or
APGScript.RunArgs():
' Run the APG script product.apg
'
Pass in the product id as the first argument
Dim
oAPGen
Set oAPGen =
CreateObject("APGen")
oAPGen.RunArgs
"product.apg", nProduct_id
...
The APG script can retrieve and set the arguments using the Script.Arguments collection:
<%# ' ------------------- product.apg
------------------
' Retrieve
the product
ID passed in as the first argument
Dim nProductID
nProductID =
Script.Arguments(0)
...
#%>
The host application can set global values in the APGen.Values collection:
' Run several APG scripts
Dim
rgScripts
rgScripts = Array("product.apg", "catalog.apg")
' Create an
APGen object
Dim oAPGen
Set oAPGen = CreateObject("APGen")
' Set
the global connection string so it can be used in all APG
scripts
oAPGen("ConnStr")=
"Provider=SQLOLEDB;Initial
Catalog=
pubs;" &_<BR> "UserID=sa;Password=''"
' Run each APG script
Dim sScript
For Each sScript In
rgScripts
oAPGen.Run sScript
Next
...
The APG scripts can retrieve and set the shared values using the APGen.Values
collection:
<%#
Dim cn
Set cn =
Script.CreateObject("ADODB.Connection")
' Use the
connect string stored in the APGen.Values collection
cn.Open APGen("ConnStr")
...
#%>
Note that values stored in APGen.Valuesare shared between all APG scripts that
are executed by the same APGen object
. If one APG script changes a value, all
sibling APG scripts may be affected by the changed value. This is similar
in behavior to the ASP Application object.
When an APG script is run in an ASP page:
<%
' Run Page.apg
Dim oAPGen, oAPGScript
Set oAPGen = Server.CreateObject("APGen")
Set oAPGScript= oAPGen.OpenScript(Server.MapPath("Page.apg"))
oAPGScript.Run
%>
<HTML><BODY>
<H2>The APG Script <%=
oAPGScript.Filename %> has finished execution.</H2>
<A href="<%=
oAPGScript.Util.BuildRelativePath(Server.MapPath("default.asp"),
oAPGScript.Output.Path) %>">
Click here</A> to view the generated web page.
</BODY></HTML>
The APG script can access and manipulate the ASP objects
from the running page using the
ObjectContext.Item property:
<%#
Dim bInASP, Request, Session, Application
' Grab ASP objects from ObjectContext
Set Request = ObjectContext("Request")
If IsEmpty(Request) Then
' We're not running within ASP
bInASP = False
Else
' We're running within ASP
bInASP = True
' Grab the other ASP objects
Set
Session =
ObjectContext("Session")
Set Application =
ObjectContext("Application")
' Do something with the ASP
objects, ala normal ASP
' For example, we
can access the User's ID stored in the Session object
Dim nUserID
nUserID = Session("UserID")
' Or, we can access the ASP
application's root path
Output.Dir =
Request.ServerVariables("APPL_PHYSICAL_PATH")
'
We can even set values in the ASP
objects
Application("last_APGen_exec") =
Now
End If
'...
#%>
The APGScript.Globals
object can be used to set and
retrieve global variables in an
APG script, and to call functions in the APG script. This example consists
of an APG script named globals.apg, which defines global
variables and a Square function:
<%# @LANGUAGE="VBScript" #%>
<%#
' Define two global variables
Dim timeFirstRun
timeFirstRun =
Now
Dim nBiggestSquare
nBiggestSquare = 0
' Define a function
Function Square(n)
Square = n*n
If Square > nBiggestSquare Then
nBiggestSquare = Square
End If
End Function
#%>
The host application can use the APGScript.Globals object to access the script's
variables and functions:
' Create an APGen object
Dim oAPGen
Set oAPGen =
CreateObject("APGen")
' Open the APG script
Dim oAPGScript
Set oAPGScript = oAPGen.OpenScript("globals.apg")
' Run it - important to run global code before using APGScript.Globals
oAPGScript.Run
' Call the Square function (inside globals.apg) using APGScript.Globals
Dim n
n = oAPGScript.Globals.Square(4)
n =
oAPGScript.Globals.Square(7)
' Access the global variables inside globals.apg
Dim nMaxSquare, timeRun
nMaxSquare =
oAPGScript.Globals.nBiggestSquare
timeRun =
oAPGScript.Globals.timeFirstRun