2

I can delete a layer by name:

    var myDoc = app.activeDocument;
    var myLayers = myDoc.layers;

    var myLayer = myLayers["layer name"];
    myLayer.remove();

or by stacking order:

    var myLayer = myLayers[1];
    myLayer.remove();

and I know how to find and replace text with a script so I am trying to find a way to combine these two and find a string, then delete the layer that string is on since I can't use the layer name or stacking order. I am trying to learn scripting as I go so this may not be possible (then someone saying it's not possible would be helpful) and my script is probably full of issues...but this is what I tried to put together and it doesn't work so here I am asking for help.

    var myDoc = app.activeDocument;
    var search_string = "words I am trying to search for";  
    var text_frames = myDoc.textFrames;  

    if (text_frames.length > 0)  
    {  
        for (var i = 0 ; i < text_frames.length; i++)  
          {  
              var this_text_frame = text_frames[i];   

               if (this_text_frame = search_string.contents)  
                   {  
                        this_text_frame.remove();  
                   }  
          }  
    }  
  • 1) This shows no effort using code and appears to be seeking someone to write something for you. 2) There is no clear question here so I'm unsure what your attempt is. If you would like help please make an edit with a question explaining what you actually need and show us what you've tried and we'll be happy to help. – DᴀʀᴛʜVᴀᴅᴇʀ Dec 19 '17 at 14:45
  • 1
    If you would like to weigh in on the discussion around code questions for applications reference: Should code questions be closed if no code is attempted? – DᴀʀᴛʜVᴀᴅᴇʀ Dec 19 '17 at 14:48
  • Let the record show the initial question was closed for lack of effort and I did downvote the question after closing which resulted the questions receiving a total of three downvotes. OP took it upon their self to edit the question, I reviewed the edit, found it fits our site Q&A and reopened the question. Since the question now shows effort and fits within our model I also plus 1. – DᴀʀᴛʜVᴀᴅᴇʀ Dec 19 '17 at 16:53
2

This will do it so long as there is a consistent piece of text to search for. Can be applied to an action and batched.

Notice that it loops in reverse order. This is a common strategy whenever looping items that will get removed from the array. Otherwise, the shortening array length will interfere with future cycles through the loop.

In using the match method, you can see if a string is anywhere within the contents.

The match method will also accept a RegExp object (regular expression). This will be useful if your find text changes slightly but follows a pattern.

var doc, texts, i, count, find;

find = "MyString"; // Replace with your own string

doc = activeDocument;
texts = doc.textFrames;
count = texts.length;
for (i=count-1; i>0; i--)
{ if (texts[i].contents.match (find) == find)
  { texts[i].remove ();
  }
}
1

One method would be to just Find and Replace, replace with nothing and run this script to remove all empty text frames.

Written By Łukasz Wieczorek - hellowoo.com
you can destribute this freely via creative commons(http://creativecommons.org/licenses/by-sa/3.0/)
 but leave this message in tact, or give credit to original script.

var numberOfEmptyTextBoxes = 0;
var layersWithNoText = new Array();
if ( app.documents.length > 0 ) {
    for ( i = 0; i < app.activeDocument.textFrames.length; i++ ) {
        text = app.activeDocument.textFrames[i].textRange;
        numWords = app.activeDocument.textFrames[i].words.length;
        if (numWords == 0){ 
            layersWithNoText.push(app.activeDocument.textFrames[i]);
            numberOfEmptyTextBoxes++; 
        }
    }
    if(numberOfEmptyTextBoxes > 0){
        alert("You have " + numberOfEmptyTextBoxes + " empty text boxes. They will be deleted.");
    }
    removeTextLayersWithNoContent(layersWithNoText);
}
function removeTextLayersWithNoContent(layersWithNoText) {
    var layersWithNoText = layersWithNoText;
    for (var i = 0; i < layersWithNoText.length; i++) {
        var currentIndex = layersWithNoText[i];
        layersWithNoText[i].remove();
    };
}

Copy paste this and save it out as a .jsx

Sadly Illustrator doesn't have as robust searching methods as InDesign does.

  • The problem is that the text I want to delete varies from file to file but has one section that would be consistent. I wanted to use the consistent section to be able to call out the whole text box if possible. – kmrtn Dec 18 '17 at 21:13
  • 1
    @kmrtn If it varies then this is probably un-batchable. What is the text even? Can you update your question with some more detailed information/screenshots? – Ovaryraptor Dec 18 '17 at 22:09

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.