How do I rename a text box in Illustrator with a JS script? (CS4)

New Here ,
Jul 19, 2021 Jul 19, 2021

Copy link to clipboard

Copied

Sorry I'm super new to this, I can't figure out how to rename a text box. I have a function that's supposed to take in a text box object and resize it to fit the text inside. I would want it to keep the same text box, but the way the function works now it makes a new one that's the right size. That's ok, but I want it to keep the same name so I can refer to the same textbox in future executions of the script, with like docRef.textFrames.getByName("myTextboxName"); (probably not the right way to do this but it's what I could figure out). However, I can't figure out how to rename the thing, it always leaves it unnamed after the function runs. I can change the text in the text box using newObj.parent.textRange.contents = "myText"; but trying to assign a title with like newObj.parent.textRange.name doesn't work.

function resizeTextBox(obj) { // Got the code for this function from http://kelsocartography.com/scripts/scripts/nvkelso/FitToTextContent_v2.jsx
var includeExtraLines = 0.5;
objTop = obj.top;
objLeft = obj.left;

// Make the new point type object and locate it
// Make sure the new object is in the same Z stacking order as the original
copy1 = obj.duplicate(obj, ElementPlacement.PLACEBEFORE);
//copy1.move(obj, ElementPlacement.PLACEBEFORE);

// now make the text box much bigger, but not absurdly big
if( copy1.height * 10 < 2000 ) {
copy1.textPath.height = copy1.height * 10;
} else {
copy1.textPath.height = 2000;
}

howManyLines = copy1.lines.length;

outlineObject = copy1.duplicate();
outlineObject = outlineObject.createOutline();

targetHeight = outlineObject.height + includeExtraLines * (outlineObject.height / howManyLines );

// Now assign y-axis depth of the point text to the area text box
rect = obj.parent.pathItems.rectangle(copy1.textPath.top, copy1.textPath.left, obj.width, targetHeight);
copy2 = obj.parent.textFrames.areaText(rect);
copy2.selected = true;
rect.selected = true;

// Always delete these intermediate objects
outlineObject.remove();
copy1.remove();

// Now take care of the end and original objects
newObj = obj.textRange.duplicate(copy2);
obj.remove();

//NEED TO RENAME HERE
}


Thanks so much, and sorry for the noob question. I'm using an old version of Illustrator if that changes anything (CS4).

TOPICS
Scripting

Views

50

Likes

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
Adobe Community Professional ,
Jul 20, 2021 Jul 20, 2021

Copy link to clipboard

Copied

I'm not sure what the script does but after deleting the original selection and the copies the resulting text frame is copy2, 

 

place this at the end of your script

copy2.contents = "myText";
copy2.name = "newObjName";

 

Likes

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
Enthusiast ,
Jul 20, 2021 Jul 20, 2021

Copy link to clipboard

Copied

The script fits the height of a text frame to the text contents. 

 

To keep the name of "obj", assign it to a variable as the first line in the function definition:

var name1 = obj.name;

 

Then assign that variable to the name of "copy2" as the last line in the function definition(like Carlos said):

copy2.name = name1;

 

Likes

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
New Here ,
Jul 21, 2021 Jul 21, 2021

Copy link to clipboard

Copied

LATEST

that worked, thanks so much

Likes

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