![]()
Inter-Process Communication
Introduction
MacSpice (v2.10 onwards) offers several mechanisms for communicating with other applications. These greatly expand the range of problems it can tackle; other applications can compensate for MacSpice's weaknesses, or exploit its strengths.
For example, the result of a noise analysis is often a very long list of values. This is difficult to convert into a designer-friendly form within MacSpice, but the results can easily be passed to the shell for processing by grep(1), sort(1), awk(1), etc., which were designed for this type of task.
Another example is a program that reads several user-supplied parameters then calculates a result. Finding an optimum combination of parameter values manually is a tedious process. However, the MacSpice optimize command can be used to automate the task via the inter-process communication mechanisms described below.
Shell (Backquote) Evaluation
The MacSpice command interpreter handles quoted text as described in the User's Guide (§5.6). Text between backquote characters '`' is expanded in several stages (alias substitution, history substitution, variable evaluation, etc.) before being passed to bash(1) for evaluation.
This example stores the text "date" in the variable 'com', so the command 'date' is evaluated by bash(1)
MacSpice 1 -> set com = "date"
MacSpice 2 -> echo "It is now `$com`"
It is now Sun Jun 17 23:43:05 BST 2007
MacSpice 3 ->
Quote characters to be passed to bash(1) need protecting from the MacSpice command interpreter, normally by using backslash quoting. Quote characters in results returned by bash(1) also need protecting, so things can get ugly:
MacSpice 3 -> echo `echo \\\"hello world\\\"`
"hello world"
MacSpice 4 ->
Sending Apple Events to MacSpice
MacSpice has handlers for the basic set of Apple Events described in its dictionary. The most interesting of these is the 'misc/dosc' event handler which allows AppleScript to send commands for interpretation by MacSpice e.g.
tell application "MacSpice"
do script "let j = 0"
do script { "repeat 10", "let j = j + 1", "end" }
end tell
This example shows how a set of lines that blocks when sent one at a time can be sent as a list.
In normal circumstances, events are acknowledged immediately, but are held in a queue until MacSpice is ready to process them.
MacSpice and Applescript
As described in the User's Guide (§5.3.ap), MacSpice can compile and run AppleScripts, and assign the results to MacSpice variables.