Satimage Previous
Making graphs by script
Home Documentation SmileLab Scripting Making graphs by script  
This section presents the rules for having a script make graphs and use them. As you will see, using SmileLab's objects merely consists in applying AppleScript to SmileLab Suite's dictionary, a section in the dictionary of Smile.
Making a new graph
  • To make a new object, use make new, followed by the class of the object to make. The make new command returns a reference to the new object.
    set w to make new graphic window
  • If the object has to belong to another object (its container), use the at preposition.
    set v to make new plot view at w
    set c to make new curve at v
    So far, we are making empty objects: in further pages you will see how to provide data to a graphical object by assigning values to its properties. For example the following will actually draw a curve.
    set w to make new graphic window
    set v to make new plot view at w
    set c to make new curve at v
    set c's xdata to {1, 2, 3, 4, 5}
    set c's ydata to {1, 4, 9, 16, 25}
    draw w


    Import script
  • To set properties at the creation of the object, use the with properties parameter. Some properties have to be set at the creation, and cannot be changed once the object is created. For instance, template.
    set w to make new graphic window with properties {pagewidth: 3 as inches, pageheight: 9/4 as inches, template: "GrayGraphicWindow.plist"}
Saving a graph
When you save a graphic window by script, you have several ways of specifying the format of the document.
  • you can use the as preposition to specify the format of the document, as a string: "bundle" for making a bundle, "PDF " for PDF, "JPEG" for JPEG, "PNGf" for PNG, "TIFF" for TIFF, "BMPf" for BMP, and "8BPS" for PSD (Photoshop). The line below saves the window as a bundle.
    set the_dt to (path to desktop as text)
    save window 2 in file (thedt & "document") as "bundle"
  • without the as preposition, if you supply a file name with an extension when saving the graphic window, Smile will make a document of the corresponding format. Supply ".bundle" to make a bundle, ".pdf" for PDF, ".jpg" for JPEG, ".png" for PNG, ".tiff" for TIFF, ".bmp" for BMP, and ".psd" for PSD (Photoshop). The line below saves the window as a PNG.
    set the_dt to (path to desktop as text)
    save window 2 in file (thedt & "document.png")
  • by default, if Smile does not recognize a valid format nor a known extension, it will save the window as a PDF document. The line below saves the window as a PDF document.
    set the_dt to (path to desktop as text)
    save window 2 in file (thedt & "document")
  • Making a QuickTime movie
To refer to files in AppleScript, you manipulate two kinds of entities: alias and file specification. An existing file is alias "path:to:the:file". To refer to a non-existing file, for instance to supply a file reference to a command which will create the file, use the file specification file "path:to:the:file".
Deleting a graph
You suppress an object with delete.
set w to make new graphic window
delete w
Note the existence of the close event. The close event is sent to a window when the user attempts to close the window, even if the operation will eventually not be completed.
Referring to a graphical object
  • You refer to an object by stating its class, followed by either its name or its index. If the object belongs to another object, you use the of preposition up to the application, the implicit root.
    set w to window "untitled 8"
    set v to plot view 1 of w
  • The container property returns a reference to the container of the object, its direct parent in the hierarchy.
    set w to window "untitled 8"
    set v to plot view 1 of w
    w is container of v
      -- true
  • For any graphical object in a graphic window, the window property returns a reference to the graphic window which contains the object.
    set c to QuickCurve(0, "x", 0)
    set c's formula to "x^3"
    draw c's window
For more details about referring to objects in Smile, click here.
Controlling a graph
Controlling an object in SmileLab mainly consists in reading and changing the values of its properties.
  • you get the current value of a property of an object with get. In all circumstances, get evaluates a reference.
    set d to (get dash of (get curve 3 of v))
  • you assign a value to a property of an object with set:
    set formula of c to "x^3"
  • you assign values to several properties with the special properties property, as in the example below where we make a curve and then in one command we supply it with data, we suppress the stroke of the curve, and we set the data symbols to a red square of 12 pixels.
    set w to make new graphic window
    set v to make new plot view at w
    set c to make new curve at v
    set x to {1, 2, 3, 4, 5}
    set y to {1, 4, 9, 16, 25}
    set properties of c to {xdata:x, ydata:y, line style:0, pattern style:2, pattern size:12, fill color:2}
    draw w


    Import script
QuickPlotLib, the high-level plotting library
SmileLab includes a library of high-level routines, the QuickPlotLib library, to make a graph in really few lines.
QuickPlotLib is an AppleScript library. Smile automatically loads QuickPlotLib when it launches, so any script running in Smile under any form can use QuickPlotLib and call its routines.
Copyright ©2008 Paris, Satimage