Functions for InDesign
Miscellaneous useful functions and titbits of code sorted by alphabetized categories.
Cross reference
Backup
Backup before save by Marc Autret
Book
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.
DPS article
Excel
Font
Geometry: bounds, width, height, etc.
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]; }
Sort objects by geometric bounds function
Glyph
FontGlyphCount by Marc Autret — Fast Glyph Counter
Group
Group Add Items by Marc Autret — Easily add PageItems to a Group
Image
Get orientation
function GetOrientation(img) { var result = null; var gb = img.geometricBounds; var width = Math.floor((gb[3]-gb[1])*100)/100; var height = Math.floor((gb[2]-gb[0])*100)/100; if (height > width) { result = "P"; } else if (width > height) { result = "L"; } return result; }
Increment
Label
Layer
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; }
Link
Log
Menu item
Override
Override all pageItems function
Page
Report
Script
Get active script
function GetActiveScript() {
try{
return app.activeScript;
}
catch(err){
return new File(err.fileName);
}
}
Selection
Get selected story
function GetSelectedStory() { if (app.selection.length != 1) return null; var sel = app.selection[0]; if (sel.parent instanceof Character) { // anchored box is selected -- get the main text flow selectedStory = sel.parent.parentStory; } else if (sel.hasOwnProperty("baseline") || sel.constructor.name == "TextFrame") { selectedStory = sel.parentStory; if (sel.constructor.name != "InsertionPoint") { selectedText = sel; } } }
Get selected items that have the geometricBounds property
function GetSelection() { var selection = app.selection, selectedItems = []; for (var i = 0; i < selection.length; i++) { if (selection[i].hasOwnProperty("geometricBounds")) { selectedItems.push(selection[i]); } } return selectedItems; }
Sorting
Text Sort Paragraphs by Marc Autret — Quick paragraph sorter
Sorting two arrays according to one (parallel arrays)
Sort file names function for Mac
Sort objects by geometric bounds
Special characters
Style
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 (typeof 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; }
Get style by name
function GetStyleByName(name, namesList, styles) { var result = null; for (var i = 0; i < namesList.length; i++) { if (namesList[i] == name) { result = styles[i]; } } return result; }
Swatch (Color)
Tables of Contents
Batch-update Tables of Contents by Marc Autret
Text
Get overset text
var myDoc = app.activeDocument; var a = myDoc.stories[0].tables[0].cells[0, 0]; // var a = myDoc.stories[0]; 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 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);
Uncategorized
Override all pageItems function
Redefine scaling as 100 function by Marc Autret
Remove floppy symbols
Removes floppy drive symbols from imported paragraph and character styles, keeping properties intact.
Unit value
User
Get user name
function getUserName() { if (File.fs == "Windows") { return $.getenv("USERNAME"); } else { return $.getenv("USER"); } }
Version
Get application version function
XML
Zoom
function ShowIt(theObj) { if (arguments.length > 0) { // Select object, turn to page and center it in the window app.select(theObj, SelectionOptions.replaceWith); } // Note: if no object is passed and there is no selection the current page // will be centered in the window at whatever zoom is being used var zoom = app.activeWindow.zoomPercentage; app.activeWindow.zoom(ZoomOptions.showPasteboard); app.activeWindow.zoomPercentage = zoom; }