about

download

reference

tutorials

support

gallery






-

@basil_js
« »

Hello World!

The unavoidable hello world example. By Ludwig Zeller

Open up the Adobe ExtendScript Editor, which is the script editor that comes with the Adobe Creative Suite. We will use this editor for our tutorials, but basically you could use any JavaScript editor.

Create a new file and save it to helloworld.jsx within your user folder. Copy and paste the following code into your newly created file.

Simple Hello World example


#includepath "~/Documents/;%USERPROFILE%Documents";
#include "basiljs/bundle/basil.js";

function draw() {

  b.println( "Hello World!" );

}

b.go();

The include statements found in this examples always have to be at the very beginning of each basil.js file. They will load the actual library from your basil.js installation.

Afterwards you will find two function blocks. setup() is pretty much the same as in Processing. It's the first user function that is called once right after your code is started. You should use this to initialise variables, etc. Afterwards the draw() function is executed. But unlike in Processing it is only called once, since basil.js is not interactive by default (There is an interactive mode available for advanced use though). At the end of the file you find the statement b.go(), which will trigger the execution of your code. Just like the include statements this is also mandatory.

Now run the script... either by pressing the play button from within the ExtendScript editor or by double-clicking your script from the InDesign scripts panel. You will be able to read an output in the console of the ExtendScript editor saying "Hello World!".

No wonder, the statement

b.println( "Hello World!" );

is exactly meant for that. Please note, that each basil function has to be called with a b. in front of it. For experienced users: basil.js is packaged away in a global object called b in order to avoid namespace collissions. In future releases we might be able to get rid of this.

Printing to the console is nice, but actually far from what basil.js is designed for. We should output our Hello World message to an InDesign textbox!

Hello World example using a textbox

#includepath "~/Documents/;%USERPROFILE%Documents";
#include "basiljs/bundle/basil.js";

function draw() {

  b.doc();
  b.text("Hello World", 100, 100, 200, 50);

}

b.go();

This is exactly what the example above is doing. Paste it over your existing code and run it by pressing play or double-clicking it from the scripts panel after saving. A text frame object will be added to your existing document - or if none was existing, a new document will be created - and filled with the Hello World message.

The b.doc() call makes sure that at least one document is open and the call to b.text("Hello World", 100, 100, 200, 50); will create a textframe with the default basil.js type settings (more on this in the typo tutorial), add it to the current layer and page, move it to 100, 100 (using the basil.js' default units), size it to 200, 50 and fill it with "Hello World!"