Bringing it together in a script


In the previous two sessions of the DO MORE with Scripting series, Peter Kahrel demonstrated how to communicate with the Typefi Engine and how to use the Typefi scripting Document Object Model (DOM). This episode brings these two things together in a case study.

The challenge featured in the case study is a common one—a publication is being automatically laid out with photographs that appear at the top of pages by default. However, it’s often desirable to override this placement rule to make sure that photographs are placed in their own text section.

In this episode, Peter shows how to achieve this using the techniques learned in the previous episodes, as well as a few new ones.


Good day, and welcome to the fourth webinar on Typefi scripts and scripting. In the previous two sessions we dealt with communication with the Typefi Engine and using the Typefi scripting DOM. In this session we’ll bring these two things together in a case study.

The problem we’ll tackle is a common one. We have a publication with photographs. These photographs should appear at the top of pages by default, but this placement rule is overridden to make sure that they’re placed in their own text section.

On the page you see here that’s not the case—the photograph is at the top of the page but its anchor is in a section that follows the image. The photograph should therefore be moved to the foot of the page.

An InDesign page showing a photo at the top of the page anchored to a paragraph at the bottom.

The first question we should ask ourselves is when the script should run. Since moving an element on a page could cause the text to reflow, the best moment is page.end, not document.end.

So at each page.end we check whether the page contains a photograph. If it does, we check whether the page contains a heading. If it does, we check if the photograph’s anchor comes before or after the heading. If it’s after the heading we know we have to move the photograph.

Typefi anchors (2:11)

A Typefi anchor is actually a small graphic line. It’s zero width and it has no colour so you can’t see it, but it is an object that can be accessed through the DOM. See the video to learn how to find the TypefiElementAnchor property.

Writing the script (3:48)

(function () {
	if (typeof TYPEFI == 'undefined') {
		TYPEFI = {inddPage:[0].activePage}

	function findElement (name) {
		var frames = TYPEFI.inddPage.pageItems.everyItem().getElements();
		for (var i = frames.length-1; i >= 0; i--) {
			if (frames[i] == name && frames[i] !== 'TPSPaginationLayer') {
				return frames[i];
		return null;

	function getHeading (frame) {
		var par = frame.paragraphs.everyItem().getElements();
		for (var i = par.length-1; i >= 0; i--) {
			if (par[i] ('sec') == 0) {
				return par[i];
		return null;

	function main () {
		var mf = TYPEFI.inddPage.typefiSettings.mainStoryFrame();
		if (!mf) return;
		var photo = findElement ('Photo');
		if (!photo) return;
		var heading = getHeading (mf);
		if (!heading) return;
		var anchor = photo.typefiSettings.element.anchor.pageItem.parent;
		if (anchor.baseline > heading.baseline) {
			photo.move (undefined, [0, mf.geometricBounds[2]-photo.geometricBounds[2]]);



Running the script in a Typefi workflow (20:00)

This demonstration shows the workflow running without any scripts, followed by another run with the script included.

< Previous webinarNext webinar >

About Peter Kahrel

Peter was born in Amsterdam, the Netherlands. He trained as a linguist at the University of Amsterdam and has an MA and a PhD, specialising in syntax, semantics, and typology. He has lived in the UK since 1994, working as a typesetter, editor, copy-editor, and indexer (and sometimes as a designer too) for publishers in the UK, the Netherlands, and Germany, preparing for the press both books and journals. He combined this with teaching at the Linguistics Department at Lancaster University.

Peter has been programming InDesign with JavaScript since 2003, mainly to cut out the tedium that faces every typesetter and indexer but in equal measure because it’s good fun. Peter has worked as a script developer at Typefi since 2010, and is highly regarded in the global InDesign scripting community.

Check out Peter’s website—which includes a bunch of free scripts—at CreativePro.