Skip to main content
victorcarbajo
Inspiring
January 24, 2011
Answered

How to divide all textFrames in one-character-per-textFrame?

  • January 24, 2011
  • 6 replies
  • 43079 views

Hello:

How to divide all textFrames in one-character-per-textFrame?

Example: the textFrame "Letters" will be divided in 7 textFrames: "L", "e", "t", "t", "e", "r", "s".

Help, please.

Correct answer CarlosCanto

Hi Victor, here's the JS version, sorry for the delay

#target Illustrator

//  script.description = splits selected texFrame into separate characters;

//  script.required = select a point text textFrame before running;

//  script.parent = CarlosCanto;  // 3/5/11

//  script.elegant = false;

var idoc = app.activeDocument;

var tWord = idoc.selection[0];

var xpos = tWord.position[0];

var ypos = tWord.position[1];

var charCount = tWord.characters.length;

for (i=charCount-1 ; i>=0 ; i--)

     {

          var ichar = tWord.duplicate();

          ichar.contents = tWord.characters.contents;

          tWord.characters.remove();

          var width = tWord.width;

          ichar.position = [xpos+width,ypos];

     }

tWord.remove();

6 replies

New Participant
July 3, 2023

Hello, thank you for your contribution. I see that even after all these years, you're still responding. I hope you can see this message and assist me. I would greatly appreciate it.

Is there any way to divide a text into lines? For example:
"Line of text 1
Line of text 2
Line of text 3"

And have each line of text separated by an editable text box?

CarlosCanto
Braniac
July 4, 2023

hello @cparrasi , see my previous post, to break a frame into separate line please use Wundes script

 

https://github.com/johnwun/js4ai/blob/master/divideTextFrame.js

New Participant
July 4, 2023

🔥 Thank you for responding so quickly. I will try to do it with what you have shared.
a question what is WUNDER SCRIPT 🙊

CarlosCanto
Braniac
November 11, 2019

Hi ahablett, did you use the latest script I posted yesterday? can you show a screen shot of you selected text including your layers expanded?

ahablett
New Participant
November 11, 2019

I would have sworn on a bible that I did, but going back and redoing the process to take screenshots for you, I must have followed directions right this time, and splitSelectedFramesIntoWords2.0.jsx works like a charm. Thank you for your script and patience, I am so stoked to use this tool!

CarlosCanto
Braniac
November 11, 2019

you're welcome, I'm happy to hear that...you know, almost 9 years later, this script is still rocking.

CarlosCanto
Braniac
November 8, 2019

here's the re-post of the Split Words into Characters script

 

#target Illustrator

//  script.name = splitSelectedWordsIntoCharacters.jsx;
//  script.description = splits selected texFrames into separate characters;
//  script.required = select point text textFrames before running;
//  script.parent = CarlosCanto;  // 4/13/11
//  script.elegant = false;
 
var idoc = app.activeDocument;
var sel = idoc.selection; // get selection
var selCount = sel.length; // count items
var tWord = []; // to hold the textFrames

for (j=selCount ; j>0 ; j--) // loop thru selection & get textFrames backwards
	{
		tWord[j-1] = sel[j-1];
	}


for (k = 0 ; k<tWord.length ; k++) // loop thru textFrames
	{
		var xpos = tWord[k].position[0]; // get x
		var ypos = tWord[k].position[1]; // get y
		var charCount = tWord[k].characters.length; // count characters
 
		for (i=charCount-1 ; i>=0 ; i--) // loop thru characters backwards
			{
				var ichar = tWord[k].duplicate(); // duplicate textFrame
				ichar.contents = tWord[k].characters[i].contents; // get last character
				tWord[k].characters[i].remove(); // remove last character from original word
				var width = tWord[k].width; // get the new width (without the last character)
				ichar.position = [xpos+width,ypos]; // position the character = original position + new width
			}
		
		tWord[k].remove(); // remove textFrame (it is empty by now)

	}

Eduard Buta
Inspiring
March 12, 2023

Hey, @CarlosCanto! Any chance this kind of logic would work for dividing multi-line text into separate lines, row by row? Couldn't find anything on the forums to help me in that regard...

CarlosCanto
Braniac
March 12, 2023
CarlosCanto
Braniac
October 29, 2019

Hi neilb69570407 and ahablett, something happened to the script when it was migrated to the new platform. 

 

try this version

 

#target Illustrator

//  script.name = splitSelectedFramesIntoWords2.0.jsx;
//  script.description = splits selected texFrames into separate words;
//  script.required = select point text textFrames before running;
//  script.parent = CarlosCanto;  // 10/14/11
//  script.elegant = false;
 
var idoc = app.activeDocument;
var sel = idoc.selection; // get selection
var selCount = sel.length; // count items
var tFrames = []; // to hold the textFrames

for (j=selCount ; j>0 ; j--) // loop thru selection & get textFrames backwards
	{
		tFrames[j-1] = sel[j-1];
	}

for (k = 0 ; k<tFrames.length ; k++) // loop thru textFrames
	{
		var xpos = tFrames[k].position[0]; // get x
		var ypos = tFrames[k].position[1]; // get y
		var words = tFrames[k].contents.split(/\s/g); // get all words into an array
		//$.writeln(words);
		var space = tFrames[k].duplicate(); // dup to get width of a space
		space.contents = " ";
		var sw = space.width;
		space.contents = words[0]; // replace space with first word
		var w = space.width; 

		var wordCount = words.length; // count words
 
		for (i=1; i<wordCount ; i++) // loop thru words
			{
				xpos2 = xpos+w+sw; // next words position = previous word pos+width+space
				var iword = space.duplicate(); // duplicate previous word
				iword.contents = words[i]; // add next word
				iword.position = [xpos2,ypos]; // position the character = original position + new width
				w = iword.width; // get words width
				xpos = iword.position[0]; // get words position
			}
		
		tFrames[k].remove(); // remove textFrame

	}

 

Participating Frequently
November 6, 2019

Thanks Carlos!

Could you amend the 'split by characters' script as well?  It gives the same error.

Participating Frequently
March 26, 2018

Hi,

This is nice code but here are some problems you should modify it. This code is not able to break the Multi text line it is working only single line text. but if it apply on multi line text (means on paragraph) then the second line text break but make a cluster in the end of first line. because here the position of y should be change for multi line could you please modify it...

thanks!

CarlosCanto
Braniac
March 26, 2018

use the Wundes script linked in post # 51 to break paragraphs into lines then use my script to break each line into words

Participating Frequently
April 3, 2018

I use that script but that is working like this (2nd, 3rd, 4th..... nth line break in words and add in first line continuously) but the requirement is break words should be in that position in which the paragraph were before breaking the text. If is it possible then help me please.. Thanks a lot.......

CarlosCanto
Braniac
January 30, 2011

here's how to do it with VBA, a copy in blue shown underneath.

Sub oneCharacterPerTFrame()

    Dim Iapp As New Illustrator.Application

    Dim Idoc As Illustrator.Document

    Dim tWord As Illustrator.TextFrame

    Dim tCharacter As Illustrator.TextFrame

    Dim a As Integer

    Dim b As Integer

    Set Idoc = Iapp.ActiveDocument

    Set tWord = Idoc.TextFrames(1)

    x = tWord.Position(0)

    y = tWord.Position(1)

    a = tWord.Characters.Count

    For b = a To 1 Step -1

        Set tCharacter = Idoc.TextFrames.Add

        tCharacter.Contents = tWord.Characters(b).Contents

        tWord.Characters(b).Delete

        w = tWord.Width

        tCharacter.Position = Array(x + w, y)

    Next

    tWord.Delete

    Set tCharacter = Nothing

    Set tWord = Nothing

    Set Idoc = Nothing

    Set Iapp = Nothing

End Sub

victorcarbajo
Inspiring
January 30, 2011

Thanks, Carlos, for your solution, but I have a Mac and Microsoft Visual Basic for Applications does not run on Mac.

Please, do you can translate your code to JavaScript (or AppleScript)?

Best regards.

CarlosCanto
Braniac
January 31, 2011

you're welcome, I have a hard time doing JS, it doesn't seem to stick to my brain....but I'll try.