This document describes how Snap! can be accessed from an outside program to start scripts, send and retrieve information. The model use case is embedding interactive Snap! projects in other websites such as MOOCs or other adaptive learning platforms.
This experimental Snap! API is a set of methods for an IDE_Morph containing a Snap! project. These methods are maintained to work with future versions of Snap! They can be used to trigger scripts, get feedback from running scripts, and access the project's global variables.
Currently the API consists of the following methods:
The getScenes() method returns an array with the names of all scenes in the projects. The minimum number of elements is 1, since there is always at least one scene per project. The scene names are unique strings within the array. Note that the empty string ('') is a valid scene identifier.
#### syntax
ide.getScenes();
#### return value
an Array of Strings, minimum length 1
### IDE_Morph.prototype.getCurrentScene()
The getCurrentScene() method returns a string representing the name of the currently active scene in the project. If the scene is unnamed and empty string is returned.
#### syntax
ide.getCurrentScene();
#### return value
a String, can be an empty String
### IDE_Morph.prototype.switchTo()
The switchTo() method displays the specified scene. It suspends all processes and clones of the previously active scene and passes control to the new scene.
The stop() method immediately terminates all currently running threads in the active scene and removes all temporary clones. It does not trigger a "When I am stopped" event.
The broadcast() method triggers all scripts whose hat block listens to the specified message. An optional callback can be added to be run after all triggered scripts have terminated.
#### syntax
ide.broadcast(message [, callback]);
#### parameters
* message
- string, the message to be sent to all listeners
* callback | optional
- function to execute after all scripts terminate, no arguments
The addMessageListenerForAll() method sets up a function that will be called whenever a message is broadcast. The function takes one argument, the message being broadcast, and can be used to react to any message. Multiple message listeners can be set up, they all get executed in the order in which they were added.
* function to execute whenever a message is sent, takes one argument: The message string
#### return value
undefined
### IDE_Morph.prototype.addMessageListener()
The addMessageListener() method sets up a function that will be called whenever the specified message is broadcast. Multiple message listeners can be set up per message, they all the executed in the order in which they were added.
#### syntax
ide.addMessageListener(message, callback);
#### parameters
* message
* string, the message to which the listener will react. If the message is an empty string the callback will be executed at any broadcast, passing the message as argument
* callback
* function to execute whenever the specified message is sent, takes no argument, except when the message to listen to is the empty string, then it takes the message as argument
#### return value
undefined
#### IDE_Morph.prototype.getMessages()
The getMessage() method returns a new Array that contains all the message strings that occur in the project, both in hat blocks and in broadcast blocks.
the loadProjectXML() method replaces the current project of the IDE with another serialized one encoded in a string in XML format. Note that no user acknowledgement is required, all unsaved edits to the prior project are lost.