Script is a sequence of commands, that are executed one after each other.[br][br][i]GeoGebra [/i]supports two scripting languages - [i]GGBScript [/i]and [i]Javascript[/i]. You enter scripts in the [i]Scripting [/i]tab of the [img width=16,height=16]https://wiki.geogebra.org/uploads/thumb/3/30/Menu-options.svg/16px-Menu-options.svg.png[/img] [url=https://wiki.geogebra.org/en/Properties_Dialog]Properties Dialog[/url] of the object you want to assign a script to.[br][br][b]Note:[/b] The [i]Properties[/i] panel needs to be closed for scripts to work.
Different objects support different types of scripting; the execution of scripts can be triggered by:[br][list][*]clicking a particular object ([i]OnClick[/i] tab)[/*][*]updating a particular object, when value or properties of the object are changed ([i]OnUpdate[/i] tab)[/*][*]changing the content of an input box ([i]OnChange[/i] tab)[/*][*]when the mouse button is released (or finger is removed from the screen for touch devices) after dragging an object ([i]On Drag-end[/i] tab)[/*][*]loading the file (for JavaScript - [i]Global Javascript[/i] tab)[/*][*]Javascript listeners (see [url=https://wiki.geogebra.org/en/Reference:JavaScript]Reference:JavaScript[/url])[/*][/list]The difference between [i]OnUpdate[/i] and [i]OnChange[/i] scripting for an input box is that in case the script is entered in the [i]OnUpdate[/i] tab, it is executed only when the focus is removed from the input box (by e.g. clicking on a button, clicking in the [i]Graphics View[/i], etc...), whereas scripting entered in the [i]OnChange tab[/i] will be executed as soon as the content of the input box changes, and the focus is still on the input box itself.[br][br][b]Notes:[/b][br][list][*]Check the [url=https://www.geogebra.org/m/bek5uqah]demo of [i]OnChange[/i] and [i]OnUpdate scripts[/i][/url] for an input box.[/*][*]Check the [url=https://www.geogebra.org/m/kxgvyqvj]demo of [i]OnDrag-end[/i] scripting[/url] for a draggable point.[/*][/list]
You can create scripts consisting of [i]GeoGebra [/i]commands, as you would use them in the [url=https://wiki.geogebra.org/en/Input_Bar]Input Bar[/url]. After triggering the script, every command is executed, in the same order they are written, sequentially.[br][br][b]Example:[/b][br][list=1][*][i][math]a[/math][/i] is an integer-valued [url=https://wiki.geogebra.org/en/Slider_Tool]slider[/url] ranging from [math]1[/math] to [math]3[/math] (therefore Increment equals [math]1[/math])[/*][*]type in: [code]list1 = {"red", "green", "blue"}[/code][/*][*]in properties of [i][math]a[/math][/i], set "On Update" script to [code]SetColor(a, Element(list1, a))[br][/code][/*][*]by moving the slider you change its color[/*][/list][br]Every time the slider is moved, there an update occurs. So, for every move the script is called and the value of [math]a[/math] is used to get one color from the list and change the color of the slider [math]a[/math].[br][br][b]Note:[/b] You can use [math]#[/math] to start a comment.[br][br][b]Hint:[/b] Some commands can only be used for scripting. The list of these commands is available in the [url=https://wiki.geogebra.org/en/Scripting_Commands]Scripting_Commands[/url] page.
JavaScript is a programming language used by many Internet technologies. Unlike GeoGebra Script, in Javascript commands don't have to be executed as a simple sequence, but a control flow ([code]if[/code], [code]while[/code], [code]for[/code]) can be used. For generic JavaScript you can find a nice tutorial on [url=https://developer.mozilla.org/en/JavaScript/Guide]developer.mozilla.org[/url]. In GeoGebra, you can use special JavaScript methods which allow you to change the construction. These methods belong to ggbApplet object, which means that you call them as [code]ggbApplet.method_name(parameter,..,parameter)[/code]. For a complete list of these methods see [url=https://wiki.geogebra.org/en/Reference:JavaScript]Reference:JavaScript[/url].[br][br][b]Example:[/b][br][*][code]for(var i =0;i<10;i++)[br] ggbApplet.evalCommand("A_"+i+"=(random()*10,random()*10)");[/code][/*][br]This script creates 10 points [i][math]A0[/math][sub][/sub][/i] to [i][math]A9[/math][sub][/sub][/i] at random coordinates.[br][br][b]Note:[/b] Scripting with JavaScript is very versatile, but many tasks can also be achieved using the simpler [i]GeoGebraScript[/i].
In the [i]Global JavaScript[/i] part of the Scripting tab in the [img width=16,height=16]https://wiki.geogebra.org/uploads/thumb/3/30/Menu-options.svg/16px-Menu-options.svg.png[/img] [i]Properties Dialog[/i] you may define [b]functions [/b](not variables) which will be available from the other scripts. You can also define [i]function ggbOnInit(name, api)[/i], which is called automatically once the construction is loaded. The [i]ggbOnInit [/i]function can be used for registering some [i]listeners[/i], as shown below.[br][br][b]Example:[/b][br][code]function onAdd(name){[br] alert("Object "+name+" was added.");[br]}[br][br]function ggbOnInit(name, api){[br] api.registerAddListener("onAdd");[br]}[/code][br][br]First we defined function [i]onAdd[/i] that given a string shows a message depending on that string. After that, using the [i]ggbOnInit[/i] function, we told [i]GeoGebra [/i]to call this function whenever a new object is added. Once we reload our construction, function [i]ggbOnInit[/i] will be called and since then, when user adds a point named e.g. [i][math]A[/math][/i], message "Object A was added" will appear.[br][br]You can also use [i]listeners[/i] for actions like rename, delete and clear construction. A complete list is available in [url=https://wiki.geogebra.org/en/Reference:JavaScript]Reference:JavaScript[/url].[br][br][b]Note:[/b] Using any ggbApplet methods in [i]Global JavaScript[/i] outside of ggbOnInit will not work as intended since they will be called before the construction is loaded.