Satimage Previous | Next
Using Javascript in Smile
Accueil Documentation Smile Javascript terminals Using Javascript in Smile  
The Javascript context
  • By default, the Javascript windows share a common Javascript context. Running a piece of Javascript in a Javascript window makes the variables and functions available in any other window.
  • However, a Javascript window may be bound to a web window by using the contextual menu. In this case, the Javascript window shares the Javascript context with the web window.
Scripting Smile
Javascript in Smile has two abstract class: SmileObject and SmileObjectList.
SmileObject corresponds the Applescript 'basic object' and SmileObjectList provides access to the SmileObject children.
The SmileObject hierarchy is described in the Smile's Applescript dictionnary.
A Smile object descriptor follows the standard Javascript syntax: application.windows[1].text_size. Thus
  • a descriptor must begin with a Smile object,
  • the root object is the global variable named application,
  • white space character in Applescript property or class name may be replaced with an underscore character,
  • SmileObjectList are obtained by appending a 's' charater to the Applescript class,
  • according to the Javascript standard, object indices are 0-based,
  • acces may be done by name or by index (according to the dictionnary),
  • access by id is provided by the method SmileObjectList.getById(), ex: application.text_windows.getById(2)
Applescript handlers (subroutines) are directly available, ex: application.SmileFolders().
Applescript commands (verbs) are available thru the global callASCommand() (see below).
However, some equivalents are implemented as Javascript methods (see below). As usual, exposed methods may be obtain by Object.keys(aSmileObject).

Global added functions

absoluteURL(url,[baseurl]) equivalent of the corresponding Applescript absoluteURL in satimage osax
  • @url: posix path or URL or relative url
  • @baseurl: optional,posix path or URL
  • return a URL
callASCommand(s,[direct],[params]) call an Applescript command
  • @s: string, name of the command
  • @direct: the direct parameter or undefined
  • @params: object containing the other parameters labeled as in the Applescript command
    ex: var pt=callASCommand("convert coordinates",[0.1,12],{'in':aView})
printf(format,…) like the C printf function but returns a string
  • @s: the format string
  • return a URL
    ex: var s=printf("%s %d", "window", 1)
SmileObject methods

SmileObject.newSmileObject(classname,[props],[index]) create a new SmileObject in the SmileObject this
  • @classname: string, class name of the object to be created,
  • @props: an object containing the desired initial properties,
  • @index: 0-based index of the new object. Default append.
SmileObject.deleteSmileObject() delete the SmileObject

SmileObject.reveal() bring to front the window SmileObject.

SmileObject.load(url) read a file as a text file
  • @url: posix path or URL
  • returns a string
SmileObject.store(url,s) write a string in a file (UTF8 encoding)
  • @url: posix path or URL
  • @s: the string
SmileObject.append(url,s) append a string to a file
  • @url: posix path or URL
  • @s: the string
SmileObject.eval(s) excecutes the script and enhances the current Javascript context
  • @s: the Javascript source
  • returns anything (the result of the script)
Application methods

application.pause(t) suspends the execution of the script for a duration t (or until the escape key is pressed) and allows Smile to handle pending update events.
  • @t: the duration
SmileObjectList methods

SmileObjectList.GetById(theID) provided access by id
  • @theID: the id of the target object
  • returns the SmileObject or fails
SmileObjectList.GetByName(s) provided access by name
  • @s: the name of the target object
  • returns the SmileObject or fails
SmileObjectList.splice(start, deleteCount) follows the Array.splice() syntax
  • @start: index of the first element to remove
  • @deleteCount: the numbers of element to remove
SmileObjectList.forEach(f,thisArg) follows the Array.forEach() syntax
  • @f: a callback function
  • @thisArg: an optional this argument
SmileObjectList.find(f,thisArg) follows the Array.find() syntax
  • @f: a callback function
  • @thisArg: an optional this argument
  • returns a SmileObject or undefined
Windows methods

SmileObject.reveal() bring to front the window SmileObject.

The SmileLab part

A matrix is a Float64Array with a property 'ncols'.
A matrix starts with the first row and so on (C storage).
A complex Float64Array is a Float64Array with a property 'complex' set to true.
Be aware that the length property is twice the number of complex numbers.
A polynomial is a Float64Array with a property 'polynomial' set to true.

createArray(n,range) create a Float64Array
  • @n: size of the requested array
  • @range: array for the requested range of values
  • return a Float64Array
readBinArray(path,hints) reads an array in a binary file
  • @path: string, posix path or URL
  • @hints: Object
    • hints.offset offset in bytes
    • hints.length number of items to read
    • hints.fromType type of the items in the binary file. A value in ["real64","real32","sint32","sint16","sint8","uint32","uint16","uint8","complex64","complex32"]. Default "real64".
    • hints.toType type of the resquested typedArray. Values are as hints.fromType. Default value hints.fromType.
  • return TypedArray
writeBinArray(path, array,hints) writes values in a binary file
  • @path: string, posix path or URL
  • @array: an Array or a TypedArray
  • @hints: Object
    • hints.toType type of the written array. As in readBinArray
    • hints.offset offset in bytes in the file
    • hints.length number of items to write
    • hints.skip number of items to skip
evalfFormula(s,params) is an equivalent of the Applescript command evalformula
  • @s: the formula
  • @params: a Javascript object associating the variable names and the corresponding values (Number, Array,TypedArray or matrix.
  • return Number, TypedArray or matrix.
setFormulaContext(s,name,names) add a function to the evalformula context. setFormulaContext is an equivalent of the Applescript command setformulacontext
  • @s: string, a formula
  • @name: string, the name of the created function
  • @names: array of string, the name of the input variables.
Version française
Copyright ©2020 Paris, Satimage