Controlling MathPad with AppleEvents

A scripting system such as AppleScript can be used with MathPad to automate batch plotting or other data processing tasks.

Event Summary

required suite
open file "name" open or switch to the named document
print file "name" open, evalute, print plot, close
quit quit MathPad

custom events
hide don't show windows
clear delete text from beginning up to cursor
insert "string" insert a line of text at cursor
append "string" append text to end of previous insert line
evaluate evaluate the current document
value "name" return numeric value of a MathPad variable
print plot print the plot window contents
copy plot  copy the plot to the clipboard
[width] specify the width of plot page in pixels
[height] specify the height of plot page in pixels
[scale by] scale the plot and font by the amount given
save clipboard  save the clipboard as a PICT file "clip"
[file "name"] specify the file name
close close the current document (without save)

An AppleScript Example

This example creates a sequence of plots while varying a parameter. Each plot is saved as a PICT file.
A MathPad document named "plot cubic" generates the plot.

--------------------- plot cubic -------------------------
Xmin= -10; Xmax= 10
Ymin= -500; Ymax= 500; Ydiv= 250
plot (i-5)/10*X^3 -- AppleScript will insert values for i

The script evaluates this document once for each plot and varies a the "i" parameter.

tell application "MathPad"
  open file "plot cubic"
  set frame to 1
  repeat 10 times
     insert "i=" & frame
     copy plot width 200 height 200
     save clipboard file ("cubic." & (frame + 100)) -- name the files "cubic.101","cubic.102",...
     set frame to frame + 1
  end repeat
end tell

Event Details

The custom events operate on the current document. If there is no open document, an empty one will be created. The open event (required suite) is used to open an existing document. If MathPad already has the document in a window, the open event will not read from the file. It will move its window to the front and make it the current document.

Suppresses showing windows. This is intended for cases where MathPad is being scripted as a background task. An initial window is opened when MathPad is launched but if a hide event is sent, no further windows will be shown.

insert "text"
Inserts text at the current cursor position. There are no events to position the cursor directly. When a document is opened the cursor is placed at the beginning. The first insert will put text at the beginning of the document. Each insert leaves the cursor at the end of the inserted text.

For convenience insert adds a return to the end of the given text.

append "text"
If the cursor is at a return, the return will be deleted before inserting a new line of text. This has the affect of appending text to the previous line.

Deletes text from the beginning of the document up to the current cursor position. The clear has no effect when a document is first opened because the cursor is at the beginning. If there have been insert events, the cursor will be at the end of the inserted text and clear will delete the inserted text. If there are no errors during evaluation the cursor will remain at the end of the inserted text. The clear event can then be used to return the document text to its original state.

MathPad expects to be in the foreground when it receives an evaluate event. It is probably best to send an activate at some time before sending an evaluate.

Scripts will normally be used to evaluate documents that have been previously set up and debugged. Errors in attempting to evaluate the document may move the cursor. You must restore the document to the expected starting state before trying to run the script again.

The scripting system should always wait for the evaluate event to complete before attempting to send other events. In some cases this may require increasing the timeout value.

value "name"
Returns the value of the named MathPad variable. If the variable is not defined or is an array, no value is returned. An evaluate event must be sent before attempting to access variables.

print plot
Prints the plot without putting up user interaction dialog boxes. Any saved page setup information for the current document will be used.

copy plot [width] [height] [scale by]
If no options are used, the plot window is copied to the clipboard at its current size. The options width and height can be used to specify the size and shape desired. The scale by option can be used to scale both axes and text by some factor. This is helpful for exporting high resolution PICTs.

save clipboard [file "name"]
If there is a PICT in the clipboard, a copy of it will be saved to a PICT file. If no file specification is given it will be saved as "clip" in the folder of the current document. If the file already exists it will be replaced.

The current document will be closed. Any changes made by insert or append events will be discarded.

Event ID codes

MathPad has an AppleScript dictionary resource to define the event codes but if you wish to use some other scripting system or send events directly, the following table gives event details.

Event class 'MPad'
hide 'hide' '----' 'null'
clear 'clrt' '----' 'null'
insert 'insr' '----' 'TEXT'
append 'apnd' '----' 'TEXT'
evaluate 'eval' '----' 'null'
value 'getv' '----' 'TEXT' 'doub'
print plot 'pplt' '----' 'null'
copy plot 'cplt' '----' 'null'
    width 'pixw' 'shor'
    height 'pixh' 'shor'
    scale by 'scal' 'doub'
save clipboard 'sclp' '----' 'fss '
close 'clos' '----' 'null'

Back to MathPad manual