More fun with Smalltalk script
Par Hilaire Fernandes le mardi, août 5 2008, 09:48 - Lien permanent
In a previous article, I introduced the Smalltalk script system used in DrGeoII. I exposed how a simple script with argument can be used to generate random numbers. Here I will demonstrate the use of script with arguments. When using argument with your script you can do complex computation and plug the result in the drawing area of DrGeoII.
A script with two points as argument
We will write a simple script to calculate the distance between two points. Note that this feature is built in DrGeoII.
To handle the arguments in our script we use the Smalltalk keyword-message. The argument of our script are two points called pointA and pointB. Before going further, I need to explain the nature of the arguments sent to script.
Nature of the argument
From the user point of view, the argument sent to a script is related to the item selected in the figure by the user. From an internal point of view, this argument is the model object of this visual item. To be accurate we have three layers to represent a geometric item: its models, its costume and its morph, the one the user see in the drawing area. The model of the items are all subclasses to the DrGMathItem.
Therefore the point arguments of our script are instances of DrGMathItem subclasses, exactly DrGPointItem.
Back to our script
Our script comes with two arguments, we can call it @distance:to:@, it is a two-keyword message. From the DrGeoII pop up menu, we open the script browser numerics>edit script and input our script content:

Next, we create two points in the drawing area and we open the script wizard dialog from the menu numerics>use script.

From the wizard dialog, select our distance:to: script, then select the two points, the selected points are flashing, then click somewhere in the drawing area to plug our script.
With the mouse over a script plugged in the figure, we have interesting feedback about its name and its arguments name:

Conclusion
In this article we have explained:
- the nature of the arguments received by the script,
- how to write keyword message
- the use of script in a drawing area
In a next article, we will expose a more complex use of Smalltalk script.
