Now For Something Useful

Here is a macro that inserts the path of the current buffer in the text:

String newText = buffer.getPath();
textArea.setSelectedText(newText);

Unlike in our first macro example, here we are calling class methods on particular objects. First, we call getPath() on the current Buffer object to get the full path of the text file currently being edited. Next, we call setSelectedText() on the current text display component, specifying the text to be inserted as a parameter.

In precise terms, the setSelectedText() method substitutes the contents of the String parameter for a range of selected text that includes the current caret position. If no text is selected at the caret position, the effect of this operation is simply to insert the new text at that position.

Here's a few alternatives to the full file path that you could use to insert various useful things:

// the file name (without full path)
String newText = buffer.getName();

// today's date
import java.text.DateFormat;

String newText = DateFormat.getDateInstance()
    .format(new Date());

// a line count for the current buffer
String newText = "This file contains "
    + textArea.getLineCount() + " lines.";

Here are brief comments on each: