tags: PageMaker OS9 REALbasic Automation

Programming env: OS 9.2, REALbasic 3.2.1, PageMaker 6.5

1. Initializing AppleEvent Object

ae = NewAppleEvent("misc", "dosc", "AK65")
ae.Timeout = 3600
// timeout at 3600 seconds. Some operations in PageMaker are really time consuming, so the default timeout value will really time out.

2. Define Function to Execute Commands

Function ExecCmd(cmd As String) As String
  ae.StringParam("----") = cmd
  if ae.Send then
    return ae.ReplyString
    raise new NilObjectException
  end if
End Function

3. Command Example

  cmd = "Open """ + fullPath + """"
  result = ExecCmd(cmd)

4. Parsing Result

Many of PageMaker’s reply is a long string with fields separated by comma. Realbasic’s NthField/CountFields can parse such strings, but it’s much slow. The following function can help you with it.

Function NextToken(ByRef wholeStr As String, ByRef fieldStart As Integer) As String
  Dim fieldIndex As Integer
  Dim token As String
  if fieldStart <= 0 or fieldStart > Len(wholeStr) then
    return ""
  end if
  fieldIndex = InStr(fieldStart, wholeStr, ",")
  if fieldIndex > 0 then
    token = Mid(wholeStr, fieldStart, fieldIndex  - fieldStart)
    fieldStart = fieldIndex + 1
    token = Mid(wholeStr, fieldStart, Len(wholeStr) + 1 - fieldStart)
    fieldStart = Len(wholeStr) + 1
  end if
  return token
End Function

Example code to call this function:

cmd = "GetFontList"
result = ExecCmd(cmd)
fieldStart = 1 // init fieldStart variable, it will be changed at each call of NextToken
totalFonts = Val(NextToken(result, fieldStart)) // first field is count of all fonts
for fontIndex = 1 to totalFonts
    fontName = NextToken(result, fieldStart)
    fontId = NextToken(result, fieldStart)
    installed = Val(NextToken(result, fieldStart))
    used = Val(NextToken(result, fieldStart))