wiki:Help/Plugin/Scripting

Version 1 (modified by anonymous, 13 years ago) ( diff )

Initial Version

Scripting Plugin

In a nutshell

The scripting plugins allows you to run scripts within 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. 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:

Configuring a Scripting 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:

  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

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 isn't a neither an explicit scripting API nor any documentation outside of the JOSM code.

Groovy

/*
 * HelloWorld.groovy - displays the number of actually 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

/*
* 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

#
# 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 == None:
		return None
	if Main.main.map == None:
		return None
	return Main.main.map.mapView


numlayers = 0
mv = getMapView()
if mv != None:
	numlayers = mv.getNumLayers()
	
JOptionPane.showMessageDialog(Main.parent, "[Python] Hello World! You have %s layer(s)." % numlayers)
Note: See TracWiki for help on using the wiki.