JOSM/Plugins/Scripting

From OpenStreetMap Wiki
Jump to: navigation, search
Help
Available languages
English

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.

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

Configuring a script engines

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:

  1. Select the menu item Scripting -> Configure ...
  2. 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

  1. Select the menu item Scripting -> Run...
  2. Enter the path to the script
  3. Press Run

Running a script


Using the script console

  1. Select the menu item Scripting -> Scripting console
  2. Load and/or edit a script
  3. Press Run
Scripting console with a sample Groovy script


Available Scripts

This is a list of scripts contributed by various authors:

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).")

Download

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).")

Download


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)

Download

Here are some more examples to illustrate the possibilities:

More Python/Jython examples

RCN route validator

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.

Personal tools
Namespaces
Variants
Actions
site
Toolbox