Skip to content

Instantly share code, notes, and snippets.

@grefel
Last active August 29, 2015 14:07
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save grefel/55afd90a8e6a5ccd1a7b to your computer and use it in GitHub Desktop.
Save grefel/55afd90a8e6a5ccd1a7b to your computer and use it in GitHub Desktop.
Get #InDesign Style by String
var root = app.activeDocument;
var result = getStyleByString(root, "Formatgruppe 2:Form\\:atgruppe 1:Zeichen\\:\\:format 1", "characterStyles", true);
$.writeln(result.name);
function getStyleByString(root, string, property, recreate) {
if (recreate == undefined) recreate = false;
stringResult = string.match (/^(.*?[^\\]):(.*)$/);
var cStyleName = (stringResult) ? stringResult[1] : string;
cStyleName = cStyleName.replace(/\\:/g, ":");
remainingString = (stringResult) ? stringResult[2] : "";
var newProperty = (stringResult) ? property.replace(/s$/, '') + "Groups" : property;
var cStyle = root[newProperty].itemByName(cStyleName);
if (!cStyle.isValid && recreate) cStyle = root[newProperty].add({name:cStyleName});
if (remainingString.length > 0 && cStyle.isValid) cStyle = getStyleByString (cStyle, remainingString, property, recreate);
return cStyle;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment