AEvents.htmlTEXTMPad(ʵdd AEvents


Controlling MathPad with AppleEvents

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

The required print AppleEvent has a special meaning in MathPad so that it can be used for batch plotting. A print event will open a document, evaluate it, print the resulting plot and then close the document. You can use this shortcut in the Finder by selecting a MathPad document and then choosing Print from the Finder's menu.

To make a script that plots data from several different files, a scriptable text editor can be used to change the data file name in a MathPad document between each print event.

The AEvents Xfun
The XFun "AEvents2" can be added to provide some custom events. These events allow MathPad to be scripted directly without a scriptable text editor. They also allow scripts to directly access some of MathPad's capabilities.

Installing AEvents
To add the custom events:
 1) Quit MathPad if it is runing.
 2) Put the "AEvents2" XFun file into MathPad's "Active XFuns" folder.
 3) Run MathPad.
 4) Quit MathPad.

The first time you run MathPad with AEvents2, the XFun installs a dictionary resource into the MathPad application. This resource is used by AppleScript to determine what events are available. You need to quit MathPad to make this resource visible to AppleScript. After this first time, AppleScript will recognize the custom events.

If you remove AEvents2 from the "Active XFuns" folder, the dictionary resource is not removed from MathPad. AppleScript will still recognize the custom events but MathPad will not be able to process them them. If you wish to use custom custom events you must be sure that the AEvents2 XFun is active.

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

custom events
nameparmaction
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"
  activate
  open file "plot cubic"
  set frame to 1
  repeat 10 times
     clear
     insert "i=" & frame
     evaluate
     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.

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.

clear
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.

evaluate
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. Images will be printed as grayscale.

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.

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

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