23 | | The {{{scripting}}} plugin doesn't ship with a script engine. A standard Java 6 installation should already include a JavaScript engine (Rhino). |
24 | | If you want to use another scripting language, you have to configure the respective script engine first: |
25 | | |
26 | | 1. Select the menu item '''Scripting''' -> '''Configure ...''' |
27 | | 2. Check whether your preferred scripting engine is already configured. If not, |
28 | | * download the jar file providing the scripting engine |
29 | | * add the path to this jar file to the list of scripting engine jars |
30 | | |
31 | | == Running a Script == |
32 | | |
33 | | 1. Select the menu item '''Scripting''' -> '''Run...''' |
34 | | 2. Enter the path to the script |
35 | | 3. Press '''Run''' |
| 20 | You can run scripts from console or from files. If you pick files, then latest scripts will appear in "scripting" menu. See also |
46 | | === Groovy === |
47 | | {{{ |
48 | | /* |
49 | | * HelloWorld.groovy - displays the number of actually open layers |
50 | | */ |
51 | | import javax.swing.JOptionPane; |
52 | | import org.openstreetmap.josm.Main; |
| 32 | == Implementations details == |
| 33 | The plugin includes a embedded scripting engine for Javascript based on Mozilla Rhino and a |
| 34 | [https://gubaer.github.io/josm-scripting-plugin/apidoc/modules/josm.html Javascript API] for the JOSM application objects.\\ |
| 35 | Alternatively, you can use any scripting language which provides a JSR-223 compatible scripting engine, in particular |
| 36 | [https://groovy-lang.org/index.html Groovy], [https://www.jython.org/ Python], or [https://www.jruby.org/ Ruby].\\ |
57 | | JOptionPane.showMessageDialog(Main.parent, "[Groovy] Hello World!\nYou have ${numlayers} layer(s).") |
| 40 | |
| 41 | == Scripts == |
| 42 | === Scripts in JavaScript === |
| 43 | * https://gist.github.com/Rub21/feb83f57a727ac0d8a34 - Expand abbreviations (US) |
| 44 | * https://gist.github.com/Rub21/47838797856566a8b6ba - Capitalize names and eliminate "name=S/N" |
| 45 | * https://gist.github.com/Rub21/cc055320c925c855926e - expand abbreviations (Peru); [https://www.youtube.com/watch?v=Cpi_5dB1NLQ video demonstration] |
| 46 | |
| 47 | === Scripts in Python === |
| 48 | * [wikitr:/Help/Plugin/Scripting/Python] - various small scripts that are meant to illustrate how to do one thing. |
| 49 | * [wikitr:/Help/Plugin/Scripting/Python/SurfaceTypesOfRoutes] - (Interesting for cycle routes, both loops and linear ones). Short piece of code, but illustrates some interesting concepts and reports on OSM data. Doesn't make changes to the data |
| 50 | * [wikitr:/Help/Plugin/Scripting/Python/RCN_Route_Validator] - This is not a trivial script anymore. It does a lot in a complicated field (networks of cycle node routes with numbered nodes), makes changes to relations, writes to a file that can be pasted to the wiki. Analyzes routes, but also networks of routes or collections of networks of routes, depending on the selection in JOSM when the script was run. |
| 51 | * [attachment:ticket:7991:AutoAddIntersections.py AutoAddIntersections] (needs [wikitr:/Help/Plugin/UtilsPlugin2 UtilsPlugin2]) - Adds command queue listener that will perform utilsplugin2's AddNodesAtIntersections command after encountering Extrude command. (See #7991)\\ |
| 52 | Run once per JOSM session. |
| 53 | |
| 54 | |
| 55 | == Advanced settings == |
| 56 | Search for {{{scripting.}}} substring. |
| 57 | |
| 58 | {{{#!comment |
| 59 | == See also == |
60 | | === JavaScript === |
61 | | {{{ |
62 | | /* |
63 | | * HelloWorld.js - displays the number of currently open layers |
64 | | */ |
65 | | importClass(Packages.javax.swing.JOptionPane) |
66 | | importClass(Packages.org.openstreetmap.josm.Main) |
67 | | |
68 | | function getMapView() { |
69 | | if (Main.main == null) return null |
70 | | if (Main.main.map == null) return null |
71 | | return Main.main.map.mapView |
72 | | } |
73 | | |
74 | | var numlayers = 0 |
75 | | var mv = getMapView() |
76 | | if (mv != null){ |
77 | | numlayers = mv.getNumLayers() |
78 | | } |
79 | | JOptionPane.showMessageDialog(Main.parent, "[JavaScript] Hello World! You have " + numlayers + " layer(s).") |
80 | | }}} |
81 | | |
82 | | === Python === |
83 | | {{{ |
84 | | #!python |
85 | | # |
86 | | # HelloWorld.py - displays the number of currently open layers |
87 | | # |
88 | | from javax.swing import JOptionPane |
89 | | from org.openstreetmap.josm import Main |
90 | | |
91 | | def getMapView(): |
92 | | if Main.main == None: |
93 | | return None |
94 | | if Main.main.map == None: |
95 | | return None |
96 | | return Main.main.map.mapView |
97 | | |
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 | | }}} |
106 | | |
107 | | |
108 | | |
109 | | |
110 | | |
| 62 | ---- |
| 63 | Back to [wikitr:/Plugins Plugin Help] \\ |
| 64 | Back to [wikitr:/Help Main Help] |