This is the legacy 4D documentation web site. Documentations are progressively being moved to developer.4d.com |
|||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
4D v19
Creating and using macros
|
Element tags | Description |
<macros> </macros> | Start and end of macro file (mandatory tag). |
<macro> </macro> | Start and end of the definition of a macro and its attributes. |
Attributes: | |
- name: Name** of macro as it appears in menus and Method editor lists (mandatory attribute). | |
- type_ahead_text: Character string** to be entered to call the macro using the type-ahead (aka autocomplete) function*. | |
- in_menu: Boolean indicating whether the macro can be called using the context menu*. Values = "true" (default) or "false." | |
- type_ahead: Boolean indicating whether the macro can be called using the type-ahead (aka autocomplete) function*. Values = "true" (default) or "false." | |
- method_event: Used to trigger the automatic calling of the macro depending on the current handling phase of each method (creation, closing, and so on). Values = "on_load": The macro is triggered on the opening of each method, "on_save": The macro is triggered when each method is saved (closing of a modified method or saving using the File>Save command, "on_create": The macro is triggered when each method is created, "on_close": The macro is triggered when each method is closed. | |
"on_save" and "on_close" can be used jointly — in other words, both of these events are generated when a modified method is closed. On the other hand, "on_create" and "on_load" are never generated in a consecutive manner. This attribute can be used, for example, to preformat methods when they are created (comments in header area) or to record information such as the date and time when they are closed. | |
- version: Used to activate the new mode of supporting text selections for the macro (see the “About the <method> Tag” section below). To activate this new mode, pass the value "2". If you omit this attribute or pass version="1", the former mode is kept. | |
- in_toolbar: Boolean indicating if the macro must be present in the menu of the Macro button of the toolbar. Values= "true" (default) or "false". | |
<selection/> | Tag replaced by the selected text when the macro is inserted. The selection may be empty. |
<text> </text> | Start and end of code that must be inserted in the method. A carriage return will be added before and after the code. |
<method> </method> | Start and end of the name of the project method and its (optional) parameter. The method is executed when the macro is called. You can pass a parameter in the form ("param1;param2;..."). This parameter will be received in the method using the variables $1, $2, etc. For additional information about this tag, refer to the “About the <method> Tag” section below. |
<caret/> | Location of the insertion point in the code after the macro has been inserted. |
<user_4D/> | Tag replaced by the name of the current 4D user. |
<user_os/> | Tag replaced by the current system user name. |
<method_name/> | Tag replaced by the current project method name. |
<method_path/> | Tag replaced by full pathname of the current project method. |
<date/> | Tag replaced by the current date. |
Attribute: | |
- format: 4D format used to display the date. If no format is set, the default format is used. Values = number of 4D format (0 to 8). | |
<time/> | Tag replaced by the current time. |
Attribute: | |
- format: 4D format used to display the time. If no format is set, the default format is used. Values = number of 4D format (0 to 6). | |
<clipboard/> | Tag replaced by the contents of the clipboard. |
Attribute: | |
- index: Clipboard to be pasted. Values = number of the clipboard (0 to 9). |
* Macros can be called using the context menu of the Method editor or using the type-ahead function (see the following section).
** If you want to conform to XML language specifications, you must not use extended characters (accented characters, quotation marks, etc.).
Here is an example of a macro definition:
Content of macro | Comments |
<?xml version=”1.0”...?> | XML declaration |
<!DOCTYPE macros SYSTEM> | Document declaration |
<macros> | Start of macros XML file |
<macro name="RecordLoop"> | Start of macro definition and name |
<text> | Start of macro code |
For($i;1;Records in selection(<Selection/>)) | The <Selection/> tag will be replaced by the selected code in the 4D method at the time of macro insertion (for instance, a table name) |
SAVE RECORD(<Selection/>) | |
NEXT RECORD(<Selection/>) | |
End for | |
</text> | End of macro code |
</macro> | End of macro definition |
</macros> | End of macros XML file |
The <method> tag allows you to generate and use macro-commands that execute 4D project methods. This allows developers to create sophisticated functions that can be distributed via macro-commands which are associated with components. For example, the following macro will cause the MyMethod method to be executed with the name of the current method as parameter:
<method>MyMethod("<method_name/>")</method>
The code of a called method is executed in a new process. This process is killed once the method is executed.
Note: The structure process remains frozen until the called method execution is completed. You must make sure that the execution is quick and that there is no risk of it blocking the application. If this occurs, use the Ctrl+F8 (Windows) or Command+F8 (Mac OS) command to “kill” the process.
By default, macros can be called using the context menu or toolbar of the Method editor, the autocomplete function, or a specific list at the bottom of the Method editor window.
Note that for each macro it is possible to restrict the possibility of calling it using the context menu and/or the autocomplete function.
By default, all macros can be called via the context menu of the Method editor (using the Insert macro hierarchical command) or the “Macros” button of the toolbar.
The in_menu attribute of the <macro> tag is used to set whether or not the macro appears in this menu.
In the context menu, macros are displayed in the order of the “Macros.xml” file and any additional XML files. It is thus possible to change the order by modifying these files.
By default, all macros are accessible using the autocomplete (aka type-ahead) function (see Writing a method). The type_ahead attribute of the <macro> tag can be used to exclude a macro from this type of operation.
Note: If the macro contains the <selection/> tag, it will not appear in the autocomplete pop-up window.
You can display your macros in a list of the Method editor (see Writing a method). Simply double-click on the name of a macro in the list in order to call it. It is not possible to exclude a specific macro from this list.
Macro support can change from one version of 4D to another. In order to keep the different versions compatible while maintaining your customizations, 4D does not remove any previous versions. If you want to use the latest features available, you must adapt your version accordingly.
In versions of 4D prior to v11, the program automatically maintained a set of process variables for manipulating text in methods when using the <method> tag: input variables (_textSel, _blobSel, _selLen, _textMethod, _blobMethod, _methodLen) to retrieve text and output variables (_textReplace, _blobReplace, _action) to insert text. For the sake of compatibility, this mechanism is still supported, but since version 11 it is obsolete for the following reasons:
Consequently, it is recommended to manage text selections using the GET MACRO PARAMETER and SET MACRO PARAMETER commands. These commands can be used to overcome the partitioning of the host database/component execution spaces and thus allow the creation of components dedicated to the management of macros. In order to activate this mode for a macro, you must declare the Version attribute with the value 2 in the Macro element. In this case, 4D no longer manages the predefined variables _textSel, _textReplace, etc. and the GET MACRO PARAMETER and SET MACRO PARAMETER are used. This attribute must be declared as follows:
<macro name="MyMacro" version="2">
--- Text of the macro ---
</macro>
If you do not pass this attribute, the previous mode is kept.
Beginning with 4D v11, strict syntax rules must be observed in order for macros files to respect the XML standard. This may lead to incompatibilities with the code of macros created with previous versions and prevent the loading of XML files. The following are the main sources of malfunctioning:
In version 12 of 4D, there are new macro commands available to facilitate the use of the SQL commands. Since you can customize the "Macros.xml" file, installing a new version of 4D does not automatically replace the existing version of the file. To use the new SQL macro commands of 4D v12, you must either:
Product: 4D
Theme: Editing methods
4D Design Reference ( 4D v19)