Skip to main content
victorcarbajo
Inspiring
Jan 24, 2011 6:51 PM UTC
Answered

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

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.

CarlosCanto
CarlosCantoCorrect answer
Community Expert
Mar 6, 2011 6:04 AM UTC

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();

85 replies

Participant
Jul 3, 2023 11:53 PM UTC

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?

Translate
CarlosCanto
Community Expert
Community Expert
Jul 4, 2023 12:16 AM UTC

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

Translate
Participant
Jul 4, 2023 1:04 AM UTC

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

Translate
CarlosCanto
Community Expert
Community Expert
Nov 11, 2019 2:55 AM UTC

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?

Translate
ahablett
Participant
Nov 11, 2019 7:21 AM UTC

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!

Translate
CarlosCanto
Community Expert
Community Expert
Nov 11, 2019 7:34 AM UTC

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

Translate
CarlosCanto
Community Expert
Community Expert
Nov 8, 2019 8:19 AM UTC

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)

	}

Translate
ahablett
Participant
Nov 11, 2019 1:14 AM UTC

Hey Carlos, 

This is the message I get with this script running Adobe CC (2019) on a mac. Let me know if there's any other info you need or resources I can research to figure out how to get the scripts to work—the word separation one specifically would save days of work. I'm grateful for this thread to show it's even a possibility. 🙂 

 

Translate
CarlosCanto
Community Expert
Community Expert
Oct 29, 2019 7:43 AM UTC

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

	}

 

Translate
Participating Frequently
Nov 6, 2019 8:24 PM UTC

Thanks Carlos!

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

Translate
Participating Frequently
Mar 26, 2018 12:56 PM UTC

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!

Translate
CarlosCanto
Community Expert
Community Expert
Mar 26, 2018 3:07 PM UTC

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

Translate
Participating Frequently
Apr 3, 2018 4:51 AM UTC

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

Translate
CarlosCanto
Community Expert
Community Expert
Jan 30, 2011 9:28 AM UTC

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

Translate
victorcarbajo
Inspiring
Jan 30, 2011 11:40 PM UTC

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.

Translate
CarlosCanto
Community Expert
Community Expert
Jan 31, 2011 12:43 AM UTC

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

Translate