| 28 | | 1. Select the menu item '''Scripting''' -> '''Configure ...''' |
| 29 | | 2. Check whether your preferred scripting engine is already configured. If not, |
| 30 | | * download the jar file providing the scripting engine |
| 31 | | * add the path to this jar file to the list of scripting engine jars |
| 32 | | |
| 33 | | == Running a Script == |
| 34 | | |
| 35 | | 1. Select the menu item '''Scripting''' -> '''Run...''' |
| 36 | | 2. Enter the path to the script |
| 37 | | 3. Press '''Run''' |
| 38 | | |
| 39 | | |
| 40 | | == Writing a script == |
| 41 | | The {{{scripting}}} plugin doesn't provide a development console for scripts (yet). |
| 42 | | |
| 43 | | Use your preferred editor or development environment to write the script in your |
| 44 | | preferred language. |
| 45 | | |
| 46 | | 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. |
| 47 | | |
| 48 | | === Groovy === |
| 49 | | {{{ |
| 50 | | /* |
| 51 | | * HelloWorld.groovy - displays the number of actually open layers |
| 52 | | */ |
| 53 | | import javax.swing.JOptionPane; |
| 54 | | import org.openstreetmap.josm.Main; |
| 55 | | |
| 56 | | def numlayers = Main.main?.map?.mapView?.numLayers |
| 57 | | if (numlayers == null) numlayers = 0 |
| 58 | | |
| 59 | | JOptionPane.showMessageDialog(Main.parent, "[Groovy] Hello World!\nYou have ${numlayers} layer(s).") |
| 60 | | }}} |
| 61 | | |
| 62 | | === JavaScript === |
| 63 | | {{{ |
| 64 | | /* |
| 65 | | * HelloWorld.js - displays the number of currently open layers |
| 66 | | */ |
| 67 | | importClass(Packages.javax.swing.JOptionPane) |
| 68 | | importClass(Packages.org.openstreetmap.josm.Main) |
| 69 | | |
| 70 | | function getMapView() { |
| 71 | | if (Main.main == null) return null |
| 72 | | if (Main.main.map == null) return null |
| 73 | | return Main.main.map.mapView |
| 74 | | } |
| 75 | | |
| 76 | | var numlayers = 0 |
| 77 | | var mv = getMapView() |
| 78 | | if (mv != null){ |
| 79 | | numlayers = mv.getNumLayers() |
| 80 | | } |
| 81 | | JOptionPane.showMessageDialog(Main.parent, "[JavaScript] Hello World! You have " + numlayers + " layer(s).") |
| 82 | | }}} |
| 83 | | |
| 84 | | === Python === |
| 85 | | {{{ |
| 86 | | #!python |
| 87 | | # |
| 88 | | # HelloWorld.py - displays the number of currently open layers |
| 89 | | # |
| 90 | | from javax.swing import JOptionPane |
| 91 | | from org.openstreetmap.josm import Main |
| 92 | | |
| 93 | | def getMapView(): |
| 94 | | if Main.main and Main.main.map: |
| 95 | | return Main.main.map.mapView |
| 96 | | else: |
| 97 | | return None |
| 98 | | |
| 99 | | numlayers = 0 |
| 100 | | mv = getMapView() |
| 101 | | if mv != None: |
| 102 | | numlayers = mv.getNumLayers() |
| 103 | | |
| 104 | | JOptionPane.showMessageDialog(Main.parent, "[Python] Hello World! You have %s layer(s)." % numlayers) |
| 105 | | }}} |
| | 26 | == Example scripts in Python == |