Skip to content

Instantly share code, notes, and snippets.

@sonburn
Last active February 1, 2019 07:12
Show Gist options
  • Select an option

  • Save sonburn/f7b8388817894a4787dd248a26bf91b4 to your computer and use it in GitHub Desktop.

Select an option

Save sonburn/f7b8388817894a4787dd248a26bf91b4 to your computer and use it in GitHub Desktop.
function findStringInTextLayers(textToFind,exactMatch,currentPage) {
var predicate = NSPredicate.predicateWithFormat("className == %@","MSTextLayer"),
textLayers = NSMutableArray.array();
if (currentPage == 1) {
var page = context.document.currentPage();
textLayers.addObjectsFromArray(page.children().filteredArrayUsingPredicate(predicate));
} else {
var pages = context.document.pages(),
pageLoop = pages.objectEnumerator(),
page;
while (page = pageLoop.nextObject()) {
textLayers.addObjectsFromArray(page.children().filteredArrayUsingPredicate(predicate));
}
}
var textLayerLoop = textLayers.objectEnumerator(),
textLayer,
foundMatch = 0;
while (textLayer = textLayerLoop.nextObject()) {
if (exactMatch == 1 && textLayer.stringValue() == textToFind || exactMatch == 0 && textLayer.stringValue().indexOf(textToFind) != -1) {
log(textLayer.objectID());
log(textLayer.stringValue());
log("...");
foundMatch++;
}
};
log("Found " + foundMatch + " matches for \"" + textToFind + "\"");
}
// text to find, exact match (1) or fuzzy match (0), current page (1) or all pages (0)
findStringInTextLayers("9.",0,0);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment