-
Notifications
You must be signed in to change notification settings - Fork 2
Defining a script
You can pass an inline script to the script macro as follows:
script("language:source")language can be either a language name or mime type, see Languages.
source is your script’s source code.
Examples:
script("groovy:Math.max(2, 4)")
script("application/javascript:21 * 2")If you pass a valid file path to the script macro, the script’s source code will be read from the file.
You can use absolute file paths or file paths relative to your home directory.
Examples:
script("myscripts/abc.js")
script("/tmp/script.js")If the file name has a known extension (see Languages), the script language will be inferred from the extension.
If the file name does not have a known extension (or you need to override the language for some reason), you can specify the language followed by a colon before the file path:
script("language:path")Examples:
script("javascript:myscripts/abc")
script("application/x-groovy:/tmp/script.js")All languages that have a ScriptEngine available within the IntelliJ IDEA instance can be used with the script macro.
The following table provides an overview of the available engines as of IntelliJ IDEA 2016.3.1:
| Names | Mime Types | File extensions | ScriptEngine |
|---|---|---|---|
nashorn, Nashorn, js, JS, JavaScript, javascript, ECMAScript, ecmascript |
application/javascript, application/ecmascript, text/javascript, text/ecmascript |
js |
|
groovy, Groovy |
application/x-groovy |
groovy |
If you want to write more than a one-liner in JavaScript, you will have to wrap your code in an Immediately invoked function expression (IIFE).
Example:
script("js:(function() {var a = 1; var b = 2; return a + b;})()")Following the script specification parameter, any number of additional arguments can be passed to the script.
A List of those arguments
will be available to the script as _args.
Example: script("groovy:_args[1]", "arg0", "arg1") prints arg1
Other macros provided by IntelliJ or other plugins can be used to dynamically pass arguments to the script.
Example: script("groovy: _args.join(',')", currentPackage(), className(), methodName())
prints the current package, class and method separated by commas.