Difference between revisions of "Scripting"

From GeoGebra Manual
Jump to: navigation, search
 
(27 intermediate revisions by 7 users not shown)
Line 1: Line 1:
<noinclude>{{Manual Page}}[[Category:Manual (official)|{{PAGENAME}}]]</noinclude>
+
<noinclude>{{Manual Page|version=5.0}}</noinclude>{{objects|property}}
{{objects|property}}
+
Script is a sequence of commands, that are executed one after each other.
GeoGebra supports two scripting languages -- GGBScript and Javascript. Script is a sequence of actions which can be triggered by  
+
 
* clicking or updating particular object
+
GeoGebra supports two scripting languages - GGBScript and Javascript.  
* loading the file (in case of Javascript)
+
You can enter scripts in the ''Scripting'' tab of the [[File:Menu-options.svg|link=|16px]] [[Properties Dialog]] of the object you want to assign a script to.
 +
{{Note|The ''Properties'' panel needs to be closed for scripts to work.}}
 +
 
 +
 
 +
Different objects support different types of scripting; the execution of scripts can be triggered by :
 +
* clicking a particular object (''OnClick'' tab)
 +
* updating a particular object (when value or properties of the object are changed) (''OnUpdate'' tab)
 +
* changing the content of an input box (''OnChange'' tab)
 +
* when the mouse button is released (or finger is removed from the screen for touch devices) after dragging an object (''On Drag-end'' tab)
 +
* loading the file (for JavaScript - ''Global Javascript'' tab)
 
* Javascript listeners (see [[Reference:JavaScript]])
 
* Javascript listeners (see [[Reference:JavaScript]])
You can set this script via Scripting panel of [[Properties Dialog]].
+
 
 +
The difference between ''OnUpdate'' and ''OnChange'' scripting for an input box is that in case the script is entered in the ''OnUpdate'' tab, it is executed only when the focus is removed from the input box (by e.g. clicking on a button, clicking in the ''Graphics View'', etc...), whereas scripting entered in the ''OnChange tab'' will be executed as soon as the content of the input box changes, and the focus is still on the input box itself.
 +
 
 +
{{Notes|1=<div>
 +
* Check this https://www.geogebra.org/m/bek5uqah demo of ''OnChange'' and ''OnUpdate'' scripts for an input box.
 +
* Check this https://www.geogebra.org/m/kxgvyqvj demo of ''OnDrag-end'' scripting for a draggable point.</div>}}
 +
 
 
==GGBScript==
 
==GGBScript==
You can create scripts consisting of GeoGebra commands.
+
You can create scripts consisting of GeoGebra commands, as you would use them in the [[Input Bar]]. After triggering the script, every command is executed, in the same order they are written, sequentially.
{{Example|
+
* ''a'' is an integer-valued [[Slider Tool|slider]]
+
{{Example|1=<div>
* <nowiki><code>list1={"red","green","blue"}</code></nowiki>
+
* ''a'' is an integer-valued [[Slider Tool|slider]] ranging from 1 to 3 (therefore Increment equals 1)
* in properties of a set On Update script to <code>SetCaption[a,Element[list1,a]</code>
+
* type in: <code><nowiki>list1 = {"red", "green", "blue"}</nowiki></code>
* by moving the slider you change its color}}
+
* in properties of ''a'', set "On Update" script to <code>SetColor(a, Element(list1, a))</code>
 +
* by moving the slider you change its color</div>}}
 +
 
 +
'''Explanation:''' Every time the slider is moved, there an update occurs. So, for every move the script is called and the value of ''a'' is used to get one color from the list and change the color of the slider ''a''.
 +
 
 +
{{Note|You can use <code>#</code> to start a comment}}
 +
{{Hint|Some commands can only be used for scripting. The list of these command is available in the [[Scripting_Commands]] page.}}
  
 
==JavaScript==
 
==JavaScript==
JavaScript is a programming language used by many internet  technologies. Unlike GGB Script, in Javascript the 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 [http://www.w3schools.com/js/default.asp w3schools.com]. In GeoGebra, you can use special JavaScript commands which allow you to change the construction.
+
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 [https://developer.mozilla.org/en/JavaScript/Guide developer.mozilla.org].
 +
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 complete list of these methods see [[Reference:JavaScript]].
 +
 
 +
{{Example|1=
 +
<pre>
 +
for(var i =0;i<10;i++)
 +
  ggbApplet.evalCommand("A_"+i+"=(random()*10,random()*10)");
 +
</pre>
 +
 
 +
This script creates 10 points ''A<sub>0</sub>'' to ''A<sub>9</sub>'' at random coordinates.
 +
}}
 +
{{note|Scripting with JavaScript is very versatile, but many tasks can also be achieved using the simpler GeoGebraScript.}}
 +
 
 +
===Global JavaScript===
 +
In the ''Global JavaScript'' part of the ''Scripting'' tab in the [[File:Menu-options.svg|link=|16px]] ''Properties Dialog'' you may define '''functions''' (not variables) which will be available from the other scripts. You can also define function ''ggbOnInit(name, api)'', which is called automatically once the construction is loaded. The ''ggbOnInit'' function can be used for registering some ''listeners'', as shown below.{{example|1=
 +
<pre>
 +
function onAdd(name){
 +
    alert("Object "+name+" was added.");
 +
}
  
{{Example|{{description}}}}
+
function ggbOnInit(name, api){
 +
    api.registerAddListener("onAdd");
 +
}
 +
</pre>
 +
First we defined function ''onAdd'' that given a string shows a message depending on that string. After that, using the ''ggbOnInit'' function, we told GeoGebra to call this function whenever a new object is added. Once we reload our construction, function ''ggbOnInit'' will be called and since then, when user adds a point named e.g. ''A'', message "Object A was added" will appear. }}
  
Complete list of available commands can be found in [[Reference:JavaScript]]. GeoGebra contains its own JavaScript engine. When exported as [[Dynamic Worksheet]] one can chose whether to use this engine or the one contained in browser in applets.
+
You can also use ''listeners'' for actions like rename, delete and clear construction. A complete list is available in [[Reference:JavaScript]].
 +
{{Note|Using any ggbApplet methods in ''Global JavaScript'' outside of ggbOnInit will not work as intended since they will be called before the construction is loaded.}}

Latest revision as of 11:18, 3 May 2023


Script is a sequence of commands, that are executed one after each other.

GeoGebra supports two scripting languages - GGBScript and Javascript. You can enter scripts in the Scripting tab of the Menu-options.svg Properties Dialog of the object you want to assign a script to.

Note: The Properties panel needs to be closed for scripts to work.


Different objects support different types of scripting; the execution of scripts can be triggered by :

  • clicking a particular object (OnClick tab)
  • updating a particular object (when value or properties of the object are changed) (OnUpdate tab)
  • changing the content of an input box (OnChange tab)
  • when the mouse button is released (or finger is removed from the screen for touch devices) after dragging an object (On Drag-end tab)
  • loading the file (for JavaScript - Global Javascript tab)
  • Javascript listeners (see Reference:JavaScript)

The difference between OnUpdate and OnChange scripting for an input box is that in case the script is entered in the OnUpdate tab, it is executed only when the focus is removed from the input box (by e.g. clicking on a button, clicking in the Graphics View, etc...), whereas scripting entered in the OnChange tab will be executed as soon as the content of the input box changes, and the focus is still on the input box itself.

Notes:

GGBScript

You can create scripts consisting of GeoGebra commands, as you would use them in the Input Bar. After triggering the script, every command is executed, in the same order they are written, sequentially.

Example:
  • a is an integer-valued slider ranging from 1 to 3 (therefore Increment equals 1)
  • type in: list1 = {"red", "green", "blue"}
  • in properties of a, set "On Update" script to SetColor(a, Element(list1, a))
  • by moving the slider you change its color


Explanation: Every time the slider is moved, there an update occurs. So, for every move the script is called and the value of a is used to get one color from the list and change the color of the slider a.

Note: You can use # to start a comment
Note Hint: Some commands can only be used for scripting. The list of these command is available in the Scripting_Commands page.


JavaScript

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 (if, while, for) can be used. For generic JavaScript you can find a nice tutorial on developer.mozilla.org. 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 ggbApplet.method_name(parameter,..,parameter). For complete list of these methods see Reference:JavaScript.

Example:
for(var i =0;i<10;i++) 
   ggbApplet.evalCommand("A_"+i+"=(random()*10,random()*10)");
This script creates 10 points A0 to A9 at random coordinates.
Note: Scripting with JavaScript is very versatile, but many tasks can also be achieved using the simpler GeoGebraScript.

Global JavaScript

In the Global JavaScript part of the Scripting tab in the Menu-options.svg Properties Dialog you may define functions (not variables) which will be available from the other scripts. You can also define function ggbOnInit(name, api), which is called automatically once the construction is loaded. The ggbOnInit function can be used for registering some listeners, as shown below.

Example:
function onAdd(name){
    alert("Object "+name+" was added.");
}

function ggbOnInit(name, api){
    api.registerAddListener("onAdd");
}
First we defined function onAdd that given a string shows a message depending on that string. After that, using the ggbOnInit function, we told GeoGebra to call this function whenever a new object is added. Once we reload our construction, function ggbOnInit will be called and since then, when user adds a point named e.g. A, message "Object A was added" will appear.


You can also use listeners for actions like rename, delete and clear construction. A complete list is available in Reference:JavaScript.

Note: Using any ggbApplet methods in Global JavaScript outside of ggbOnInit will not work as intended since they will be called before the construction is loaded.

Comments

© 2024 International GeoGebra Institute