As each new version is released, we take into account the feedback we receive from our customers on the general use of 4D Write Pro and its commands. While remaining 100% compatible, some commands and language functions are evolving!
Some commands, whose syntax have been improved in 4D 20 R8, now allow the use of modern parameters such as objects or collections. Others are now functions, making them easier to use. Finally, a number of new functions have been created. All this will make your code simpler, more generic and easier to maintain!
WP GET/SET ATTRIBUTES: All attributes in a single object!
One of the most popular commands is WP SET ATTRIBUTES, because it can be used for all kinds of targets, from a single character to the entire document. This command can now receive an object as a parameter, rather than attribute-value pairs.
Code sample
$attributes:={color: "red"; fontBold: 0; fontItalic: 1;
fontSize: "16pt"}
WP SET ATTRIBUTES($range1; $attributes)
WP SET ATTRIBUTES($range2; $attributes)
WP SET ATTRIBUTES($range3; $attributes)
In the same way, it’s possible to receive in an object all or part of the attributes of any range or element. This is very useful if you wish to apply the same attributes to another target… If you pass only one parameter, the returned object will contain all the attributes, but you can limit this by using a collection!
$attributes:=WP Get attributes($range; ["color"; "fontItalic"])
Finally the WP RESET ATTRIBUTES command now accept a collection instead of multiple parameters (of course the previous syntax is still valid)
$attributes:=["color"; "fontBold"; "fontItalic"; "fontSize"]
WP RESET ATTRIBUTES($range1; $attributes)
WP RESET ATTRIBUTES($range2; $attributes)
WP RESET ATTRIBUTES($range3; $attributes)
WP TABLE APPEND ROW: formulas, collections and function returns!
The parameters of this command can now be of the formula type or a named formula (Reminder : a named formula is an object containing both the formula and the formula name… see this BLOG).
Then, parameters can be passed to the command as a collection and best of all, the command now returns a row element!
$row:=WP Table append row($table; "Reference"; "Date";
"Time"; "rnd 1"; "rdn 2")
WP SET ATTRIBUTES($row; wk background color; "lightgrey")
$colItems:=[]
$colItems.push("KX-825")
$colItems.push(Formula(Current date))
$colItems.push(Formula(String(Current time; HH MM SS)))
$colItems.push(Formula(Random))
$colItems.push({name: "RND NUMBER"; formula: Formula(Random)})
$row:=WP Table append row($table; $colItems)
WP INSERT PICTURE
When inserting an element into a document, it’s generally useful to have immediate access to this element. The WP INSERT PICTURE command therefore becomes a WP Insert picture function and returns a picture element. For example, you may want to frame an image immediately after inserting it. In addition, this command can now accept a 4D.File as an argument!
$pictureFile:=File(Folder(fk resources folder).path+"bullet.png";
fk posix path)
$picture:=WP Insert picture(WParea; $pictureFile; wk append)
$attributes:={}
$attributes[wk margin]:="8pt"
$attributes[wk padding]:="5pt"
$attributes[wk border width]:="2pt"
$attributes[wk border style]:=wk solid
$attributes[wk border color]:="navy"
WP SET ATTRIBUTES($picture; $attributes)
DOCUMENTS and PICTURES
The WP IMPORT DOCUMENT, WP EXPORT DOCUMENT, WP INSERT PICTURE and WP Add picture commands have also been modernized, and now accept 4D.File arguments in addition to the usual argument types.
$documentFile:=File("/RESOURCES/reference.4wp")
WParea:=WP Import document($documentFile)
$documentFile:=File("/DATA/CopyOfReference.4wp")
WP EXPORT DOCUMENT(WParea; $documentFile)
$pictureFile:=File("/RESOURCES/bullet.png")
$picture3:=WP Insert picture(WParea; $pictureFile; wk append)
$pictureFile:=File("/RESOURCES/bullet.png")
$picture:=WP Add picture(WParea; $pictureFile)
“INSERT DOCUMENT BODY” INSTEAD OF “INSERT DOCUMENT”
For greater clarity, the WP INSERT DOCUMENT command has been renamed WP Insert document body, as this is in fact what it does.
When it is called, the body of the document is inserted. Headers, footers, anchored images and text boxes are ignored. The only exception: style sheets are also imported!
As you can see in the example below, this command now returns a range corresponding to the imported body!
$file:=File("/RESOURCES/Main.4wp")
$main:=WP Import document($file)
$pictureFile:=File("/RESOURCES/bullet.png")
$sub:=WP Import document($pictureFile)
$range:=WP Insert document body($main; $sub; wk append)
MORE RANGE INFO
Some commands were silent and returned… nothing, when often a range would have been quite useful. The following commands do just that, and all return text ranges!
- WP INSERT FORMULA
- WP INSERT BREAK
- WP INSERT DOCUMENT BODY (formerly WP INSERT DOCUMENT)
$range:=WP Insert formula(WParea; Formula(Current date); wk append)
WP SET ATTRIBUTES($range; {color: "red"; fontItalic: 1; fontBold: 0})
WELCOME NEW FEATURES
Sections
Sections both have names and indexes (these are attributes), but accessing this information was not easy.
It’s now child’s play with the new This.sectionIndex and This.sectionName functions, which can be inserted as formulas directly into your documents!
Page indexes
If required, you can now access the page index with the This.pageIndex function.
Here’s a quick explanation: unlike the This.pageNumber function, whose value can vary (it be set or reset per section), the page index always starts at 1 and depends exclusively on pagination.
For example, a document may contain three sections whose page numbers systematically start at 1. The preamble part runs from 1 to 30, the main part from 1 to 200 and the appendix part from 1 to 8. In this case, the page index will run from 1 to 238.
Conclusion
There’s no doubt that the simplified commands will make life easier for 4D Write Pro developers, and simplify code review and maintenance. We look forward to your feedback on the forum!