Marco Web Center

Delphi Developers' Handbook

Chapter 15: Other Delphi Extensions

Copyright Marco Cantu' 1997

External Tools and Transfer Macros

The easiest way to extend the Delphi environment is to add external applications to the Tools menu. There are several services available to external tools that, unfortunately, few Delphi programmers recognize and use.

When you install an external tool (by using the Tools » Configure Tools menu command), you can use several transfer macros to specify the command-line parameters that Delphi will pass to that application, as you can see in Figure 15.1. Although these macros are not terribly powerful by themselves, you can use them in various combinations to automate many operations.

Figure 15.1: Using transfer macros in Delphi
Figure 15.1

Specifically, the transfer macros allow you to supply the following types of command-line parameters to the tool you’re activating:

  • The name of the resulting executable file, provided you’ve already compiled the project (the $EXENAME macro).
  • The name of the active file in the editor (the $EDNAME macro).
  • The current text in the editor, or more precisely, the token closest to the cursor position (the $CURTOKEN macro)
  • A parameter string to be entered by the user (the $PROMPT macro).

Other transfer macros allow you to extract the path, the filename, or the extension of a parameter (generally the editor or project file name, extracted using the above macros), or to save the current project’s files before processing them with an external tool.

As a simple example, we can create a tool that displays the path of the current project. The external program is terribly simple: it displays in a dialog box the value passed as command-line parameter. Below is the complete source code of the project (available in the PrjPath subdirectory on the companion disk):

program PrjPath;
 
uses
  Classes, Dialogs; uses
  Dialogs,
 
begin
  if ParamCount > 0 then
    ShowMessage (ParamStr (1))
  else
    ShowMessage ('No project active');
end.

We’ll pass the path information to this external tool using a combination of two transfer macros. First, we need to create the new external tool. In the Tool Properties dialog box, enter the name of the compiled file in the Program field, and enter the following value for the Parameters field:

$PATH($EXENAME)

The effect of this tool is modest: when you select the new tool from the Tools menu, a message box displays the current project’s program path, as you can see in Figure 15.2.

Figure 15.2: The simple output of the PrjPath example. The path information is made available through Delphi transfer macros.
Figure 15.2

We built this example to emphasize a point about external tools: Programs that receive parameters from Delphi through transfer macros are easy to build, and yet they can be quite powerful. You could, for example, extend the PrjPath program to display a list of the files in the project directory, with detailed information about each source file.

[Chapter 15 Index]