JOSM/Plugins/Scripting
Install the scripting plugin to run scripts in any JSR-223 compatible scripting language in JOSM.
Use it to automate small tasks for which no dedicated plugin is available, i.e.
- additional quality tests for which no validator test cases are available
- automatically entering data in very specific situations (i.e. automatically entering a sequences of house numbers)
- importing from a custom file format not supported by JOSM
- exporting to a custom file format not supported by JOSM
You can use any scripting language which provides a JSR-223 compatible scripting engine, in particular:
Contents |
News
| Date | News | Download |
|---|---|---|
| Jun 18, 2011 |
Now includes a scripting console (see GitHub) |
Open JOSM preferences and update plugin scripting |
| Jan 22, 2011 |
Now caches compiled scripts, if possible. Fixes OutOfMemory when running groovy scripts. (see SVN) |
Open JOSM preferences and update plugin scripting (version 25108) |
| Jan 16, 2011 |
Initial version |
Open JOSM preferences and install plugin scripting |
Configuring a Script Engine
The scripting plugin doesn't ship with a script engine. A standard Java 6 installation should already include a JavaScript engine (Rhino). If you want to use another scripting language, you have to configure the respective script engine first:
- Select the menu item Scripting -> Configure ...
- Check whether your preferred scripting engine is already configured. If not,
- download the jar file providing the scripting engine
- add the path to this jar file to the list of scripting engine jars
Running a Script
- Select the menu item Scripting -> Run...
- Enter the path to the script
- Press Run
Using the script console
- Select the menu item Scripting -> Scripting console
- Load and/or edit a script
- Press Run
Available Scripts
This is a list of scripts contributed by various authors:
- address-relation.js(JavaScript) - allows to add address relations to selected ways (provided by user severality)
Writing a script
The scripting plugin doesn't provide a development console for scripts (yet).
Use your preferred editor or development environment to write the script in your preferred language.
Here 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 is neither an explicit scripting API nor any documentation outside of the JOSM code.
Groovy - Example
/* * HelloWorld.groovy - displays the number of currently open layers */ import javax.swing.JOptionPane; import org.openstreetmap.josm.Main; def numlayers = Main.main?.map?.mapView?.numLayers if (numlayers == null) numlayers = 0 JOptionPane.showMessageDialog(Main.parent, "[Groovy] Hello World!\nYou have ${numlayers} layer(s).")
JavaScript - Example
/* * HelloWorld.js - displays the number of currently open layers */ importClass(Packages.javax.swing.JOptionPane) importClass(Packages.org.openstreetmap.josm.Main) function getMapView() { if (Main.main == null) return null if (Main.main.map == null) return null return Main.main.map.mapView } var numlayers = 0 var mv = getMapView() if (mv != null){ numlayers = mv.getNumLayers() } JOptionPane.showMessageDialog(Main.parent, "[JavaScript] Hello World! You have " + numlayers + " layer(s).")
Python
#!python # # HelloWorld.py - displays the number of currently open layers # from javax.swing import JOptionPane from org.openstreetmap.josm import Main def getMapView(): if Main.main and Main.main.map: return Main.main.map.mapView else: return None numlayers = 0 mv = getMapView() if mv and mv.editLayer and mv.editLayer.data: numlayers = mv.getNumLayers() JOptionPane.showMessageDialog(Main.parent, "[Python] Hello World! You have %s layer(s)." % numlayers)
Here are some more examples to illustrate the possibilities:
Help and Howtos
Online help information and howtos are maintained on the JOSM wiki. You can access them from the scripting plugin by hovering over the Scripting menu item and pressing F1.
