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

javascript Copy the textFrames contents to the new textFrames

New Here ,
Aug 04, 2025 Aug 04, 2025

There is no problem copying the manually created textFrames, but there is a problem with the newly created one. I am a novice and don’t know what is going on. Please help me

Error code: targetCharacters[j].appliedFont = sourceCharacters[j + sourceLines[0].length + 1].appliedFont;

                 targetCharacters[j].pointSize = sourceCharacters[j + sourceLines[0].length + 1].pointSize;

 

source code:
var doc = app.activeDocument;


var sourceTextFrameName = "AAA";


for (var i = 0; i < doc.textFrames.length; i++) {
var sourceTextFrame = doc.textFrames[i];


if (sourceTextFrame.name === sourceTextFrameName) {
var sourceLines = sourceTextFrame.contents.split('\r'); 


if (sourceLines.length >= 2) {
var secondLine = sourceLines[1]; 

var frameWidth = sourceTextFrame.geometricBounds[3] - sourceTextFrame.geometricBounds[1]; // 左右边界相减


var newTextFrame = doc.textFrames.add({
geometricBounds: [0, 0, 20, frameWidth], 
contents: "" 
});

newTextFrame.contents = secondLine;


var sourceCharacters = sourceTextFrame.characters;
var targetCharacters = newTextFrame.characters;

for (var j = 0; j < secondLine.length; j++) {


if (j < sourceCharacters.length) {
targetCharacters[j].appliedFont = sourceCharacters[j + sourceLines[0].length + 1].appliedFont; 
targetCharacters[j].pointSize = sourceCharacters[j + sourceLines[0].length + 1].pointSize; 
}
}} else {
alert("NO");
}
break;
}
}

TOPICS
Scripting
191
Translate
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

Community Expert , Aug 04, 2025 Aug 04, 2025

Hi @hecheng_3069 the problem is your 'sourceCharacters = sourceTextFrame.characters' gives you a Characters collection object. This object isn't very useful to you in your script unless you are going to loop over each character, or choose a range of characters. Some methods are: everyItem, firstItem, item, itemByRange, lastItem, middleItem, nextItem, previousItem.

Better might be sourceTextFrame.texts[0], or maybe sourceTextFrame.textStyleRanges.

 

Maybe I am wrong but your code might be taking

...
Translate
Community Expert ,
Aug 04, 2025 Aug 04, 2025

Hi @hecheng_3069 the problem is your 'sourceCharacters = sourceTextFrame.characters' gives you a Characters collection object. This object isn't very useful to you in your script unless you are going to loop over each character, or choose a range of characters. Some methods are: everyItem, firstItem, item, itemByRange, lastItem, middleItem, nextItem, previousItem.

Better might be sourceTextFrame.texts[0], or maybe sourceTextFrame.textStyleRanges.

 

Maybe I am wrong but your code might be taking a difficult approach to this problem. Have a look at this modified code below and see if it is useful to you.

 

If not, please let us know what you are trying to do. And have a good look at the ExtendScript documentation.

- Mark

 

function main() {

    var doc = app.activeDocument;

    var sourceTextFrameName = "AAA";

    // if there is only one sourceTextFrame you can do this:
    var sourceTextFrame = doc.textFrames.itemByName(sourceTextFrameName);

    if (!sourceTextFrame.isValid)
        return alert('Could not find text frame.');

    var sourceParagraphs = sourceTextFrame.paragraphs;

    if (sourceParagraphs.length < 2)
        return alert("No");

    var secondParagraph = sourceParagraphs[1];

    var frameWidth = sourceTextFrame.geometricBounds[3] - sourceTextFrame.geometricBounds[1]; // 左右边界相减

    var newTextFrame = doc.textFrames.add({
        geometricBounds: [0, 0, 20, frameWidth],
        contents: ""
    });

    secondParagraph.duplicate(LocationOptions.AT_BEGINNING, newTextFrame);

};
app.doScript(main, ScriptLanguage.JAVASCRIPT, undefined, UndoModes.ENTIRE_SCRIPT, 'Second Paragraph Example');

 

Translate
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 ,
Aug 04, 2025 Aug 04, 2025
LATEST

You're awesome!!
I'm still learning, so I'm experimenting with different functions to achieve what I need.
My main goal is the following logic:
1. I want to loop through all text boxes and process any text box named "AAA","BBB" , "CCC", "DDD"


For the AAA text box: Suppose the first line contains "123456," the second line contains "123456789," and the third line contains "123456." However, the width of the text box causes the second line to wrap, resulting in a four-line layout. Scale all characters horizontally to return them to three lines.


For the BBB text box: Use the same rules as above, but compress the font size. The minimum font size must be at least 5, and the line spacing must be adjusted accordingly.

 

For the CCC text box: Compress the font size horizontally to prevent overflow if the text overflows.


For the DDD text box: Compress the font size horizontally to prevent overflow if the text overflows. The minimum font size must be at least 5, and the line spacing must be adjusted accordingly.

Translate
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