Skip to main content
Known Participant
August 12, 2022
Answered

Creating different alignment

  • August 12, 2022
  • 3 replies
  • 731 views

How can we set  a text frame contents to vertical alignment center using script?

This topic has been closed for replies.
Correct answer m1b

Thanks @Kurt Gold you have hit the nail on the head!

 

@SA8888, here is a script that incorporates Kurt's action. Give it a try. I found the technique of saving the action file posted to the forum by @Charu Rajput. Thanks Charu!

- Mark

 

/**
 * Center text in selected text frame vertically.
 * @discussion https://community.adobe.com/t5/illustrator-discussions/creating-different-alignment/m-p/13131693
 */

// selection should be a text frame
centerSelectionVertically();



/**
 * Create and execute "Center vertically"
 * action on the selection.
 * @author KurtGold created this action.
 */
function centerSelectionVertically() {

    var actionString = "/version 3\n" +
        "/name [ 29\n" +
        "	616c69676e5f747970655f766572746963616c6c795f63656e74726564\n" +
        "]\n" +
        "/isOpen 1\n" +
        "/actionCount 1\n" +
        "/action-1 {\n" +
        "	/name [ 14\n" +
        "		766572746963616c5f616c69676e\n" +
        "	]\n" +
        "	/keyIndex 0\n" +
        "	/colorIndex 0\n" +
        "	/isOpen 1\n" +
        "	/eventCount 1\n" +
        "	/event-1 {\n" +
        "		/useRulersIn1stQuadrant 0\n" +
        "		/internalName (adobe_SLOAreaTextDialog)\n" +
        "		/localizedName [ 20\n" +
        "			466cc3a46368656e746578746f7074696f6e656e\n" +
        "		]\n" +
        "		/isOpen 1\n" +
        "		/isOn 1\n" +
        "		/hasDialog 0\n" +
        "		/parameterCount 3\n" +
        "		/parameter-1 {\n" +
        "			/key 1952543846\n" +
        "			/showInPalette 4294967295\n" +
        "			/type (integer)\n" +
        "			/value 1\n" +
        "		}\n" +
        "		/parameter-2 {\n" +
        "			/key 1952539754\n" +
        "			/showInPalette 4294967295\n" +
        "			/type (integer)\n" +
        "			/value 2\n" +
        "		}\n" +
        "		/parameter-3 {\n" +
        "			/key 1952541806\n" +
        "			/showInPalette 4294967295\n" +
        "			/type (integer)\n" +
        "			/value 1\n" +
        "		}\n" +
        "	}\n" +
        "}\n" +
        "";

    doActionFromString("vertical_align", "align_type_vertically_centred", actionString)

};



/**
 * Create and execute an action using
 * the parameters supplied. The action
 * file will be removed afterwards.
 * @author Charu Rajput
 * @discussion https://community.adobe.com/t5/illustrator-discussions/quot-duplicate-layer-quot-command-in-script/m-p/13131761
 * @param {TextFrame} frame - an Illustrator TextFrame.
 */
function doActionFromString(name, setName, str) {

    var tmp = File(Folder.temp + '/' + setName + '.aia');
    tmp.open('w');
    tmp.write(str);
    tmp.close();
    app.loadAction(tmp);
    app.doScript(name, setName, false);
    app.unloadAction(setName, '');
    tmp.remove();

};

 

3 replies

Kurt Gold
Community Expert
Community Expert
August 13, 2022

Running this action with a script should work.

 

Align Type vertically centred

 

Or just use the action without a script.

 

m1b
Community Expert
m1bCommunity ExpertCorrect answer
Community Expert
August 14, 2022

Thanks @Kurt Gold you have hit the nail on the head!

 

@SA8888, here is a script that incorporates Kurt's action. Give it a try. I found the technique of saving the action file posted to the forum by @Charu Rajput. Thanks Charu!

- Mark

 

/**
 * Center text in selected text frame vertically.
 * @discussion https://community.adobe.com/t5/illustrator-discussions/creating-different-alignment/m-p/13131693
 */

// selection should be a text frame
centerSelectionVertically();



/**
 * Create and execute "Center vertically"
 * action on the selection.
 * @author KurtGold created this action.
 */
function centerSelectionVertically() {

    var actionString = "/version 3\n" +
        "/name [ 29\n" +
        "	616c69676e5f747970655f766572746963616c6c795f63656e74726564\n" +
        "]\n" +
        "/isOpen 1\n" +
        "/actionCount 1\n" +
        "/action-1 {\n" +
        "	/name [ 14\n" +
        "		766572746963616c5f616c69676e\n" +
        "	]\n" +
        "	/keyIndex 0\n" +
        "	/colorIndex 0\n" +
        "	/isOpen 1\n" +
        "	/eventCount 1\n" +
        "	/event-1 {\n" +
        "		/useRulersIn1stQuadrant 0\n" +
        "		/internalName (adobe_SLOAreaTextDialog)\n" +
        "		/localizedName [ 20\n" +
        "			466cc3a46368656e746578746f7074696f6e656e\n" +
        "		]\n" +
        "		/isOpen 1\n" +
        "		/isOn 1\n" +
        "		/hasDialog 0\n" +
        "		/parameterCount 3\n" +
        "		/parameter-1 {\n" +
        "			/key 1952543846\n" +
        "			/showInPalette 4294967295\n" +
        "			/type (integer)\n" +
        "			/value 1\n" +
        "		}\n" +
        "		/parameter-2 {\n" +
        "			/key 1952539754\n" +
        "			/showInPalette 4294967295\n" +
        "			/type (integer)\n" +
        "			/value 2\n" +
        "		}\n" +
        "		/parameter-3 {\n" +
        "			/key 1952541806\n" +
        "			/showInPalette 4294967295\n" +
        "			/type (integer)\n" +
        "			/value 1\n" +
        "		}\n" +
        "	}\n" +
        "}\n" +
        "";

    doActionFromString("vertical_align", "align_type_vertically_centred", actionString)

};



/**
 * Create and execute an action using
 * the parameters supplied. The action
 * file will be removed afterwards.
 * @author Charu Rajput
 * @discussion https://community.adobe.com/t5/illustrator-discussions/quot-duplicate-layer-quot-command-in-script/m-p/13131761
 * @param {TextFrame} frame - an Illustrator TextFrame.
 */
function doActionFromString(name, setName, str) {

    var tmp = File(Folder.temp + '/' + setName + '.aia');
    tmp.open('w');
    tmp.write(str);
    tmp.close();
    app.loadAction(tmp);
    app.doScript(name, setName, false);
    app.unloadAction(setName, '');
    tmp.remove();

};

 

SA8888Author
Known Participant
August 14, 2022

Thank you @m1b  for helping me out on this

SA8888Author
Known Participant
August 12, 2022

I want to align the textframe contents the same as above using a script.

m1b
Community Expert
Community Expert
August 13, 2022

Hi @SA8888, I looked into it and unfortunately the vertical alignment setting is not exposed via the scripting API.

 

As a poor substitute, you could try something like this script, which vertically centers the text simply by moving the whole textFrame up or down. Try it out to see what I mean.

- Mark

 

/**
 * @author m1b
 */
var doc = app.activeDocument,
    item = doc.selection[0],
    dup = item.duplicate().createOutline(),
    c1 = centerOfBounds(item.geometricBounds),
    c2 = centerOfBounds(dup.geometricBounds),
    d = differenceBetweenPoints(c1, c2);

dup.remove();
item.translate(0, d[1]);


function centerOfBounds(bounds) {

    var l = bounds[0],
        t = bounds[1],
        r = bounds[2],
        b = bounds[3],
        x = l + ((r - l) / 2),
        y = t + ((b - t) / 2);

    return [x, y];

};


function differenceBetweenPoints(p1, p2) {

    return [-(p2[0] - p1[0]), -(p2[1] - p1[1])];

};

 

Here are screenshots showing what it does. The black keyline around it is just for visual reference.

SA8888Author
Known Participant
August 13, 2022

Hi, @m1b thankyou for the script. I'm trying to place the contents vertically aligned inside a text frame, not on the whole page.

Like the screenshot below, I have used AreaType Option --> align vertical --> center. I wanted to try this same using the script

 

 

 

Community Manager
August 12, 2022

Hello @SA8888,

 

Thanks for reaching out. I would request you check the steps shared in this tutorial and check if it helps: https://www.youtube.com/watch?v=YgCD5tCftDM.

 

Looking forward to your response.

 

Thanks,

Anubhav

SA8888Author
Known Participant
August 12, 2022

Hi, @Anubhav M the above video didn't help what I needed. I want to align text to the centre of the frame using script.