Skip to main content
dublove
Legend
June 5, 2026
Answered

Is it possible to automatically create a text frame of the same size in the original location?

  • June 5, 2026
  • 3 replies
  • 88 views

I want to translate the old text into English, but the original text may have been converted to curves. I need to create a text frame of the same size (the exact height might be difficult to achieve, so the same width would work as well).
I’d like the script to move the selected objects to the “old layer,” hide that layer, create a “area text box” of the same size in the original position, and move that text box to the “Text layer.”

If possible, please pre-fill it with the letter “O”

 

    Correct answer jduncan

    Try this out and let me know if it works for you. This will iterate over all selected object and create matching text frames no matter the type.

    /*
    Adobe Community Forum Question - https://community.adobe.com/questions-652/is-it-possible-to-automatically-create-a-text-frame-of-the-same-size-in-the-original-location-1626610

    I want to translate the old text into English, but the original text may have been converted to curves. I need to create a text frame of the same size (the exact height might be difficult to achieve, so the same width would work as well).

    I’d like the script to move the selected objects to the “old layer,” hide that layer, create a “area text box” of the same size in the original position, and move that text box to the “Text layer.”

    If possible, please pre-fill it with the letter “O”

    Related Docs:
    Working with text frames - https://ai-scripting.docsforadobe.dev/scriptingJavascript/workingWithTextFrames/?h=area+text
    */

    //@target illustrator
    (function () {
    ////////////////////////////
    // MAIN SCRIPT OPERATIONS //
    ////////////////////////////

    // no need to continue if there is no active document
    if (!app.documents.length) {
    alert("No active document.");
    return;
    }

    // grab document
    var doc = app.activeDocument;

    // get the current selection
    var sel = doc.selection;

    // no need to continue if there is no active selection
    if (!sel.length) {
    alert("No active selection.");
    return;
    }

    /**
    * Get layer by `name` or create if not found.
    */
    function getOrCreateLayer(name) {
    var layer;
    try {
    layer = doc.layers.getByName(name);
    layer.visible = true;
    layer.locked = false;
    } catch (e) {
    $.writeln('Layer "' + name + '" not found. Creating it.', e);
    layer = doc.layers.add();
    layer.name = name;
    }
    return layer;
    }

    // setup layers
    var oldLayer = getOrCreateLayer("OLD");
    var textLayer = getOrCreateLayer("TEXT");

    var rect, tf, font;
    for (var i = 0; i < sel.length; i++) {
    // move selected item to "OLD" layer
    sel[i].moveToEnd(oldLayer);

    // create new text frame on "TEXT" layer
    rect = textLayer.pathItems.rectangle(
    sel[i].top,
    sel[i].left,
    sel[i].width,
    sel[i].height
    );
    tf = textLayer.textFrames.areaText(rect);
    tf.textRange.characterAttributes.size = 6;

    // if you want to use a specific font update the name here
    try {
    font = app.textFonts.getByName("TimesNewRomanPSMT");
    tf.textRange.characterAttributes.textFont = font;
    } catch (e) {
    $.writeln("Times New Roman font not found. Using default font.", e);
    }

    tf.contents = "O";
    }

    // lastly, make the "OLD" layer hidden
    oldLayer.visible = false;
    })();

     

    3 replies

    dublove
    dubloveAuthor
    Legend
    June 6, 2026

    Hi ​@jduncan 

    Is it possible to check the “Auto-resize” option using a script?

    Check this box for a simple and effective command.

    m1b offers a solution, but I feel like it doesn't always work.
    If it's too complicated to implement, I'll stick with m1b's solution.

     

     

    jduncan
    Community Expert
    Community Expert
    June 8, 2026

    No, unfortunately I don’t think that setting is available via the API.

    dublove
    dubloveAuthor
    Legend
    June 10, 2026

    Hi ​@jduncan ​@m1b 

    I want to link your code to m1b's.

    But after running your part, the selected state is lost.

    I tried
    app.activeDocument.selection = tf;
    d.select = tf;
    but neither worked.


    How can I keep “tf” selected and pass it to m1b's script?

    /*
    Adobe Community Forum Question - https://community.adobe.com/questions-652/is-it-possible-to-automatically-create-a-text-frame-of-the-same-size-in-the-original-location-1626610
    I want to translate the old text into English, but the original text may have been converted to curves. I need to create a text frame of the same size (the exact height might be difficult to achieve, so the same width would work as well).
    I’d like the script to move the selected objects to the “old layer,” hide that layer, create a “area text box” of the same size in the original position, and move that text box to the “Text layer.”
    If possible, please pre-fill it with the letter “O”
    Related Docs:
    Working with text frames - https://ai-scripting.docsforadobe.dev/scriptingJavascript/workingWithTextFrames/?h=area+text
    */

    //app.preferences.setBooleanPreference("text/autoSizing",!app.preferences.getBooleanPreference("text/autoSizing"));
    //首选项 开启新区域文本自动调整大小
    app.preferences.setBooleanPreference("text/autoSizing", true);

    //@target illustrator
    var tf;
    // grab document
    var d = app.activeDocument;
    // get the current selection
    var sel = d.selection;
    fill();
    function fill() {
    ////////////////////////////
    // MAIN SCRIPT OPERATIONS //
    ////////////////////////////
    // no need to continue if there is no active document
    if (!app.documents.length) {
    alert("No active document.");
    return;
    }

    // no need to continue if there is no active selection
    if (!sel.length) {
    alert("No active selection.");
    return;
    }
    /**
    * Get layer by `name` or create if not found.
    */
    function getOrCreateLayer(name) {
    var layer;
    try {
    layer = d.layers.getByName(name);
    layer.visible = true;
    layer.locked = false;
    } catch (e) {
    $.writeln('Layer "' + name + '" not found. Creating it.', e);
    layer = d.layers.add();
    layer.name = name;
    }
    return layer;
    }
    // setup layers
    var oldLayer = getOrCreateLayer("OLD");
    var textLayer = getOrCreateLayer("TEXT");
    var rect, font;
    for (var i = 0; i < sel.length; i++) {
    // move selected item to "OLD" layer
    sel[i].moveToEnd(oldLayer);
    // create new text frame on "TEXT" layer
    rect = textLayer.pathItems.rectangle(
    sel[i].top,
    sel[i].left,
    sel[i].width,
    sel[i].height
    );
    tf = textLayer.textFrames.areaText(rect);
    tf.textRange.characterAttributes.size = 6;
    // if you want to use a specific font update the name here
    try {
    font = app.textFonts.getByName("TimesNewRomanPSMT");
    tf.textRange.characterAttributes.textFont = font;
    } catch (e) {
    $.writeln("Times New Roman font not found. Using default font.", e);
    }
    tf.contents = "O\rO";
    }
    // lastly, make the "OLD" layer hidden
    oldLayer.visible = false;
    }


    //d.selection = tf;
    d.select = tf;
    //alert(d.selection);

    setAuto(tf);


    /**
    * @file Auto Size Selected TextFrame.js
    *
    * Invoke the "Auto Size" area text option on the selected text frame(s).
    *
    * @author m1b
    * @version 2026-05-18
    * @discussion https://community.adobe.com/questions-652/how-to-enable-the-auto-resize-function-under-the-preferences-and-regional-text-options-1623882
    * https://judicious-night-bca.notion.site/app-getIntegerPreference-e4088e6caef64b3ba5b801d86fba7877
    * https://community.adobe.com/questions-652/can-we-set-auto-size-property-for-text-frames-through-jsx-776876
    */


    //开启文字菜单>>区域文本选项>>自动文本
    function setAuto(s) {
    //if (
    // 0 === app.documents.length
    // || 0 === app.activeDocument.selection.length
    //)
    // return alert('Please select one or more text frames and try again.');
    const KEEP_AUTOSIZE_ON = true;
    autoSizeSelectedTextFrames(KEEP_AUTOSIZE_ON);
    }
    /**
    * Auto-sizes the given `textFrames`.
    * @param {Boolean} [keepOn] - whether to leave Auto Size turned ON (default: true).
    */
    function autoSizeSelectedTextFrames(keepOn) {
    // this is the .aia markup to invoke "Auto Size" in Area Text Options
    const AUTOSIZE_AREATEXT_OPTIONS_AIA_AND_KEEP_ON = '/version 3 /name [ 4 54656d70 ] /isOpen 1 /actionCount 1 /action-1 { /name [ 8 416374696f6e2031 ] /keyIndex 0 /colorIndex 0 /isOpen 0 /eventCount 1 /event-1 { /useRulersIn1stQuadrant 0 /internalName (adobe_SLOAreaTextDialog) /localizedName [ 17 417265612054797065204f7074696f6e73 ] /isOpen 0 /isOn 1 /hasDialog 0 /parameterCount 1 /parameter-1 { /key 1952539754 /showInPalette 4294967295 /type (integer) /value 1 } } }';
    const AUTOSIZE_AREATEXT_OPTIONS_AIA_AND_KEEP_OFF = '/version 3 /name [ 4 54656d70 ] /isOpen 1 /actionCount 1 /action-1 { /name [ 8 416374696f6e2031 ] /keyIndex 0 /colorIndex 0 /isOpen 0 /eventCount 2 /event-1 { /useRulersIn1stQuadrant 0 /internalName (adobe_SLOAreaTextDialog) /localizedName [ 17 417265612054797065204f7074696f6e73 ] /isOpen 0 /isOn 1 /hasDialog 0 /parameterCount 1 /parameter-1 { /key 1952539754 /showInPalette 4294967295 /type (integer) /value 1 } } /event-2 { /useRulersIn1stQuadrant 0 /internalName (adobe_SLOAreaTextDialog) /localizedName [ 17 417265612054797065204f7074696f6e73 ] /isOpen 0 /isOn 1 /hasDialog 0 /parameterCount 1 /parameter-1 { /key 1952539754 /showInPalette 4294967295 /type (integer) /value 2 } } }';
    var aia = keepOn
    ? AUTOSIZE_AREATEXT_OPTIONS_AIA_AND_KEEP_ON
    : AUTOSIZE_AREATEXT_OPTIONS_AIA_AND_KEEP_ON;
    // invoke the auto-size
    doActionFromString(aia);
    };
    /**
    * Create and execute an action using the markup supplied.
    * @author m1b (and thanks to Charu Rajput for the temp loading system).
    * @version 2026-05-18
    * @param {String} aia - the action markup.
    */
    function doActionFromString(aia) {
    // replace the set name with a unique throwaway name to avoid collisions
    var setNameHex = encodeHex('DeleteMe' + Math.floor(Math.random() * 99999 + 1));
    aia = aia.replace(/\/name \[ [0-9]+ [0-9a-f]+ \]/, '/name [ ' + (setNameHex.length / 2) + ' ' + setNameHex + ' ]');
    var findName = /\s\/name\s\[ [0-9]+ ([0-9a-f]+) \]/g;
    var setName = decodeHex(findName.exec(aia)[1]);
    var actionName = decodeHex(findName.exec(aia)[1]);
    try {
    // save temp action, load it, do it, unload it, delete it
    var tmp = File(Folder.temp + '/' + setName + '.aia');
    tmp.open('w');
    tmp.write(aia);
    tmp.close();
    app.loadAction(tmp);
    app.doScript(actionName, setName, false);
    app.unloadAction(setName, '');
    tmp.remove();
    } catch (error) {
    alert('Script failed: ' + error.message);
    }
    };
    /**
    * Returns an ASCII string, given hexadecimal number.
    * @param {String} hex - the hexadecimal to decode.
    * @returns {String} - the decoded string.
    */
    function decodeHex(hex) {
    var result = '';
    for (var i = 0; i < hex.length; i += 2)
    result += String.fromCharCode(parseInt(hex.substr(i, 2), 16));
    return result;
    };
    /**
    * Returns a hexadecimal string encoding of an ASCII string.
    * @param {String} str - the string to encode.
    * @returns {String} - the hex-encoded string.
    */
    function encodeHex(str) {
    var result = '';
    for (var i = 0; i < str.length; i++) {
    var code = str.charCodeAt(i).toString(16);
    result += (code.length < 2 ? '0' : '') + code;
    }
    return result;
    };

     

    dublove
    dubloveAuthor
    Legend
    June 5, 2026

    Hi jduncan

    That's great.
    Thank you so much.

    jduncan
    Community Expert
    jduncanCommunity ExpertCorrect answer
    Community Expert
    June 5, 2026

    Try this out and let me know if it works for you. This will iterate over all selected object and create matching text frames no matter the type.

    /*
    Adobe Community Forum Question - https://community.adobe.com/questions-652/is-it-possible-to-automatically-create-a-text-frame-of-the-same-size-in-the-original-location-1626610

    I want to translate the old text into English, but the original text may have been converted to curves. I need to create a text frame of the same size (the exact height might be difficult to achieve, so the same width would work as well).

    I’d like the script to move the selected objects to the “old layer,” hide that layer, create a “area text box” of the same size in the original position, and move that text box to the “Text layer.”

    If possible, please pre-fill it with the letter “O”

    Related Docs:
    Working with text frames - https://ai-scripting.docsforadobe.dev/scriptingJavascript/workingWithTextFrames/?h=area+text
    */

    //@target illustrator
    (function () {
    ////////////////////////////
    // MAIN SCRIPT OPERATIONS //
    ////////////////////////////

    // no need to continue if there is no active document
    if (!app.documents.length) {
    alert("No active document.");
    return;
    }

    // grab document
    var doc = app.activeDocument;

    // get the current selection
    var sel = doc.selection;

    // no need to continue if there is no active selection
    if (!sel.length) {
    alert("No active selection.");
    return;
    }

    /**
    * Get layer by `name` or create if not found.
    */
    function getOrCreateLayer(name) {
    var layer;
    try {
    layer = doc.layers.getByName(name);
    layer.visible = true;
    layer.locked = false;
    } catch (e) {
    $.writeln('Layer "' + name + '" not found. Creating it.', e);
    layer = doc.layers.add();
    layer.name = name;
    }
    return layer;
    }

    // setup layers
    var oldLayer = getOrCreateLayer("OLD");
    var textLayer = getOrCreateLayer("TEXT");

    var rect, tf, font;
    for (var i = 0; i < sel.length; i++) {
    // move selected item to "OLD" layer
    sel[i].moveToEnd(oldLayer);

    // create new text frame on "TEXT" layer
    rect = textLayer.pathItems.rectangle(
    sel[i].top,
    sel[i].left,
    sel[i].width,
    sel[i].height
    );
    tf = textLayer.textFrames.areaText(rect);
    tf.textRange.characterAttributes.size = 6;

    // if you want to use a specific font update the name here
    try {
    font = app.textFonts.getByName("TimesNewRomanPSMT");
    tf.textRange.characterAttributes.textFont = font;
    } catch (e) {
    $.writeln("Times New Roman font not found. Using default font.", e);
    }

    tf.contents = "O";
    }

    // lastly, make the "OLD" layer hidden
    oldLayer.visible = false;
    })();