Showcase and discover digital art at yex

Follow Design Stacks

Subscribe to our free newsletter to get all our latest tutorials and articles delivered directly to your inbox!

PageMaker Scripting FAQ – Introducing External Scripting

A. What is external scripting?

External scripting is simply the process of using an application besides PageMaker itself to control the scripting process. The commands sent to PageMaker are the same as used in traditional scripting–in fact you can run the identical scripts!

The difference comes in how you process the results. Often a third-party application–such as Frontier on the Macintosh or Visual Basic on the PC–has more power than PageMaker’s limited language. You could use this power to create a better interface to the user, such as a more complex dialog box. Or perhaps you want your database program to communicate database changes right to PageMaker. It wouldn’t be inconceivable to have your database send an updated list of prices and inventory quantities to PageMaker and have it formatted automatically into a standard layout which you devised. Just think of the possibilities!

B. What are the advantages of external scripting?

With the release of PageMaker 6.5 the advantages of external scripting have been greatly reduced. In the past PageMaker didn’t support variables or conditional loops, greatly reducing the usefulness of scripting.

There still are limitations to PageMaker’s language, however. PageMaker cannot create very complicated dialog boxes. It also does not allow much control of the user’s environment, such as the manipulation of files and folders. PageMaker does not let you communicate with other programs, such as passing information to a database. You have to use external scripting to “pull” the information from PageMaker.

C. What are the disadvantages of external scripting?

One disadvantage to using external scripting is that you need to learn two different scripting languages — PageMaker’s and the external application. Since PageMaker’s language is so easy, however, this isn’t too problematic. Anyone used to working in PageMaker will be familiar with PageMaker’s keywords and menu commands. Adapting those concepts to scripting simply involves learning to think in a step-by-step, logical fashion.

Another problem with external scripting is that you must have two programs loaded at the same time: PageMaker and the external scripting application (such as HyperCard on the Mac). If you want to create a number of scripts this can mean many files or applications open at once. This isn’t a fatal flaw unless you are low on system resources (such as memory).

A third problem is that external scripts most likely are not portable across platforms. Adobe has worked very hard in PageMaker 6.5 to make sure that internal scripts written on the Mac will work on the PC and vice versa. (There are a couple subtle differences but they are minor.) But currently there are no external scripting systems that run on both PCs and Macs, so if you want your script to function on both platforms, you’d have to rewrite it in two different languages (such as Frontier and Visual Basic).

Another disadvantage is that like anything else, each external application has its own list of advantages and disadvantages. See the next section for more information on a few types of external scripting programs.

D. What are some Macintosh external scripting applications?

On the Macintosh, there are a number of programs you can use to script PageMaker. Many of these are free or low cost.

1. HyperCard

HyperCard has been around since 1986 when Apple Computer bundled it with the Macintosh Plus. It’s is a surprisingly powerful programming environment, especially when you consider how easy it is to use. HyperCard’s interface and scripting language has spawned many imitators, but the original is all most people need.

HyperCard works with a card-based metaphor. Each card can have it’s own buttons, text fields, etc. In a sense, a card is almost like a floating palette full of interface elements. Each button or text field can have its own script — the script is contained inside the item, so a single card can have dozens or even thousands of tiny scripts. Copying a button also copies its script, so it’s easy to reuse pieces of code.

HyperTalk, HyperCard’s programming language, looks very much like English. Here’s a sample script of a “right arrow” button that you could use to give your script VCR-like controls to switch between cards.

on mouseUp
visual effect wipe left
if the commandKey is down then
go last card
else
go next card
end mouseUp

Easy, eh? HyperTalk is so much like English it actually knows what words like “first” and “last” mean. This makes programming in HyperTalk very simple. But HyperCard is deceptively powerful. In fact, many commercial software programs are written in HyperCard, like Erik Spegilman’s “Maus” CD-ROM or the game MYST.

To use HyperCard to script PageMaker, you first have to tell HC which program you wish to talk with by using the “answer program” command which allows the user to pick an active application to talk with. Once the user has done this, you can save this information so you don’t have to bother asking the user every time.

Here’s a simple example of a HyperTalk program that gets a list of fonts used in the active PageMaker document and puts them into a scrolling text field named “Fonts”:

on mouseUp
answer program "Send Apple Events to:"
put it into theTarget

-- asks PageMaker for list of fonts
request "getfontlist" & return from program theTarget
put it into fontList
-- PageMaker returns a font listing like this:
-- 4,"Times",20,1,1,"Monaco",4,1,1, etc. etc.
-- The first item in the font list is
-- the total number of fonts
put item 1 of fontList into numFonts
-- erase the field called "Fonts"
put "" into card field "Fonts"
put 0 into fontLine
-- We start our index variable (count)
-- at 2 (after font quantity)
put 2 into count

-- here we loop through every font
repeat with i=1 to numFonts
set cursor to busy
-- the third item in PageMaker's list
-- after the font name is either:
-- 1=font used or 0=font not used
if item (count+3) of fontList=1 then
put (item count of fontList) into fontName
-- trim off quotes
delete the first char of fontName
delete the last char of fontName
add 1 to fontLine
-- this simply puts it into the text
-- field on the card
put fontName into line fontLine
of card field "Fonts"
end if
-- since each font takes up 4 items,
-- we add 4 to our index
add 4 to count
end repeat
end mouseUp

With HyperCard 2.2 or better, you can save your “stack” as a stand-alone, double-clickable application. You are then free to distribute this application to anyone you’d like. The only disadvantage to this approach is that the application is very large — even a simple HC app will be over a megabyte in size! (This is because HyperCard embeds most of its own code inside your application in order to process your scripts.)

2. AppleScript

Another popular scripting language on the Macintosh platform is AppleScript. AppleScript is free from Apple and included with all installations of System 7.5 or better. AppleScript consists of several parts: a system extension, which installs in your Extensions folder, scripting commands which install in the “Scripting Additions” folder inside your Extensions folder, and the Script Editor, an application which you use to compile and execute scripts.

AppleScript’s syntax is similar in structure to HyperTalk, but not nearly as friendly. It tends to be quite particular about how its commands are executed, and often it is difficult to tell where the error is located. Even worse, because AppleScript’s language is extensible and application specific, different applications respond differently to different commands, making the language extremely confusing.

But PageMaker scripters can take heart: using AppleScript to control PageMaker is fairly simple and you don’t have to learn much AppleScript to do it. In fact, because AppleScript was designed with interapplication communication in mind, it’s actually simpler than HyperTalk. Here’s the same program as the HyperTalk example:

-- Font Lister
-- by Marc Zeedar 8/96
-- Here we set up to talk to PageMaker.
-- You may have to change the
-- text in quotes to work for your version
-- of PageMaker.
tell application "Aldus PageMaker 5.0"
-- Here we tell AppleScript that a
-- comma divides items in a list
set AppleScript's text item delimiters to ","
-- We ask PageMaker for the list of fonts
copy (evaluate "getfontlist") to fontList
-- PageMaker returns a font listing
-- similar to this:
-- 4,"Times",20,1,1,"Monaco",4,1,1, etc. etc.
-- The first item in the font list is
-- the total number of fonts
set numFonts to (text item 1 of fontList) as number
copy "" to theFonts
-- We start our index variable (theCount)
-- at 2 (after font quantity)
-- and loop through every font in the list
set theCount to 2
repeat with i from 1 to numFonts
-- the third item in PageMaker's list
-- after the font name is either:
-- 1=font used or 0=font not used
if (text item (theCount+3) of fontList)="1" then
copy (text item theCount of fontList) to fontName
copy theFonts & "," & fontName to theFonts
end if
-- Since each font takes up 4 items,
-- we add 4 to our index
copy (theCount + 4) to theCount
end repeat
-- This shows us the results
display dialog theFonts
end tell
-- end of script

To execute your script, you can either launch it directly from within the Script Editor (click the “Run” button) or you can save the script as a executable application program (double-click to run).

An even better way to run your scripts is to use the shareware OSA menu by Leonard Rosenthol. This program puts a new menu of scripts on your menu bar that’s available within all programs. Simply choosing an installed script from the menu runs it. Other similar options are CE Software’s QuicKeys or Westcode’s One Click macro programs, which can execute AppleScripts.

3. One Click

One of the most exciting Macintosh programs to come along in years is WestCode’s One Click. It’s a combination macro/scripting program. The software is a system extension, and once installed, allows you to create an unlimited number of floating palettes of buttons. These palettes can be linked to specific applications, such as PageMaker, or set to global (available at all times).

The palette contain customizable buttons each with its own script. The script can be something as simple as a macro to choose a menu command (making it easy to create your own floating “toolbar”) or as complex as a real software program with loops and conditionals. One Click scripts can even contain “embedded” AppleScript code, allowing you to mix and match the two scripting languages within a single script!

One Click’s language, EasyScript, really is easy. it’s almost as powerful as HyperTalk but simpler than AppleScript.

This script copies the current selection to the clipboard. It’s the same as choosing “Copy” from the “Edit” menu.

SelectMenu "Edit", "Copy" 

Here’s a longer script that fixes a major irritation of PageMaker 5: this button opens all of PageMaker’s palettes and repositions them where I think they belong. I like this because it drives me nuts to not have all my palettes open and available. Note that in EasyScript comments start with two slashes (//). Note that this script is irrelevant for PageMaker 6.5 as it–finally–remembers palette locations.

// PageMaker AutoPosition
// by Marc Zeedar 8/96
Variable theWind, i
// Opens palettes that aren't open
// and positions them where I like
If NOT Menu("Window", "Tool palette").Checked
SelectMenu "Window", "Tool palette"
End If
Window("Toolbox").Location = 1000, 22
If NOT Menu("Window", "Style palette").Checked
SelectMenu "Window", "Style palette"
End If
Window("Styles").Location = 1000, 100
Window("Styles").Size = 150, 300
If NOT Menu("Window", "Color palette").Checked
SelectMenu "Window", "Color palette"
End If
Window("Colors").Location = 1000, 405
Window("Colors").Size = 150, 250
If NOT Menu("Window", "Control palette").Checked
SelectMenu "Window", "Control palette"
End If
Window("Control Palette").Location = 315, 834
// This resizes and moves all document windows
For i = 1 To Window.Count
If Window(i).Name = "Voyager" OR
Window(i).Name = "Colors" OR
Window(i).Name = "Control Palette" OR
Window(i).Name = "Styles" OR
Window(i).Name = "Toolbox"
// do nothing
Else
Window(i).Location = 2, 22
Window(i).Size = 995, 810
// Exit For
End If
End For
// end of script

To communicate with PageMaker, you can use the Process.SendAE command. This short script deletes all the guides on the current page in PageMaker 5 (PageMaker 6 has a menu command for this feature).

// Delete guides by Marc Zeedar 8/96
Process("*PageMaker*").SendAE "misc,dosc,'----':"
& ""deleterulerguides""

By sending a slew of these cumbersome Process.SendAE commands to PageMaker each with a single scripting instruction you can easily automate PageMaker. Here’s the Font Listing script in EasyScript:

// Font Lister 
// by Marc Zeedar 8/96
// One Click requires we define variables
Variable temp, fontList, fontName
Variable numFonts, theFonts, theCount
// Here we say a comma divides list items
ListDelimiter = ","
// We ask PageMaker for the list of fonts
temp = "misc,dosc,'----':" & (char 210)
& "getfontlist" & (char 211)
fontList = Process("*PageMaker*").SendAE temp
// PageMaker returns a font listing
// similar to this:
// 4,"Times",20,1,1,"Monaco",4,1,1, etc. etc.
// The first item in the font list
// is the total number of fonts
numFonts = MakeNumber (ListItems fontList, 1)
theFonts = ""
// We start our index variable (theCount)
// at 2 (after font quantity)
// and loop through every font in the list
theCount = 2
Repeat numFonts
// the third item in PageMaker's
// list after the font name
// is either: 1=font used or 0=font not used
If (ListItems fontList, (theCount+3))="1"
fontName = ListItems fontList, theCount
theFonts = theFonts & "," & fontName
End If
// Since each font takes up 4 items,
// we add 4 to our index
theCount = theCount + 4
End Repeat
// This shows us the results
Message theFonts
// end of script

My favorite feature of One Click is that it is always available. I don’t have to launch a special program to be able to launch or edit my scripts. One Click’s palettes are graphically pleasing and, if designed correctly, fit transparently into other software programs. Once you get used to using the palettes you have made you won’t be able to live without them.

4. UserLand Frontier

Another Macintosh scripting language is UserLand Frontier by Dave Winer. In functionality it is similar to AppleScript — it also is designed to automate applications. I’d always heard that Frontier was more difficult to learn than AppleScript so for years I stayed away from it, but now that it’s my web authoring system, I plan to explore it more. Here is a sample script from Aldus’ PageMaker 5 Script Lanuage Guide. (Note this example works only with PageMaker 5, although it could be easily changed to 6.5.)

on pmCommand (command)
return (appleEvent('ALD5','misc','eval','----',command+char(0)))

pmCommand("new")

Note that the « character is Frontier’s comment character.

on fontLister()
local
comma = "," « delimiter
thefontList
numFonts

theFonts = ""
theCount = 2
i

thefontList = user.websites.svp.tools.pmCommand("getfontlist")
« -- PageMaker returns a font listing like
« -- similar to this:
« -- 4,"Times",20,1,1,"Monaco",4,1,1, etc. etc.
« -- The first item in the font list is
« -- the total number of fonts
numFonts = string.NthField(thefontList, comma, 1)

« -- We start our index variable (theCount)
« -- at 2 (after font quantity)
« -- and loop through every font in the list
for i = 1 to numFonts
« -- the third item in PageMaker's list
« -- after the font name is either:
« -- 1=font used or 0=font not used
if string.NthField(thefontList, comma, theCount+3) equals "1"
theFonts = theFonts + comma +
string.NthField(thefontList, comma, theCount)
« -- Since each font takes up 4 items,

« -- we add 4 to our index
theCount = theCount + 4 « -- This shows us the results
dialog.notify(theFonts)

Note: I placed my “pmCommand” script in the tools directory of my website (probably not the best place ;-). If you put yours in a different place, you’ll need to change the line

  thefontList = user.websites.svp.tools.pmCommand("getfontlist") 

to reflect the new location in your Frontier database.

E. What are some Windows external scripting applications?

Whereas the Macintosh uses AppleEvents to communicate between software programs, Microsoft Windows uses DDE (Dynamic Data Exchange). Here is an example of DDE communication with PageMaker via a Microsoft Excel macro from Aldus’ PageMaker 5 Script Lanuage Guide.

ch=INITIATE("PageMaker","")
=EXECUTE(ch,"new 5")
=REQUEST(ch,"getpages")
=TERMINATE(ch)=RETURN

This example creates a new 5-page document in PageMaker and then asks how many pages are in the document, displaying the result (which obviously should be five).

1. Visual Basic

Microsoft’s Visual Basic is a popular programming language under the Windows operating system. You can use it to communicate with PageMaker, creating an interface with Visual Basic’s tools.

Contributor Timothy A. Kessler writes in the steps you’d use in Visual Basic 5.0 to send PageMaker a scripting command:

Start a new project

Add a form to the project

Add a textbox to the form

Change the textbox.linktopic to PageMaker|

Make sure you put at least a space after the pipe

Change TextBox.LinkTimeout to the time you want to wait for a response from the PageMaker conversation.

(LinkTimeout is timed in tenths of a second)

Change TextBox.LinkMode to Manual

Then you can invoke a PageMaker script command with the following code

TextBox.LinkExecute (“new 1”)

TextBox.LinkExecute (“box .25 .25 5 5”)

You can only send one command at a time

 

F. Why don’t my PageMaker 6.5 scripts work via AppleScript?

The new script engine of PageMaker 6.5 actually intercepts and processes certain aspects of the PageMaker Scripting Language before those commands are actually sent to PageMaker. PageMaker itself only understands layout commands, like “new” or “box” not script control structures like “loop”. If you send an “if” statement to PageMaker it will respond with an error. Since scripts created with AppleScript go directly to PageMaker and bypass the PageMaker Script Processor, you cannot use script control structures within AppleScript (or any external scripting language).

1. Using PageMaker Scripting Language Commands From Within AppleScript Scripts

Here is an example of an AppleScript that sends commands to PageMaker. The series of commands that it sends works fine when run as a PageMaker script within PageMaker, but it breaks when sent from outside PageMaker using AppleScript.

  tell application "Adobe® PageMaker® 6.5"
activate
do script "zeropointreset"
do script "renderclip in"
do script "paste"
do script "headline = 0"
do script "getselectidlisttop => N, list"
do script "loop n=1, N"
do script "set list => ¬
oID,...,...,...,...,...,...,...,...,...,...,...,...,...,..., list"
do script "headline = oID"
do script "endloop" do script "selectid headline"
do script "textedit"
do script "selectall"
do script "font "LB Helvetica Black""
do script "size 50"
do script "tool pointer"
do script "selectid headline"
do script "move left, guide 2"
do script "move top, guide 1"
do script "deselect"
end tell

Here’s why the AppleScript breaks: AppleScript is sending all these commands to PageMaker, which is then supposed to interpret and execute them. However, PageMaker only understands the layout commands of the PageMaker Scripting Language – all the control commands (loops, if/then decisions, variables, etc.) are handled by a helper application. If you watch carefully when running a script from within PageMaker you’ll see that a tiny application called “PM Script” gets launched. “PM Script” is the application that actually “understands” loops and variables, and it passes only the layout commands to PM.

This means that any loops or variable assignments cannot be used via AppleScript, since PageMaker won’t understand them and PM Script is not scriptable through AppleScript. Since AppleScript can do that stuff itself, it’s not a problem – use AppleScript to do handle those chores. For example, the forth line above tells PageMaker that the variable “headline” equals zero. PageMaker doesn’t understand that command, but you can get the same result by doing this:

  set headline to 0 
copy (evaluate "getselectidlisttop") to theList
repeat with x from 1 to (item 1 of theList)
-- put other PM commands here
end repeat

Note how much more complicated this is – you have to coerce PageMaker responses into AppleScript, use AppleScript to process them and then pass them back to PageMaker. It works, but writing the script is more work. The AppleScript version may also be slower because of the constant switching between AppleScript and PageMaker, but a pure PageMaker script requires switching from PageMaker to PM Script, so there may not actually be that much difference. However, because of the added complexity it probably doesn’t make sense to use AppleScript except in situations where you need to control PageMaker from another program, such as if you want to automate the publication of data from a Filemaker database.

The thing to keep in mind is that you can’t use any of PM 6.5’s new scripting features in an AppleScript script because the new commands are mostly the control language. Only the layout commands can be sent via “do script” or “evaluate”.

The “evaluate” and “do script” commands are functionally identical. The only reason to use one rather than the other is to improve the clarity of your scripts. You can use “evaluate” for code that returns a value (queries) and “do script” for commands that ask PageMaker to manipulate objects.

Here’s a version of the example script that has been re-written so that it will actually work:

  -- This script requires text in the Mac clipboard, and must be run
-- in a PageMaker document that has a horizontal guide 1 and a vertical
-- guide 2. The script pastes the text, styles it to 50pt Helvetica Black,
-- and aligns its top left corner to the intersection of guides 1 and 2.
-- The name of the application in the first line must match your copy of
-- PageMaker exactly.

tell application "Adobe® PageMaker® 6.5"
activate
do script "zeropointreset"
do script "renderclip in"
do script "paste"
-- The following routine finds the ID of the object that was just
-- created and stores it in a variable (objectID").



-- First, we tell AppleScript that a comma divides items in a list:
set AppleScript's text item delimiters to ","

-- Ask PageMaker for the list of selected objects
copy (evaluate "getselectidlisttop") to objectList

-- PageMaker returns a list of selected objects similar to this:
-- numObjects,objectID,(14 other object attributes),objectID, etc., etc.
-- numObjects is the total number of selected objects.
-- It is followed by the IDs and attributes of all selected
-- objects, with the topmost object (the one just created) last.

-- Initialize a variable to hold the ID of our object:
copy 0 to objectID

-- Find out how many objects there are:
set numObjects to (text item 1 of objectList) as number

-- Loop through the list, putting each object ID into
-- the objectID variable, replacing the previous contents
-- each time. The index variable (numObjects) starts at 2 to
-- capture the first object ID:
set currentObject to 2
repeat with i from 1 to numObjects
copy (text item currentObject of objectList) as number to objectID
-- Since each object takes up 15 items, we add 15 to our index:
copy (currentObject + 15) to currentObject
end repeat

-- Now that we have the ID of the new object
-- safely stored in objectID we can start to manipulate it.
-- Note that the next instruction cannot be written as:
-- do script "selectid objectID"
-- because objectID is an AppleScript variable, and we
-- need to send PageMaker the value of that variable,
-- not its name.

do script "selectid " & objectID
do script "textedit"
do script "selectall"


-- Note that the quotes around LB Helvetica Black in the next line must
-- be preceded by backslashes, or AppleScript will generate an error.
do script "font "LB Helvetica Black""
do script "size 50"
do script "tool pointer"
do script "selectid " & objectID
do script "move left, guide 2"
do script "move top, guide 1"
do script "deselect"
end tell



One way to speed this script up dramatically is to collapse all the “do script” lines that aren’t separated by purely AppleScript commands into a single “do script” step, like so:

     tell application "Adobe® PageMaker® 6.5"
activate
do script "zeropointreset
renderclip in
paste"

...
do script "selectid " & objectID & "
textedit
selectall
font "LB Helvetica Black"
size 50
tool pointer
selectid " & objectID & "
move left, guide 2
move top, guide 1
deselect"
end tell

Good luck!

Comments