The itemByPath() Collection Functions

This script adds itemByPath() functions to the styles collections, for use by other scripts. The itemByPath() functions work similar to itemByName(), except that the ":" colon is interpreted as path separator, the method descends the style groups accordingly.

The function assumes there is at least one style in its collection, which is always given for the root style collections of the application or document. Style collections held by style groups may be empty, in this case the script will fail.

Installation

The script can be installed as Startup Script or via #include .

Usage

// DESCRIPTION: Demonstrate itemByPath() functions #target "InDesign" #include "/www.ixta.com/scripts/style/itemByPath.jsx" var doc = app.documents.add(); doc.characterStyleGroups.add({name:"My Style Group"}).characterStyles.add({name:"My Character Style"}); var cstyle = doc.characterStyles.itemByPath("My Style Group:My Character Style"); alert(cstyle.name); // var pstyle = doc.paragraphStyles.itemByPath("Style Group 1:Paragraph Style 1"); // var ostyle = doc.objectStyles.itemByPath("Style Group 1:Object Style 1");

The Script

// DESCRIPTION: Install itemByPath() functions for use by other scripts. // http://www.ixta.com/scripts/style/itemByPath.jsx // // Copyright ©2009 Dirk Becker, http://www.ixta.de // This software is provided "as is", with absolutely no warranty. // Permission to install and use the software is hereby granted without fee. // Redistribution by any means is expressly prohibited. app.paragraphStyles; ParagraphStyles.prototype.itemByPath = function (aPath) { var path = aPath.split(":"); var parent = this.item(0).parent; while( path.length>1 ) parent = parent.paragraphStyleGroups.itemByName(path.shift()); return parent.paragraphStyles.itemByName(path[0]); } app.characterStyles; CharacterStyles.prototype.itemByPath = function (aPath) { var path = aPath.split(":"); var parent = this.item(0).parent; while( path.length>1 ) parent = parent.characterStyleGroups.itemByName(path.shift()); return parent.characterStyles.itemByName(path[0]); } app.objectStyles; ObjectStyles.prototype.itemByPath = function (aPath) { var path = aPath.split(":"); var parent = this.item(0).parent; while( path.length>1 ) parent = parent.objectStyleGroups.itemByName(path.shift()); return parent.objectStyles.itemByName(path[0]); } app.tableStyles; TableStyles.prototype.itemByPath = function (aPath) { var path = aPath.split(":"); var parent = this.item(0).parent; while( path.length>1 ) parent = parent.tableStyleGroups.itemByName(path.shift()); return parent.tableStyles.itemByName(path[0]); } app.cellStyles; CellStyles.prototype.itemByPath = function (aPath) { var path = aPath.split(":"); var parent = this.item(0).parent; while( path.length>1 ) parent = parent.cellStyleGroups.itemByName(path.shift()); return parent.cellStyles.itemByName(path[0]); }