• Global community
    • Language:
      • Deutsch
      • English
      • Español
      • Français
      • Português
  • 日本語コミュニティ
    Dedicated community for Japanese speakers
  • 한국 커뮤니티
    Dedicated community for Korean speakers
Exit
0

Does illustrator have such a script?

Participant ,
Mar 27, 2023 Mar 27, 2023

Copy link to clipboard

Copied

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!

TOPICS
Feature request , Scripting , Third party plugins

Views

1.3K

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines

correct answers 2 Correct answers

Advocate , Mar 29, 2023 Mar 29, 2023

Bonjour,

Merci femkeblanco, j'ai corrigé le script plus haut.

l'alerte signifie:

Il n’y a pas de blocs de texte dans le document qui contiennent le texte spécifié

200

j'ai ajouté au début du script

var searchText = "344,556,996,6544";

René

 

 

Votes

Translate

Translate
Enthusiast , Mar 31, 2023 Mar 31, 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(th
...

Votes

Translate

Translate
Adobe
Participant ,
Mar 27, 2023 Mar 27, 2023

Copy link to clipboard

Copied

// 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? Thanks9.png

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Advocate ,
Mar 28, 2023 Mar 28, 2023

Copy link to clipboard

Copied

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

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Mentor ,
Mar 28, 2023 Mar 28, 2023

Copy link to clipboard

Copied

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.

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Participant ,
Mar 28, 2023 Mar 28, 2023

Copy link to clipboard

Copied

Hello renél80416020 First of all, thank you for your help, this window pops up when the script runs6.png

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Guide ,
Mar 29, 2023 Mar 29, 2023

Copy link to clipboard

Copied

I think there is a typo in the script.  I think this line

for (var i = allGroups.length-1; i > 0; i--) {

should be

for (var i = allGroups.length-1; i > -1; i--) {

 For a prompt, change the first line to

var searchTexts = [prompt()];

Note that the script will only work with the particular hierarchy that is a text frame within a group. 

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Participant ,
Mar 29, 2023 Mar 29, 2023

Copy link to clipboard

Copied

Hello femkeblanco First of all, thank you for your help. Can you modify it for me? Can you search for multiple text objects? Listed 344,556,996,6544....

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Advocate ,
Mar 29, 2023 Mar 29, 2023

Copy link to clipboard

Copied

Bonjour,

Merci femkeblanco, j'ai corrigé le script plus haut.

l'alerte signifie:

Il n’y a pas de blocs de texte dans le document qui contiennent le texte spécifié

200

j'ai ajouté au début du script

var searchText = "344,556,996,6544";

René

 

 

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Participant ,
Mar 29, 2023 Mar 29, 2023

Copy link to clipboard

Copied

Hello renél80416020 I don't know programming, can you help me use the following code and modify it for me? Thanks

// JavaScript Document
// Searches for the first occurrence of the following texts by browsing the groups.
// Define an array of text to search for
var searchTexts = [prompt()];

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 count, currentGroup, textFrames;
for (var k = 0; k < searchTexts.length; k++) {
count = 0;
for (var i = allGroups.length-1; i > -1; 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]);
}
}
// -------

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Advocate ,
Mar 31, 2023 Mar 31, 2023

Copy link to clipboard

Copied

Bonjour Lengxiaomo,

J'ai modifié le script (voir pus haut premier post)

René

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Enthusiast ,
Mar 31, 2023 Mar 31, 2023

Copy link to clipboard

Copied

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;
  });

 

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Participant ,
Apr 01, 2023 Apr 01, 2023

Copy link to clipboard

Copied

LATEST

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

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines