Changes between Version 4 and Version 19 of Help/Plugin/Scripting


Ignore:
Timestamp:
(multiple changes)
Author:
(multiple changes)
Comment:
(multiple changes)

Legend:

Unmodified
Added
Removed
Modified
  • Help/Plugin/Scripting

    v4 v19  
    11[[TranslatedPages]]
    2 = Scripting Plugin =
     2= Plugin -> Scripting =
    33
    4 == In a nutshell ==
     4[[Image(https://josm.openstreetmap.de/pluginicon/scripting.jar/images/script-engine.png,link=,48)]]
     5[[PageOutline(2-4,,inline)]]
    56
    6 The {{{scripting}}} plugins allows you to run scripts within JOSM.
    77
    8 Use it to automate small tasks for which no dedicated plugin is available, i.e.
     8== Purpose ==
     9Use it to automate small tasks for which no any existing plugin can do i.e.
    910
    1011* additional quality tests for which no validator test cases are available
     
    1314* exporting to a custom file format not supported by JOSM
    1415
    15 You can use any scripting language which provides a JSR-223 compatible scripting engine, in particular:
    16 * [http://groovy.codehaus.org/ Groovy]
    17 * [http://www.jython.org/ Python]
    18 * [http://www.mozilla.org/rhino/ JavaScript]
    19 * [http://jruby.org/ Ruby]
    2016
    21 == Configuring a Scripting Engine ==
     17== How to execute scripts ==
     18The [https://gubaer.github.io/josm-scripting-plugin scripting plugin] allows you to run scripts within JOSM.
    2219
    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'''
     20You can run scripts from console or from files. If you pick files, then latest scripts will appear in "scripting" menu. See also
    3621
    3722
    38 == Writing a script ==
    39 The {{{scripting}}} plugin doesn't provide a development console for scripts (yet).
     23== How to write your own scripts ==
     24Please carefully inspect both resources:
     25* https://gubaer.github.io/josm-scripting-plugin/doc/documentation.html - follow all links here
     26* https://gubaer.github.io/josm-scripting-plugin/apidoc/modules/josm.html - scan what all classes, modules, mixins and namespaces can do.
    4027
    41 Use your preferred editor or development environment to write the script in your
    42 preferred language.
     28To write functional scripts than "josm.alert(josm.menu.length);" you should combine examples from multiple pages, for example:
     29* [https://gubaer.github.io/josm-scripting-plugin/apidoc/classes/JSAction.html Create an action using "JSAction" class], then [https://gubaer.github.io/josm-scripting-plugin/doc/menu.html attach it to menu as was shown at "Extending the JOSM menu and the JOSM toolbar" page]
    4330
    44 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.
    4531
    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 ==
     33The 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.\\
     35Alternatively, 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].\\
    5337
    54 def numlayers = Main.main?.map?.mapView?.numLayers
    55 if (numlayers == null) numlayers = 0
     38[https://github.com/gubaer/josm-scripting-plugin Source code at GitHub].
    5639
    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 ==
     56Search for {{{scripting.}}} substring.
     57
     58{{{#!comment
     59== See also ==
    5860}}}
    5961
    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----
     63Back to [wikitr:/Plugins Plugin Help] \\
     64Back to [wikitr:/Help Main Help]