Changes between Initial Version and Version 1 of Help/Plugin/Scripting


Ignore:
Timestamp:
2011-01-16T19:10:26+01:00 (15 years ago)
Author:
anonymous
Comment:

Initial Version

Legend:

Unmodified
Added
Removed
Modified
  • Help/Plugin/Scripting

    v1 v1  
     1= Scripting Plugin =
     2
     3== In a nutshell ==
     4
     5The {{{scripting}}} plugins allows you to run scripts within JOSM.
     6
     7Use it to automate small tasks for which no dedicated plugin is available, i.e.
     8
     9* additional quality tests for which no validator test cases are available
     10* automatically entering data in very specific situations (i.e. sequences of house numbers)
     11* importing from a custom file format not supported by JOSM
     12* exporting to a custom file format not supported by JOSM
     13
     14You can use any scripting language which provides a JSR-223 compatible scripting engine, in particular:
     15* [http://groovy.codehaus.org/ Groovy]
     16* [http://www.jython.org/ Python]
     17* [http://www.mozilla.org/rhino/ JavaScript]
     18* [http://jruby.org/ Ruby]
     19
     20== Configuring a Scripting Engine ==
     21
     22The {{{scripting}}} plugin doesn't ship with a script engine. A standard Java 6 installation should already include a JavaScript engine (Rhino).
     23If you want to use another scripting language, you have to configure the respective script engine first:
     24
     251. Select the menu item '''Scripting''' -> '''Configure ...'''
     262. Check whether your preferred scripting engine is already configured. If not,
     27     * download the jar file providing the scripting engine
     28     * add the path to this jar file to the list of scripting engine jars
     29
     30== Running a Script ==
     31
     321. Select the menu item '''Scripting''' -> '''Run...'''
     332. Enter the path to the script
     343. Press '''Run'''
     35
     36
     37== Writing a script ==
     38The {{{scripting}}} plugin doesn't provide a development console for scripts (yet).
     39
     40Use your preferred editor or development environment to write the script in your
     41preferred language.
     42
     43Here are three examples which display the number of layers in JOSM. If you want to write more complex scripts you will have to get familiar with the JOSM object model and in consequence with the JOSM code base, because there isn't a neither an explicit scripting API nor any documentation outside of the JOSM code.
     44
     45=== Groovy ===
     46{{{
     47
     48/*
     49 * HelloWorld.groovy - displays the number of actually open layers
     50 */
     51import javax.swing.JOptionPane;
     52import org.openstreetmap.josm.Main;
     53
     54def numlayers = Main.main?.map?.mapView?.numLayers
     55if (numlayers == null) numlayers = 0
     56
     57JOptionPane.showMessageDialog(Main.parent, "[Groovy] Hello World!\nYou have ${numlayers} layer(s).")
     58}}}
     59
     60=== JavaScript ===
     61{{{
     62/*
     63* HelloWorld.js  -  displays the number of currently open layers
     64*/
     65importClass(Packages.javax.swing.JOptionPane)
     66importClass(Packages.org.openstreetmap.josm.Main)
     67
     68function getMapView() {
     69        if (Main.main == null) return null
     70        if (Main.main.map == null) return null
     71        return Main.main.map.mapView
     72}
     73
     74var numlayers = 0
     75var mv = getMapView()
     76if (mv != null){
     77        numlayers = mv.getNumLayers()
     78}
     79JOptionPane.showMessageDialog(Main.parent, "[JavaScript] Hello World! You have " + numlayers + " layer(s).")
     80}}}
     81
     82=== Python ===
     83{{{
     84#
     85# HelloWorld.py  - displays the number of currently open layers
     86#
     87from javax.swing import JOptionPane
     88from org.openstreetmap.josm import Main
     89
     90def getMapView():
     91        if Main.main == None:
     92                return None
     93        if Main.main.map == None:
     94                return None
     95        return Main.main.map.mapView
     96
     97
     98numlayers = 0
     99mv = getMapView()
     100if mv != None:
     101        numlayers = mv.getNumLayers()
     102       
     103JOptionPane.showMessageDialog(Main.parent, "[Python] Hello World! You have %s layer(s)." % numlayers)
     104}}}
     105
     106
     107
     108
     109