Site logo

Functions for InDesign

Remove floppy symbols
Removes floppy drive symbols from imported paragraph and character styles, keeping properties intact.

Get book and pick book functions by Peter Kahrel
There is a property app.activeBook, but it doesn't work very well. Use these functions for that. They display a list of books from which you pick one.

Get bounds

function myGetBounds(myDocument, myPage){
	var myPageWidth = myDocument.documentPreferences.pageWidth;
	var myPageHeight = myDocument.documentPreferences.pageHeight
	if(myPage.side == PageSideOptions.leftHand){
		var myX2 = myPage.marginPreferences.left;
		var myX1 = myPage.marginPreferences.right;
	}
	else{
		var myX1 = myPage.marginPreferences.left;
		var myX2 = myPage.marginPreferences.right;
	}
	var myY1 = myPage.marginPreferences.top;
	var myX2 = myPageWidth - myX2;
	var myY2 = myPageHeight - myPage.marginPreferences.bottom;
	return [myY1, myX1, myY2, myX2];
}

Get height by Dave Saunders

This function returns the height if the object in question has one.

function getHeight(theObj) { 
  if (theObj.hasOwnProperty("geometricBounds")) { 
    var theBounds = theObj.geometricBounds; 
    return(theBounds[2] - theBounds[0]); 
  } else if (theObj.hasOwnProperty("height")) { 
    return theObj.height; 
  } else { 
    throw "Object doesn't have a height." 
  } 
}

Get width and height function

Get width and height of page item

function WidthAndHeight(myRectangle) {
   var gb = myRectangle.geometricBounds;
   var myWidth = Math.floor((gb[3]-gb[1])*100)/100;
   var myHeight = Math.floor((gb[2]-gb[0])*100)/100;
   return [myWidth,myHeight];
}

Get overset text

var myDoc = app.activeDocument;
var a = myDoc.stories[0].tables[0].cells[0, 0];
// var a = myDoc.stories[0];
var b = getOversetText(a);


function getOversetText(textFlow) { 
	if (!textFlow.overflows) return null;
	var start = textFlow.characters[0];
	if (textFlow instanceof Cell) {
		if (textFlow.characters.length > 0) {
			start = textFlow.texts[0].characters[textFlow.contents.length];
		} 
	} else {   
		if (Number(String(app.version.split(".")[0])) > 4) {
					var myTFs = textFlow.textContainers;   
				} else {
					var myTFs = textFlow.textFrames;
				}
			for (var j = myTFs.length - 1; j >= 0; j--) {    
				if (myTFs[j].characters.length > 0) {       
					start = textFlow.characters[myTFs[j].characters[-1].index + 1];
					break;     
				}
			}
		} 
	return textFlow.texts.itemByRange(start, textFlow.texts[0].characters[-1]);
}

Get page number by Dave Saunders

var myDoc = app.activeDocument;
var myObj = app.selection[0];
$.write("The selected object is on page " + findPage(myObj).name + "\n") ;
 
function findPage(theObj) { 
    var thePage = theObj; 
    if (thePage.hasOwnProperty("baseline")) { 
      thePage = thePage.parentTextFrames[0]; 
    } 
    while (thePage.constructor.name != "Page") { 
      var whatIsIt = thePage.constructor.name; 
      switch (whatIsIt) { 
        case "Story" : 
          thePage = thePage.textFrames[-1].parent; 
          break; 
        case  "Character" : 
          thePage = thePage.parentTextFrames[0]; 
          break; 
        case "Cell" : 
          try { 
            thePage = thePage.insertionPoints[0].parentTextFrames[0]; 
            break; 
          } catch (e) { 
            // must be overset, so ignore 
            return null; 
          } 
        case "Application" : 
          // must be off page, so ignore 
          return null; 
      } 
      thePage = thePage.parent; 
    } 
    return thePage 
  }

Get page number by Marc Autret

TextFrame.prototype.getPage = function()
// -----------------------------------------------
// Returns the containing Page of this TextFrame
{
var p = this.parent,
     pc;
while( pc=p.constructor )
     {
     if( pc == Page )
          return p;
     if( pc == Spread || pc == MasterSpread )
          throw Error("The textframe is placed on a spread!");
     if( 'parentTextFrames' in p && !(p=p.parentTextFrames[0]) )
          throw Error("The textframe's container overflows!");
     p=p.parent;
     }
}
 
// Sample code (assuming a text frame is selected)
// -----------------------------------------------
var tf = app.selection[0],
     pg;
     
try {
     pg = tf.getPage();
     alert( "Page: " + pg.name + " Side: " + pg.side);
     }
catch(e)
     {
     alert( e );
     }
    

Get item from collection

function for CS5-5.5 to get labeled items

function GetItemFromCollection(label, collection) {
	for (var i = 0; i < collection.length; i++) {
		if (label == collection[i].label) return collection[i];
	}
	return null;
}

Find labels

var pageItems = FindLabels(scope, "My Label");

function FindLabels(there, label) {
	var array = [];
	var pageItems = there.allPageItems;
	for (var i = 0; i < pageItems.length; i++) {
		if (pageItems[i].label == label) array.push(pageItems[i]);
	}
	return array;
}
Document.prototype.manyPageItemsByLabel = function(label) {	// Returns an array
	var pis = this.pageItems.everyItem().getElements(); 
	var wanted = new Array(); 
	for (var n = 0; n < pis.length; n++) { 
		if (pis[n].label == label) wanted.push( pis[n] ); 
	}  
	return wanted 
}

var them = doc.manyPageItemsByLabel("circle");

Get Previous paragraph by Harbs

function getPreviousPara(text){
return text.parent.characters.item(text.paragraphs[0].index-1).paragraphs[0];
}

alert(getPreviousPara(app.selection[0]).contents);

Make layer

var layer1 = MakeLayer("Text");
var layer2 = MakeLayer("Graphics", [0, 0, 255]);
var layer3 = MakeLayer("Quotes", UIColors.CUTE_TEAL);

function MakeLayer(name, layerColor) {
	var layer = doc.layers.item(name);
	if (!layer.isValid) {
		layer = doc.layers.add({name: name});
		if (layerColor != undefined) layer.layerColor = layerColor;
	}
	return layer;
}

Make object style

var sel = app.selection[0];

var objStyle = MakeObjStyle("Pop-up", {
	enableParagraphStyle: true,
	appliedParagraphStyle: "Pop-up",
	fillColor: doc.swatches.item("Black"), 
	fillTint: 15, 
	strokeWeight: 0, 
	enableTextFrameGeneralOptions: true,
	textFramePreferences: {insetSpacing: [1, 1, 1, 1]},
	objectEffectsEnablingSettings: {enableDropShadow: true},
	transparencySettings: {dropShadowSettings: {distance: 1.0, opacity: 70, size: 1, mode: ShadowMode.DROP}}
	});

sel.appliedObjectStyle = objStyle;


function MakeObjStyle(name, properties) {
	var objStyle = doc.objectStyles.item(name);
	if (!objStyle.isValid) {
		objStyle = doc.objectStyles.add({name: name});
		if (properties != undefined) objStyle.properties = properties;
	}
	return objStyle;
}

Make paragraph style

var doc = app.activeDocument;
MakeParStyle("Pop-up", {appliedFont: "Arial", fontStyle: "Regular"});

function MakeParStyle(name, properties) {
	var parStyle = doc.paragraphStyles.item(name);
	if (!parStyle.isValid) {
		parStyle = doc.paragraphStyles.add({name: name});
		if (properties != undefined) parStyle.properties = properties;
	}
	return parStyle;
}

Make character style

var doc = app.activeDocument;
var style = MakeCharStyle("Pop-up", {appliedFont: "Arial", fontStyle: "Regular"});

function MakeCharStyle(name, properties) {
	var charStyle = doc.characterStyles.item(name);
	if (!charStyle.isValid) {
		charStyle = doc.characterStyles.add({name: name});
		if (properties != undefined) charStyle.properties = properties;
	}
	return charStyle;
}

Make color

MakeColor("Red", ColorSpace.RGB, ColorModel.process, [255, 0, 0]);

function MakeColor(colorName, colorSpace, colorModel, colorValue) {
	var doc = app.activeDocument;
	var color = doc.colors.item(colorName);
	if (!color.isValid) {
		color = doc.colors.add({name: colorName, space: colorSpace, model: colorModel, colorValue: colorValue});
	}
	return color;
}

Get active script

function GetActiveScript() {
try{
return app.activeScript;
}
catch(err){
return new File(err.fileName);
}
}