4D Write Pro comes with new commands that allow a developer to programmatically manipulate the Write Pro document. Some of the most useful commands to manipulate the document with code are WP Get Paragraphs and WP Create Range to make a selection of what you want to modify, along with the command WP SET ATTRIBUTES to make the modifications. There are some subtleties to keep in mind when using these commands...
The WP Create Range command takes in three inputs which are a Write Pro area, the start of the range, and the end of the range, and returns a range object. This range object can contain multiple paragraphs, the entire document, or be a subsection of a paragraph such as the first sentence of a paragraph.
The WP Get Paragraphs command takes in only one input, a range object, and can be used in conjunction with the WP Create Range command but returns only paragraphs as range objects.
Consider the sample WP Area called $wpArea below:
Use the WP Create Range command to select the last sentence of the first paragraph and the first sentence of the second paragraph, and create a range object $rangeObj_A, then use the first range object to create a second range object $rangeObj_B with the WP Get Paragrahs command:
C_OBJECT($rangeObj_A;$rangeObj_B) $rangeObj_A:=WP Create Range($wpArea;243;355) $rangeObj_B:=WP Get Paragraphs($rangeObj_A) |
Now $rangeObj_A contains the characters of the last sentence of the first paragraph and first sentence of the second paragraph, from the 243rd to 355th characters of the Write Pro area, and the $rangeObj_B object contains the two paragraphs which $rangeObj_A spans, in other words the entire WP Area above.
Using the WP SET ATTRIBUTES command will allow for manipulations of these ranges, for example bolding the text. Using this command on the range will modify the origional paragraph in the WP Area. If the WP SET ATTRIBUTES command is used with the first range object, $rangeObj_A, the resultant WP Area will be:
WP SET ATTRIBUTES($rangeObj_A;wk font bold;wk true) |
If the WP SET ATTRIBUTES command is used with the second range object, $rangeObj_B, the resultant WP Area will be:
WP SET ATTRIBUTES($rangeObj_B;wk font bold;wk true) |
It is important to consider the range area you are working with along with the context of the attribute you are updating. For example setting a different attribute, such as the border color with wk border color, either $rangeObj_A or $rangeObj_B produce the same result even though the first range object is just two sentences and the second two paragraphs.
WP SET ATTRIBUTES($rangeObj_A;wk border color;"red") |
WP SET ATTRIBUTES($rangeObj_B;wk border color;"red") |
Both will manipulate the WP Area to be:
Why is this the case? Looking at the documentation for 4D Write Pro Attributes will reveal the answer.
Setting the wk border color attribute will act on all the 4D Write object types except for characters, so $rangeObj_A and $rangeObj_B are both considered for the paragraphs they span when applying an border color attribute, not as individual characters, where setting the wk font bold can be applied to characters.