Skip to content

Instantly share code, notes, and snippets.

@getflourish
Created August 10, 2014 20:23
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save getflourish/f5798c511c2fbdf61e22 to your computer and use it in GitHub Desktop.
Save getflourish/f5798c511c2fbdf61e22 to your computer and use it in GitHub Desktop.
// Selects all text layers that have the same text style as the reference text layer (command control t)
// init
var layerCount = 0;
var pageCount = 0;
var lastFoundOnPage = -1;
var selectedLayers = [];
var i = 0;
// We can only search for similar text layers when a reference layer is selected
if (selection.count() == 1) {
[doc showMessage: "Looking for similar text layers…"]
// Remember the reference text style
var reference = selection[0].style().textStyle().attributes();
// Loop through pages
var pages = doc.pages().objectEnumerator();
while (page = pages.nextObject()) {
i++;
// Loop through all children of the page
var layers = page.children().objectEnumerator();
while (layer = layers.nextObject()) {
// Check if the layer is a text layer
if ([layer isKindOfClass:MSTextLayer]) {
if(layer.style().textStyle().attributes() == reference) {
[layer select:true byExpandingSelection:true];
layerCount++;
if (i != lastFoundOnPage) {
pageCount++;
lastFoundOnPage = i;
}
}
}
}
}
// Show how many layers have been selected
[doc showMessage: layerCount + " layers on " + pageCount + " pages selected."]
} else {
[doc showMessage: "Please select a single reference text layer."]
}
@uhunkler
Copy link

// Selects all text layers that have the same text style as the reference text layer (command control t)

// init

var pageCount = 0,
  layerCount = 0,
  textLayerCount = 0,
  selectedLayerCount = 0,
  lastFoundOnPage = -1,
  selectedLayers = [],
  i = 0,
  reference = null,
  pages,
  page,
  layers,
  layer;

// We can only search for similar text layers when a reference layer is selected

if (selection.count() === 1) {

  doc.showMessage("Looking for similar text layers…");

  // Remember the reference text style
  reference = selection[0].style().textStyle().attributes();

  // Loop through pages
  pages = doc.pages().objectEnumerator();

  while (page = pages.nextObject()) {
    i++;
    pageCount++;

    // Loop through all children of the page
    layers = page.children().objectEnumerator();

    while (layer = layers.nextObject()) {
      layerCount++;

      // Check if the layer is a text layer
      if (layer.isKindOfClass(MSTextLayer)) {
        textLayerCount++;

        if (layer.style().textStyle().attributes() === reference) {
          layer.setIsSelected(true);
          selectedLayerCount++;

          if (i !== lastFoundOnPage) {
            lastFoundOnPage = i;
          }
        }
      }
    }
  }

  // Show how many layers have been selected
  doc.showMessage(selectedLayerCount + " text layers selected within " +
    textLayerCount + " exisiting text layers within " +
    layerCount + " layers on " +
    pageCount + " pages.");
} else {
  doc.showMessage("Please select a single reference text layer.");
}

What I did:

  • I rewrote all Cocoa code into the JavaScript equivalent
  • I moved all variable definitions into the »init« section
  • I changed week equality to strong equality (!= and == to !== and ===)
  • I added semicolons to all lines where needed
  • I added counters at more places to see if the script touches all layers
  • I changed the layer selection code to layer.setIsSelected(true);

Only the last change is relevant, the other changes may be lint optimizations.

For me this changed script selects the layers as expected without crashes.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment