Difference between revisions of "Scripting: Other languages"

From Freeplane - free mind mapping and knowledge management software
m (Jython)
 
(15 intermediate revisions by 3 users not shown)
Line 6: Line 6:
  
 
JavaScript can be used without any further installation. To use it just create a <tt>.js</tt> file in <freeplane-user-dir>/scripts, e.g. <tt>Test.js</tt>, and start to code. (Freeplane looks up the right script engine based on the file extension.) Here is an example:
 
JavaScript can be used without any further installation. To use it just create a <tt>.js</tt> file in <freeplane-user-dir>/scripts, e.g. <tt>Test.js</tt>, and start to code. (Freeplane looks up the right script engine based on the file extension.) Here is an example:
<groovy>
+
<syntaxhighlight lang="Groovy">
 
var children = node.children;
 
var children = node.children;
 
print("children: " + children);
 
print("children: " + children);
Line 12: Line 12:
 
   print('child ' + i + ' is ' + children.get(i).plainText);
 
   print('child ' + i + ' is ' + children.get(i).plainText);
 
}
 
}
print('done!');
+
node.detailsText = children.size() + " children";
 +
node.style.textColor = Java.type("java.awt.Color").RED;
 
c.statusInfo = "JavaScript script done!";
 
c.statusInfo = "JavaScript script done!";
</groovy>
+
</syntaxhighlight>
  
You see that the convenient [http:///wiki/index.php/Scripting#On_Groovy_properties_and_the_Scripting_API property access] known from Groovy is also supported for JavaScript.
+
You see that the convenient [[Scripting#On_Groovy_properties_and_the_Scripting_API|property access]] known from Groovy is also supported for JavaScript.
  
 
== Installation of other languages ==
 
== Installation of other languages ==
Line 28: Line 29:
 
# Download the all-in-one Jython-JAR, e.g. [http://search.maven.org/#search%7Cga%7C1%7Ca%3A%22jython-standalone%22 from maven central]. It contains not only the language and all necessary libraries but also the JSR 223 adapter.
 
# Download the all-in-one Jython-JAR, e.g. [http://search.maven.org/#search%7Cga%7C1%7Ca%3A%22jython-standalone%22 from maven central]. It contains not only the language and all necessary libraries but also the JSR 223 adapter.
 
# Copy <code>jython-standalone-<version>.jar</code> to <code><freeplane-user-dir>/lib/</code>.
 
# Copy <code>jython-standalone-<version>.jar</code> to <code><freeplane-user-dir>/lib/</code>.
# Add this <code>lib</code> directory to the script class path, so the .jar will be found. You may simply add <code>lib</code>, as relative paths are considered relative to the Freeplane user directory.
 
 
# Create the following test script as <code><freeplane-user-dir>/scripts/testJython.py</code> (suffix ".py" is mandatory)
 
# Create the following test script as <code><freeplane-user-dir>/scripts/testJython.py</code> (suffix ".py" is mandatory)
 
# Restart Freeplane, so the script appears in the menu.
 
# Restart Freeplane, so the script appears in the menu.
Line 34: Line 34:
 
   c.statusInfo = "Jython's here: Node's text is " + node.text
 
   c.statusInfo = "Jython's here: Node's text is " + node.text
  
Hints for scripting Freeplane in Python/Jython can be found on page [[Scripting:Jython]].
+
Hints for scripting Freeplane in Python/Jython can be found on page [[Scripting: Jython]].
 
 
=== Clojure ===
 
 
 
I tried to provide an instruction to install [http://clojure.org Clojure]. Unfortunately JSR 223 support is not built-in in Clojure so one has to find an appropriate adapter. There are three competing solutions and none of them seem to works with Freeplane:
 
 
 
- http://code.google.com/p/clojure-jsr223/
 
- http://grokbase.com/t/gg/clojure/125qn6c39f/ann-clojure-jsr-223-implementation
 
- https://github.com/pmf/clojure-jsr223
 
 
 
With all of them you get class loading issues due to incompatibilities of Freeplane's OSGI classloader and Clojure's classloader (start [http://stackoverflow.com/questions/2352242/how-can-i-embed-clojure-in-an-rcp-application here], [http://stackoverflow.com/questions/13423048/why-does-clojure-use-the-context-classloader-by-default there] and [https://groups.google.com/forum/#!topic/clojure/gRhD0HOwFGc there] if you want to dig into the problem).
 
 
 
Let us know if you succeed.
 
  
 
== Drawbacks ==
 
== Drawbacks ==
  
Groovy is the distiguished scripting language for Freeplane and will be most probably always be better supported than other languages. Here are the differences:
+
Groovy is the distinguished scripting language for Freeplane and will be most probably always be better supported than other languages. Here are the differences:
  
 
* Since most Freeplane scripting happens in Groovy, you will more likely find one to help you with problems in the Freeplane forum.
 
* Since most Freeplane scripting happens in Groovy, you will more likely find one to help you with problems in the Freeplane forum.
Line 59: Line 47:
 
* The script editor only supports Groovy scripts.
 
* The script editor only supports Groovy scripts.
  
A part of this drawbacks could be worked around in the futures. It could be made possible for instance to change the language for formulas per map. (If you would like to implement that show up in the [http://sourceforge.net/apps/phpbb/freeplane/viewforum.php?f=2 Developmers forum].)
+
A part of this drawbacks could be worked around in the future. It could be made possible for instance to change the language for formulas per map. (If you would like to implement that show up in the [http://sourceforge.net/apps/phpbb/freeplane/viewforum.php?f=2 Developers forum].)
  
 
[[Category:Script]]
 
[[Category:Script]]

Latest revision as of 08:36, 28 July 2020

Starting with Freeplane 1.3.5_05 Groovy isn't your only choice for doing scripting in Freeplane. You can use all languages that (a) build on the Java Virtual Machine (JVM) and (b) provide a JSR 223 adapter. These include most of the large list of JVM-languages , e.g. JavaScript, Jython, Scala, Clojure, ...

Built-in: JavaScript

JavaScript can be used without any further installation. To use it just create a .js file in <freeplane-user-dir>/scripts, e.g. Test.js, and start to code. (Freeplane looks up the right script engine based on the file extension.) Here is an example:

var children = node.children;
print("children: " + children);
for (var i = 0; i < children.size(); i++) {
  print('child ' + i + ' is ' + children.get(i).plainText);
}
node.detailsText = children.size() + " children";
node.style.textColor = Java.type("java.awt.Color").RED;
c.statusInfo = "JavaScript script done!";

You see that the convenient property access known from Groovy is also supported for JavaScript.

Installation of other languages

To install a new language you only have to install all required JAR (.jar) files in <freeplane-user-dir>/lib. Alternatively include JARs from other local folders by extending Freeplane's script classpath. Which JARs you need should be mentioned in the documentation of the language or in documentation of the JSR 223 adapter if is not part of the standard language distribution. Depending on the implementation/quality of the existing JSR 223 adpters problems might occur. If you get another language working please add a section here with a description what you had to do.

Jython

Here a step-by-step instruction to install Jython, the Python implementation that runs on the JVM:

  1. Download the all-in-one Jython-JAR, e.g. from maven central. It contains not only the language and all necessary libraries but also the JSR 223 adapter.
  2. Copy jython-standalone-<version>.jar to <freeplane-user-dir>/lib/.
  3. Create the following test script as <freeplane-user-dir>/scripts/testJython.py (suffix ".py" is mandatory)
  4. Restart Freeplane, so the script appears in the menu.
  5. run it via Tools -> Scripts -> Test Jython -> Execute...one selected node. Note that the same code is also valid in Groovy and JavaScript:
 c.statusInfo = "Jython's here: Node's text is " + node.text

Hints for scripting Freeplane in Python/Jython can be found on page Scripting: Jython.

Drawbacks

Groovy is the distinguished scripting language for Freeplane and will be most probably always be better supported than other languages. Here are the differences:

  • Since most Freeplane scripting happens in Groovy, you will more likely find one to help you with problems in the Freeplane forum.
  • Groovy is (so far) the only language to write formulas and filters with.
  • The Groovy scripting API is extended by some methods and tweaks (see a separate page):
 - Shortcut methods 
 - Simple references to other nodes like ID_1723255651.text
  • The script editor only supports Groovy scripts.

A part of this drawbacks could be worked around in the future. It could be made possible for instance to change the language for formulas per map. (If you would like to implement that show up in the Developers forum.)