Copy link to clipboard
Copied
So I am wanting to search through the entire document for all text frames that contain an underscore. If the text frame does include an underscore I am wanting to move that text frame to a specific layer called "Hypertext". Then I want to remove the underscore from that text frame. So in my code below I have it doing everything I have mentioned except I don't know how to get it to move to a specific layer. Any help would be appreciated!
#target illustrator
var doc = app.activeDocument;
var allText = doc.textFrames;
var searchString = /_/g;
var replaceString = " ";
for (var i = 0; i < allText.length; i++) {
var thisTextFrame = allText;
var newString = thisTextFrame.contents.replace(searchString, replaceString);
if (newString != thisTextFrame.contents) {
//alert("Contains an underscore");
allText.move("Hypertext");
thisTextFrame.contents = newString;
}
}
You can just use allText.move(targetLayer, ElementPlacement.PLACEATEND)
But you may want to go through the loop backwards in case the moving changes the length property of the collection of text frames.
Copy link to clipboard
Copied
You can just use allText.move(targetLayer, ElementPlacement.PLACEATEND)
But you may want to go through the loop backwards in case the moving changes the length property of the collection of text frames.
Copy link to clipboard
Copied
Works perfect! Thank you! Here is the final code with the targetLayer variable....
#target illustrator
var doc = app.activeDocument;
var allText = doc.textFrames;
var searchString = /_/g;
var replaceString = " ";
var targetLayer = doc.layers.getByName("Hypertext");
for (var i = allText.length - 1; i >= 0; i--) {
var thisTextFrame = allText;
var newString = thisTextFrame.contents.replace(searchString, replaceString);
if (newString != thisTextFrame.contents) {
//alert("Contains an underscore");
allText.move(targetLayer, ElementPlacement.PLACEATEND);
thisTextFrame.contents = newString;
}
}
Find more inspiration, events, and resources on the new Adobe Community
Explore Now