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

Script, split words in text box, to separate layers

Engaged ,
Feb 12, 2022 Feb 12, 2022

Copy link to clipboard

Copied

Hi!
I have a textbox , xText1, containing words

siomosp_0-1644671023010.png

 

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
 

siomosp_1-1644671149508.png

Any ideas?

TOPICS
Scripting

Views

651

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 1 Correct answer

Guide , Feb 12, 2022 Feb 12, 2022

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

 

Votes

Translate

Translate
Adobe
Guide ,
Feb 12, 2022 Feb 12, 2022

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.)

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
Engaged ,
Feb 12, 2022 Feb 12, 2022

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

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
Engaged ,
Feb 12, 2022 Feb 12, 2022

Copy link to clipboard

Copied

Yes, separate textFrames!

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 ,
Feb 12, 2022 Feb 12, 2022

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

 

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
Engaged ,
Feb 12, 2022 Feb 12, 2022

Copy link to clipboard

Copied

It is what i need 🙂

Thank you @femkeblanco

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
Valorous Hero ,
Feb 12, 2022 Feb 12, 2022

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:

 

  1. Per line, to actually copy the whole textframe,
  2. blank-out all the lines which aren't the one line being processed,
  3. copy & outline the singled-out text frame to get its coordinates.
  4. using removal of characters & not contents.trim(), trim the non-outlined copied text box which has every line except for 1 as blank, so the content moves up to the very top line.
  5. Get the coordinates of the one-line-only textbox by outlining a copy of it and getting the top/left.
  6. Get a measurement of the dx and dy from the outlined in-place item vs the trimmed one-line textbox (which may be upwards on the artboard due to the trimmed contents operation).
  7. Translate the editable one-line textbox with those dx and dy coordinates to the place it belongs xy-position-wise.
  8. Clean up any work items by removing them.

 

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.

SillyV_0-1644688058584.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
Community Expert ,
Feb 12, 2022 Feb 12, 2022

Copy link to clipboard

Copied

Hi Vasily, thanks for sharing!

I get this

splitparagraphs.jpg

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
Valorous Hero ,
Feb 12, 2022 Feb 12, 2022

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?

SillyV_0-1644719955027.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
Community Expert ,
Feb 13, 2022 Feb 13, 2022

Copy link to clipboard

Copied

yes, it looks just like yours

splitparagraphs2.jpg

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
Valorous Hero ,
Feb 13, 2022 Feb 13, 2022

Copy link to clipboard

Copied

LATEST

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.

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