Skip to content

Instantly share code, notes, and snippets.

@creold
Created August 17, 2023 08:25
Show Gist options
  • Save creold/4a6f3c4ad0174d9ad5f6463ba5c47696 to your computer and use it in GitHub Desktop.
Save creold/4a6f3c4ad0174d9ad5f6463ba5c47696 to your computer and use it in GitHub Desktop.
Converts selected point textFrames into a Block of Text. Adobe Illustrator script
//@target Illustrator
// script.name = textBlockLive.jsx;
// script.description = converts selected point textFrames into a Block of Text;
// script.required = one document with at least two selected Point Text frames;
// script.parent = carlos canto // 12/4/11; Update 03/15/205 added User Defined Units, cosmetics
// script.modification = sergey osokin // 08/12/23; keep text editable, sort texts by Y, cosmetics
// script.elegant = false;
function main() {
if (!app.documents.length) {
alert("There are no open documents");
return;
}
var sel = app.selection;
var tfs = getTextFrames(selection);
if (tfs.length < 2) {
alert("Select at least 2 Point Text Frames before running");
return;
}
// Sort array by Y and X positions
tfs.sort(function (a, b) {
return comparePosition(b.top, a.top, a.left, b.left)
});
var width = prompt("Enter desired Text Block width including Units", '300 pt', "Text Block");
if (width == null) return;
var widthUV = new UnitValue(width);
if (widthUV.type == '?') {
alert('Units were not provided, try again...');
return;
}
var widthPts = widthUV.as("pt") // convert to points
var spacing = prompt("Enter spacing including Units", '3 mm', "Text Block"); // text lines spacing in mm
if (spacing == null) return;
var spcingUV = new UnitValue(spacing);
if (spcingUV.type == '?') {
alert('Units were not provided, try again...');
return;
}
var spacingPts = spcingUV.as("pt") // convert to points
var blockGrp = selection[0].layer.groupItems.add(); // add a group to final output
blockGrp.name = "Text Block";
var left = 0;
var top = 0;
var firstTop = 0;
for (var i = tfs.length - 1; i >= 0; i--) { // loop thru selection
var tf = tfs[i];
var iText = tf.duplicate(blockGrp, ElementPlacement.PLACEATEND); // duplicate text
iText.selected = false; // deselect it
var iOutlined = iText.createOutline(); // create outlines
var perCent = widthPts / iOutlined.width * 100; // get scaling percentage, based on desired width of block
var scaleMatrix = app.getScaleMatrix(perCent, perCent);
iOutlined.remove();
iText = tf.duplicate(blockGrp, ElementPlacement.PLACEATEND);
iText.selected = false;
iText.transform(scaleMatrix);
iOutlined = iText.duplicate().createOutline();
var deltaX = iText.left - iOutlined.left;
var deltaY = iText.geometricBounds[1] - iOutlined.geometricBounds[1];
iText.left = left + deltaX;
iText.top = top + deltaY + iOutlined.height + spacingPts;
top = iText.top - deltaY;
if (i == 0) firstTop = tf.top + deltaY;
iOutlined.remove();
}
blockGrp.position = [tf.left + tf.width + 40, firstTop];
}
// Get TextFrames array from collection
function getTextFrames(coll) {
var tfs = [];
for (var i = 0, len = coll.length; i < len; i++) {
if (/text/i.test(coll[i].typename))
tfs.push(coll[i]);
else if (/group/i.test(coll[i].typename))
tfs = tfs.concat(getTextFrames(coll[i].pageItems));
}
return tfs;
}
// Compare position of two objects
function comparePosition(a1, b1, a2, b2) {
return a1 == b1 ? a2 - b2 : a1 - b1;
}
try {
main();
} catch (e) {}
@creold
Copy link
Author

creold commented Aug 17, 2023

More about script in Telegram channel.

TextBlock

@ameer-pixel
Copy link

good

@emosGambler
Copy link

Thank you, works great :)

@babyufo
Copy link

babyufo commented Oct 22, 2024

really cool, thanks

@adrralph
Copy link

Escelente, thanks!!

@EmeryNdaliko
Copy link

good job, it work thanks

@Carlogoose
Copy link

I keep getting this:
Select at least 2 Point Text Frames before running

Anyone know what it means?

Cheers

@creold
Copy link
Author

creold commented Feb 5, 2025

@Carlogoose This means that you did not select two text objects. See the example on the GIF. If you have created an area type with multiple lines of text, the script will not process such an object.

@Carlogoose
Copy link

@creold Nice one thanks!

@oldgold1877
Copy link

Does anyone know of a similar script for Photoshop?

@creold
Copy link
Author

creold commented Feb 23, 2025

@oldgold1877 I will think about your question about the Adobe Photoshop version.

@creold
Copy link
Author

creold commented Mar 4, 2025

@oldgold1877
Copy link

Added Photoshop version: https://github.com/creold/photoshop-scripts/blob/master/README.md#textblock

This is fantastic. You are a star πŸ™

@Natalia-Gab
Copy link

This is amazing. Thanks πŸ˜πŸ‘πŸ‘

@lefty-developer
Copy link

Nice one bro

@Saikat-03
Copy link

Nice one Thanks.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment