Scripting

Scripting related articles including, but not limited to, topics such as:

Language choice
Script conversion

The major theme of the article should be related to scripting creation and usage.

 

Scripting  Chapter  url:PGSguide/Cscripting
  created:2006-06-15 21:11:01   last updated:2006-06-16 13:12:04
  Copyright © 1985-2024 GrasshopperLLC. All Rights Reserved.

User Contributed Comments For Scripting
Tim Doty wrote...2006-08-08 05:20:22

SectionARexx To Python
ARexx scripts can, generally, be converted to Python without too much trouble. There are some caveats, however. ARexx talks with PageStream asynchronously. For most scripts this isn't an issue, but it is good to remember. While a Python script is running it has complete control. User input only goes through controls provided by the script. Because the user can be performing arbitrary actions in PageStream while a script is running the command LockInterface was used to disable the user interface. This command is redundant in Python.

More directly an issue are the various libraries available to ARexx. Some scripts used these libraries to enhance what could be done, primarily for the purpose of creating a graphical user interface. PageStream now has internal support for this but the structure is of course very different. While such scripts could potentially be translated to Python it might be simpler to just re-implement them.

ARexx scripts start with a comment to identify them as being such. Similarly, Python scripts must start with the following lines which identify them as being such:

Python:

#! /usr/bin/env python
# -*- coding: utf-8 -*-

NoteThe embedded interpreter will actually attempt to execute any file it is told is a script.

In ARexx you have to issue the OPTIONS RESULTS command to get return values back from commands. In Python you always get results, but to keep them you must assign them to a variable. For example:

Python:

hFrameNew = CreateColumns('default', 'page', iCurrentPage + 1)

Sometimes, and the above is an example of this, you don't actually need the whole return result. In such cases you can get just the part you want by referencing the list key:

Python:

hFrameNew = CreateColumns('default', 'page', iCurrentPage + 1)['.result']

The downside is that you lose all other return values. In ARexx stem variables get created and you can use them independently, but with Python there is actually only one result returned-and the above essentially throws away everything but the piece you ask for.

There is no substitute for actually consulting the Python documentation, but as lists are essential to using Python with PageStream I will briefly cover them. A list in Python is a structure similar to the directory-and-file tree used to organize the contents of a hard drive.

In a Python list there are keys, each of which has a value. That value might be scalar, but it could also be another list. It is this nesting of lists that gives it a passing similarity to the directory structure. To access a key you follow the variable name with the key enclosed in square brackets.
For example:

Python:

{'.error': 1, '.result': 0, 'stem': {'width': 8.5, 'sides': 'DOUBLE', 'orientation': 'PORTRAIT', 'height': 11.0}}

If that were assigned to ReturnValue then ReturnValue['.error'] has the value of one while ReturnValue['stem'] has the value

Python:

{'width': 8.5, 'sides': 'DOUBLE', 'orientation': 'PORTRAIT', 'height': 11.0}.

Note that PageStream will lowercase all stem variables passed. To avoid confusion it is best to lowercase the stem variables when specifying them in the command call. Compare the following:

Python:

Result = GetDimensions('size')['size']
Result = GetDimensions('Size')['size']

Both commands give identical results, but the first is just clearer.

(For more details on scripting in Python, including documentation for beginners and experts, check out the python documentation page at http://www.python.org/doc/Intros.html Also, take a look at the existing python scripts in the PageStream scripts folder for examples of python scripting with PageStream.)

User Contributed Comments For Scripting