In the next RText release, the old macro functionality – which recorded keystrokes in the editor component for playback – is being replaced with something much more powerful. If you’re using Java 6 or later, you’ll be able to write scripts in Rhino or Groovy (Groovy support requires the embeddable Groovy jar to be placed on RText’s classpath), assign them shortcuts, then use them like any other action in the RText menu bar! These scripts will run in the main RText process, and will have programmatic access to the entire application. This truly allows you to extend RText in any way you can think of.
To create a scripted macro, choose “New Macro…” from the Macros menu:
The name of your macro must be unique among all macros, and will be the menu item text. The description field is optional, and will display in RText’s status bar when you mouse-over the menu item. Give the macro a keyboard shortcut (if you want to), select the scripting language you want to use, and click OK to begin creating your macro. (Note that if you choose to use Groovy, you’ll be reminded in the dialog that the embeddable Groovy jar needs to be placed in the RText install, and RText needs to be restarted to pick it up. Rhino works out-of-the-box with Java 6 JVM’s since it comes standard).
A script template will open in RText:
A rather large comment at the top describes the macro functionality, as well as two pre-defined variables you can use to manipulate the running RText instance:
In this file, you simply write out your macro. As an example, below is simple Rhino script for a (very crude, and in fact mostly incorrect) word counter macro. It counts the number of “words” in the selected text in the current editor, and displays it in a popup dialog. Note the focus here isn’t on the actual correctness of the algorithm, but rather the overall macro structure:
Important things to take away are:
- Macros are run on the EDT, so you are free to call any Swing-related methods you want without wrapping them in SwingUtilities.invokeLater().
- It’s a good idea to wrap your macros in calls to textArea.beginAtomicEdit() and textArea.endAtomicEdit() if you will be manipulating the text in textArea. The latter method should also be in a “finally” block, in case any unexpected exceptions are thrown. These methods cause any action done in your macro to be remembered as a “single operation” for undo/redo purposes. Failure to do this simply means that, if you edit the text in the text component, you may have to press Ctrl+Z multiple times to get what the macro did un-done, depending on what exactly you’re doing. In the example above, we didn’t actually edit the text area’s contents, so beginAtomicEdit()/endAtomicEdit() weren’t strictly necessary, but I left them there for instruction.
Pretty cool, huh? If you assigned this macro a shortcut, you can run it by simply typing said shortcut (be sure to select some text first to watch it work!).
Editing or removing macros can be easily be done in the “Macros” panel of the Options dialog. This panel is easily accessed via the “Macros” -> “Edit Macros…” menu item.
Here you can add new macros, remove old ones, or modify existing ones (modifying either their shortcut/description, or the actual code itself).