: Search for string and delete entire text frame with JavaScript for Illustrator I can delete a layer by name: var myDoc = app.activeDocument; var myLayers = myDoc.layers; var
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();
}
}
}
More posts by @Karen819
2 Comments
Sorted by latest first Latest Oldest Best
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 ();
}
}
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.
Terms of Use Create Support ticket Your support tickets Stock Market News! © vmapp.org2024 All Rights reserved.