Skip to main content
Known Participant
March 27, 2023
Answered

Does illustrator have such a script?

  • March 27, 2023
  • 2 replies
  • 2087 views

Illustrator runs the script and pops up the input box. For example, if the input text is 3446, then the text 3446 will be automatically selected or grouped. Does anyone know how to write the code? Thank you very much for your help!

This topic has been closed for replies.
Correct answer Inventsable

Alternative approach to the other answers, in that it:

 

  1. Searches for the matching text then recurses parent instead of looping through the entirety of document groups (so it matches a much smaller collection then looks up instead of looping large collections and matching down)
  2. Uses a single regular expression to match text instead of splitting and searching for each value

 

Array.prototype.filter = function (callback) {
  var filtered = [];
  for (var i = 0; i < this.length; i++)
    if (callback(this[i], i, this)) filtered.push(this[i]);
  return filtered;
};
Array.prototype.forEach = function (callback) {
  for (var i = 0; i < this.length; i++) callback(this[i], i, this);
};
Array.prototype.map = function (callback) {
  var mappedParam = [];
  for (var i = 0; i < this.length; i++)
    mappedParam.push(callback(this[i], i, this));
  return mappedParam;
};
function get(type, parent, deep) {
  if (arguments.length == 1 || !parent) {
    parent = app.activeDocument;
    deep = true;
  }
  var result = [];
  if (!parent[type]) return result;
  for (var i = 0; i < parent[type].length; i++) {
    result.push(parent[type][i]);
    if (parent[type][i][type] && deep)
      result = [].concat(result, get(type, parent[type][i], deep));
  }
  return result;
}

function getNearestGroupParent(item) {
  if (!item.parent) return null;
  return /group/i.test(item.parent.typename)
    ? item.parent
    : getNearestGroupParent(item.parent) || null;
}
function isValidGroup(item) {
  return item && /group/i.test(item.typename);
}

app.selection = null;
var input = prompt("Enter comma separated text to select", ["3454", "3457"]);
var rx = new RegExp("(" + input.split(",").join("|") + ")");
var results = get("textFrames")
  .filter(function (textFrame) {
    return rx.test(textFrame.contents);
  })
  .map(function (textFrame) {
    return getNearestGroupParent(textFrame);
  });

if (!results.length) alert("No result found for " + input);
else
  results.forEach(function (group) {
    if (isValidGroup(group)) group.selected = true;
  });

 

2 replies

Inventsable
InventsableCorrect answer
Legend
April 1, 2023

Alternative approach to the other answers, in that it:

 

  1. Searches for the matching text then recurses parent instead of looping through the entirety of document groups (so it matches a much smaller collection then looks up instead of looping large collections and matching down)
  2. Uses a single regular expression to match text instead of splitting and searching for each value

 

Array.prototype.filter = function (callback) {
  var filtered = [];
  for (var i = 0; i < this.length; i++)
    if (callback(this[i], i, this)) filtered.push(this[i]);
  return filtered;
};
Array.prototype.forEach = function (callback) {
  for (var i = 0; i < this.length; i++) callback(this[i], i, this);
};
Array.prototype.map = function (callback) {
  var mappedParam = [];
  for (var i = 0; i < this.length; i++)
    mappedParam.push(callback(this[i], i, this));
  return mappedParam;
};
function get(type, parent, deep) {
  if (arguments.length == 1 || !parent) {
    parent = app.activeDocument;
    deep = true;
  }
  var result = [];
  if (!parent[type]) return result;
  for (var i = 0; i < parent[type].length; i++) {
    result.push(parent[type][i]);
    if (parent[type][i][type] && deep)
      result = [].concat(result, get(type, parent[type][i], deep));
  }
  return result;
}

function getNearestGroupParent(item) {
  if (!item.parent) return null;
  return /group/i.test(item.parent.typename)
    ? item.parent
    : getNearestGroupParent(item.parent) || null;
}
function isValidGroup(item) {
  return item && /group/i.test(item.typename);
}

app.selection = null;
var input = prompt("Enter comma separated text to select", ["3454", "3457"]);
var rx = new RegExp("(" + input.split(",").join("|") + ")");
var results = get("textFrames")
  .filter(function (textFrame) {
    return rx.test(textFrame.contents);
  })
  .map(function (textFrame) {
    return getNearestGroupParent(textFrame);
  });

if (!results.length) alert("No result found for " + input);
else
  results.forEach(function (group) {
    if (isValidGroup(group)) group.selected = true;
  });

 

Known Participant
April 1, 2023

Thank you for your help The script works perfectly! I wish you a happy life

Known Participant
March 28, 2023

// Get the current document
var currentDocument = app.activeDocument;
if (!currentDocument) {
alert("No document is currently open!");
}

// Get all groups in the current document
var allGroups = currentDocument.groupItems;
if (!allGroups || allGroups.length === 0) {
alert("There are no groups in the current document!");
}

// Define an array of text to search for
var searchTexts = ["Text to search for 1", "Text to search for 2", "Text to search for 3"];

// Define a collection to store text frame objects
var allTextFrames = [];

// Get all text frames in the current document and store them in the collection
for (var i = 0; i < allGroups.length; i++) {
var currentGroup = allGroups[i];

if (currentGroup.pageItems.length > 0) {
var textFrames = currentGroup.pageItems.typename === 'TextFrame' ? [currentGroup.pageItems] : currentGroup.pageItems.textFrames;

css
Copy code
for (var j = 0; j < textFrames.length; j++) {
var currentTextFrame = textFrames[j];
allTextFrames.push(currentTextFrame);
}
}
}

// Output the number of text frames found
alert("Number of text frames found: " + allTextFrames.length);

// Loop through all groups and search for text frames that contain the specified text
var hasTextFrames = false; // Check if any text frames exist in the document

for (var i = 0; i < allGroups.length; i++) {
var currentGroup = allGroups[i];

if (currentGroup.pageItems.length > 0) {
var textFrames = currentGroup.pageItems.typename === 'TextFrame' ? [currentGroup.pageItems] : currentGroup.pageItems.textFrames;

kotlin
Copy code
for (var j = 0; j < textFrames.length; j++) {
var currentTextFrame = textFrames[j];

// If the text frame contains the specified text, select the group that it belongs to
if (searchTexts.indexOf(currentTextFrame.contents) !== -1) {
currentGroup.selected = true;
hasTextFrames = true;
break;
}
}
}
}

if (!hasTextFrames) {
alert("There are no text frames in the document that contain the specified text!");
}

Error in running the script, can someone help me modify it? Thanks

renél80416020
Inspiring
March 28, 2023

Bonjour,

 

 

// JavaScript Document
// Searches for the first occurrence of the following texts by browsing the groups.
// Define an array of text to search for
var searchText = "344,556,996,6544";

if (app.documents.length) {main();}
else alert("No document is currently open!");
// -------
function main() {
  var doc = app.activeDocument;
  // Get all groups in the current document
  if (doc.groupItems == 0) {
  alert("There are no groups in the current document!");
  return;
  }
  var allGroups = [];
   for (var i = 0; i < doc.groupItems.length; i++) {
      allGroups.push(doc.groupItems[i]);
   }
  var rep, searchTexts , count, currentGroup, textFrames;
      rep = prompt("Search list\r(use comma separator like)",searchText, " De elleere");
      if (rep == null || rep == "") return;
      searchTexts = rep.split(",");
      for (var k = 0; k < searchTexts.length; k++) {
       count = 0;
        for (var i = allGroups.length-1; i >= 0; i--) {
            currentGroup = allGroups[i];
            textFrames = currentGroup.textFrames;
             for (var j = 0; j < textFrames.length; j++) {
                if (textFrames[j].contents == searchTexts[k]) {
                  currentGroup.selected = true;
                  //textFrames[j].selected = true
                  count++;
                  allGroups.splice(i,1)
                  break;
                }
             }
        }
        if (!count) alert("There are no text frames in the document that contain the specified text!\r"+searchTexts[k]);
      }
}
// -------

 

 

de elleere

Met1
Legend
March 28, 2023

I so wanted that to work! Alas it did not (yes I changed the text to my search criteria).

I have a sheet with every Pantone PMS color, arranged as they are in the Formula Guide, and sometimes I just can't find the one I'm looking for...

ANd I did think the OP wanted a dialog to input text. I really should look in to how to do that bit myself.