Four different ways to use objects with 4D Write Pro!

Automatically translated from English

Quite frankly, we love objects. 4D Write Pro uses objects as the input/output of commands and functions to manipulate documents, text, paragraphs, etc. In this blog post, we’ll show you four different ways to use objects with 4D Write Pro.

Setting attributes

Let’s use a simple example. You want to set the font, size and weight of the selected text to “Arial”, “Bold” and “16pt”.

First, capture the selected text as a range.

$range:=WP Get selection(WParea)

Reminder: WP Get selection does not return text, it returns the range matching the selected text. This range has several attributes that are visible in the debugger: “start”, “end”, “type”, and “owner”.

Then, define the attributes with the WP SET ATTRIBUTES command:

WP SET ATTRIBUTES($range;wk font family;"Arial")
WP SET ATTRIBUTES($range;wk font size;16)
WP SET ATTRIBUTES($range;wk font bold;wk true)

This code is easy to read and write thanks to constants and ,in some cases, smart constants (like wk true on the third line).

Alternative 1: OB SET

As long as $range is an object and wk font xxx is an attribute, you could consider using the OB SET command to do the same thing.

And you know what? It works great! The code below does the exact same thing:

OB SET($range;wk font family;"Arial")
OB SET($range;wk font size;16)
OB SET($range;wk font bold;wk true)

Even the same constants can be used. The only difference is that it’s no longer obvious that this code applies to a 4D Write Pro document. Hmmmmm. Let’s take a look at the second alternative.

Alternative 2: Object notation

If you have activated Object Notation in your databases, you’d like to use it as much as possible, right?

Will this code work, too?

$range.fontBold:=wk true

Yes, it does! And it comes with a couple of additional perks: compacted code, easy to read, and the result is – of course – exactly the same.

A small note of caution: Written like this, there is no auto-completion, and constants/smart constants require extra care because they are case sensitive (e.g. fontFamily will work but FontFamily will not). So if you write this kind of code, we suggest being very careful 😉

Is there another choice? Yes there is!

Alternative 3: Object notation with brackets

Object Notation is really powerful. The name of attributes can be evaluated if they are within square brackets [“myAttribute”]. So when you write:


it’s the exact equivalent of writing:"Brown"

What does it mean for 4D Write Pro? The short answer is, you can use Object Notation AND constants!

$range[wk font family]:="Arial"
$range[wk font size]:=16
$range[wk font bold]:=wk true

This final alternative is compact, easy to read/write, and it uses constants…an all-around winner!!

What about Reading attributes ?

The same goes for reading attributes. The following code examples are valid.

WP GET ATTRIBUTES($range;wk font family;$family)
WP GET ATTRIBUTES($range;wk font size;$size)
WP GET ATTRIBUTES($range;wk font bold;$bold)

Alternative 1: OB GET

$family:=OB Get($range;wk font family)
$size:=OB Get($range;wk font size)
$bold:=OB Get($range;wk font bold)

Alternative 2: Object notation


Alternative 3: Object notation with brackets

$family:=$range[wk font family]
$size:=$range[wk font size]
$bold:=$range[wk font bold]

As you can see, multiple ways of programming can lead to the same result. It’s now up to you to decide which direction to take!

Roland Lannuzel

• Product Owner & 4D Expert •

After studying electronics, Roland went into industrial IT as a developer and consultant, building solutions for customers with a variety of databases and technologies. In the late 80’s he fell in love with 4D and has used it in writing business applications that include accounting, billing and email systems.

Eventually joining the company in 1997, Roland’s valuable contributions include designing specifications, testing tools, demos as well as training and speaking to the 4D community at many conferences. He continues to actively shape the future of 4D by defining new features and database development tools.