Copy link to clipboard
Copied
Hi!
I have a textbox , xText1, containing words
Circle
Triangle
Square
Rectangle
Oval
Trapezoid
Star
Arrow
Pentagon
Octagon
Decagon
I want to loop and create seperate text boxes containing each word, having the corresponding word name
Any ideas?
To convert the contents of the textFrame into an array, split by the carriage return (\r). You can then loop through the array:
// select textFrame
var text1 = app.selection[0].contents;
var text2 = text1.split("\r");
for (var i = 0; i < text2.length; i++) {
alert(text2[i]);
}
Copy link to clipboard
Copied
I started writing a script, then I remembered that John Wundes had already written one.
https://github.com/johnwun/js4ai/blob/master/divideTextFrame.js
(I presume that you mean separate textFrames, not separate layers, since your image doesn't show separate layers.)
Copy link to clipboard
Copied
I know this script , i was hoping for simpler , without getting text position , justification
Just the loop for getting the array
Anyway, I will try to adpat it
Thank you @femkeblanco
Copy link to clipboard
Copied
Yes, separate textFrames!
Copy link to clipboard
Copied
To convert the contents of the textFrame into an array, split by the carriage return (\r). You can then loop through the array:
// select textFrame
var text1 = app.selection[0].contents;
var text2 = text1.split("\r");
for (var i = 0; i < text2.length; i++) {
alert(text2[i]);
}
Copy link to clipboard
Copied
It is what i need 🙂
Thank you @femkeblanco
Copy link to clipboard
Copied
I wanted to take a crack at it testing out a technique to keep the formatting without going through all the characterAttributes properties. It may use commands which have not been around during the time of the Wundes script, I'm not sure, but the method is:
At first I thought it was working, but then I saw that contents.trim() or any contents-operations on text does away with the formatting. So, in addition to removing lines of text to turn them blank, I had to go through the characters of the text which resulted from that operation to manually remove any nextlines and such. Because the blank lines have been removed, the operation to go through the characters of a duplicated text frame which had all except for 1 line turned into a blank is really fast. However, this script skips the formatting and vertical spacing entirely while preserving them.
//@target illustrator
function test() {
if (!Array.prototype.forEach) {
Array.prototype.forEach = function (callback, thisArg) {
var T, k;
if (this == null) {
throw new TypeError(' this is null or not defined');
}
var O = Object(this);
var len = O.length >>> 0;
if (typeof callback !== "function") {
throw new TypeError(callback + ' is not a function');
}
if (arguments.length > 1) {
T = thisArg;
}
k = 0;
while (k < len) {
var kValue;
if (k in O) {
kValue = O[k];
callback.call(T, kValue, k, O);
}
k++;
}
};
};
if (!Array.from) {
Array.from = function (arrayLikeObject) {
var arr = [];
var thisItem;
for (var i = 0; i < arrayLikeObject.length; i++) {
thisItem = arrayLikeObject[i];
arr.push(thisItem);
}
return arr;
};
};
// ------------------------------------------------------------------------------------------------------------------------ //
// ------------------------------------------------------------------------------------------------------------------------ //
var AppStrings = {
SEPARATED_TEXT: "Separated Text"
};
var step, doc, sel;
try {
step = "Open-document check.";
doc = app.activeDocument;
step = "Selection check";
if (doc.selection.length == 0) {
throw new Error("No Selection");
}
sel = doc.selection[0];
step = "Selection is a text-box check";
if (sel.typename != "TextFrame") {
throw new Error("The 1st selected object is not a text-frame.");
}
}
catch (error) {
alert("There was an error during the '" + step + "' step:\n" + error.message);
return;
}
if (sel.kind != TextType.POINTTEXT) {
sel.convertAreaObjectToPointObject();
app.redraw();
sel = doc.selection[0];
}
var originalUuid = sel.uuid;
sel = doc.getPageItemFromUuid(originalUuid);
doc.selection = null;
var sourceLines = Array.from(sel.lines);
var textLineGroup = doc.groupItems.add();
textLineGroup.name = AppStrings.SEPARATED_TEXT;
Array.from(sourceLines).reverse().forEach(function (thisLine, i) {
var newBox = sel.duplicate(sel, ElementPlacement.PLACEBEFORE);
Array.from(newBox.lines).reverse().forEach(function (n, j) {
if (j != i) {
n.remove();
}
});
var newBoxCopy = newBox.duplicate(newBox, ElementPlacement.PLACEBEFORE);
var isolatedLineTextBoxOutlined = newBoxCopy.createOutline();
Array.from(newBox.characters).reverse().forEach(function (n, j) {
if (n.contents == "" || n.contents == "\r" || n.contents == "\n" || n.contents == "\t") {
n.remove();
}
});
var newNewBoxCopy = newBox.duplicate(newBox, ElementPlacement.PLACEBEFORE);
var isolatedLineTextBoxCopy = newNewBoxCopy.createOutline();
var dX = isolatedLineTextBoxCopy.left - isolatedLineTextBoxOutlined.left;
var dY = isolatedLineTextBoxCopy.top - isolatedLineTextBoxOutlined.top;
newBox.translate(dX, -dY);
newBox.move(textLineGroup, ElementPlacement.PLACEATEND);
isolatedLineTextBoxCopy.remove();
isolatedLineTextBoxOutlined.remove();
});
doc.getPageItemFromUuid(originalUuid).remove();
};
test();
Check out these great results! The text can be totally arbitrary.
Copy link to clipboard
Copied
Hi Vasily, thanks for sharing!
I get this
Copy link to clipboard
Copied
Hmm, maybe something is going on which may throw a wrench into the whole thing.
Do your texts look somewhat like this?
Copy link to clipboard
Copied
yes, it looks just like yours
Copy link to clipboard
Copied
Mmm, yes! The script I have created indeed does not do the line-spacing where users can increase it more than the default setting. Maybe due to removing lines, if vertical spacing is default it looks like there is no difference but when there is, the removal of lines does this. With some editing it may be possible to deal with this too.