« Moving around existing PageItems Page management »
Selection and Navigation
How to stroll around in the InDesign object model. By Ludwig Zeller
Get the mouse selection
#includepath "~/Documents/;%USERPROFILE%Documents";
#include "basiljs/bundle/basil.js";
function draw() {
var selItems = b.selections();
// only use first item selected
b.typo(selItems[0], "pointSize", 30);
// for-loop
for( var i = 0; i < selItems.length; i++ ){
b.println(selItems);
b.typo(selItems[i], "pointSize", 30);
}
// experts: use a looping callback
b.selections(function(item, i){
b.println(item);
b.typo(item, "pointSize", 30);
});
}
b.go();
- b.selections() returns all the selected PageItems or Text elements as an array of mixed items
- Please note that you will always receive an array, also if just one element is selected
- This a sensible strategy if you haven't created many things and they ended up being distributed all over the page
- Also good for changing certain items only that you select by e.g. aesthetical criteria
Add, get and set a layer
#includepath "~/Documents/;%USERPROFILE%Documents";
#include "basiljs/bundle/basil.js";
function draw() {
// add new layer
var layer = b.layer("myLayer");
b.ellipse(20,20,10,10);
// set active layer
b.layer(layer);
// also possible:
b.layer("myLayer");
// delete the layer... remove() can be used with all PageItems
layer.remove();
}
b.go();
- You can add layers with b.layer()
- You can also activate pre-existing layers
- Putting your new elements on clearly labelled layers is a clean way to keep your project tidy
- You can also use layers to select and alter the new items easily or set them to invisible
Process all objects from a layer, page or document
#includepath "~/Documents/;%USERPROFILE%Documents";
#include "basiljs/bundle/basil.js";
function draw() {
// process all items on a layer
var items = b.items(b.layer("fancy"));
for ( var i = 0; i < items.length; i ++ ) {
b.println(items[i]);
}
// process all items on a page
var items = b.items(b.page(1));
for ( var i = 0; i < items.length; i ++ ) {
b.println(items[i]);
}
// process all items in document
var items = b.items(b.doc());
for ( var i = 0; i < items.length; i ++ ) {
b.println(items[i]);
}
}
b.go();
- b.items() allows you to process everything on a Layer, in a Group, on a Page or in a Document
Attaching labels to created items
#includepath "~/Documents/;%USERPROFILE%Documents";
#include "basiljs/bundle/basil.js";
function draw() {
// the following methods return the PageItem they create
// thus you can set their label directly after their method call
b.ellipse(20,20,5,5).label = "dynamic";
b.ellipse(20,20,5,5).label = "dynamic";
b.text("hihi",20,20,20,20).label = "dynamic";
b.rect(20,20,5,5).label = "dynamic";
b.line(20,20,50,50).label = "dynamic";
// this way, you can access them by calling their name
// please note that labels() always returns an array
var items = b.labels("dynamic");
// you can use forEach() to do something with all of these items
for( var i = 0; i < items.length; i++){
b.println(items[i]);
}
// or you address them individually
b.itemX(items[0], 100);
}
b.go();
- Labels can be attached to PageItems by code or mouse using the Script Label panel in InDesign
- Since labels are a bit hard to find in the GUI they should only be used if layers don't work for your endeavour
Cleaning up and removing stuff
#includepath "~/Documents/;%USERPROFILE%Documents";
#include "basiljs/bundle/basil.js";
function draw() {
// clears the given layer
b.clear(b.layer("fancy"));
// fully delete a layer
b.layer("fancy").remove();
// clears the given page
b.clear(b.page());
// clears the document
b.clear(b.doc());
}
b.go();
Downloads
Navigation.idml
Use this file as a starting point for the code examples above.
