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

How to make key object via jsx to perform "Align to Key Object"

Contributor ,
Jan 07, 2018 Jan 07, 2018

Hi, Is there any way to make the object as a key object via script to perform "Align to key Object" in Illustrator. I need it urgently. Thanks in advance

TOPICS
Scripting
2.0K
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
Adobe
Community Expert ,
Jan 07, 2018 Jan 07, 2018

no, not possible sorry.

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
Contributor ,
Jan 08, 2018 Jan 08, 2018

Is der any way you can help out with writing script where we do not want to change position of one object. We can do this using position and coordinates

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
Community Expert ,
Jan 09, 2018 Jan 09, 2018

can you change stacking order? for example, you can designate your key object to be the top most object in a selection

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
LEGEND ,
Dec 21, 2018 Dec 21, 2018

Carlos, can you expound on this? I'd like to make the bottom selected object the "Key Object" for alignment. How can I do that?

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
Community Expert ,
Dec 21, 2018 Dec 21, 2018

manually.

if you have an array of objects you'd like to align, you can get the last item in the array, then update the position of each remaining item in the list to match your desired alignment preferences.

var array = [items,to,be,aligned];

var bottomObject = array[array.length-1];

for(each item you want to align)

{

     item.position = [enter new, coordinates];

}

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
LEGEND ,
Dec 21, 2018 Dec 21, 2018

Thanks, William. I'm trying to understand how to implement this.

In my case I will always have 2 objects preselected and need to set the bottom object as the "Key Object".

I am working with AI templates that can be customized. I've learn how to select items using "pageItems.getByName" so I only need to set the bottom object as the "Key Align Object".

Ultimately, I'd like to bypass the "align to Key Object" and move the single, selected object to an X,Y coordinate variable which is accessible, in the template, as a tab delimited text block.

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
Community Expert ,
Dec 21, 2018 Dec 21, 2018

thanks for jumping in william

are you ok with william's instruccions ray? or do you need further assistance?

you won't need to use any alignment buttons since you're not actually setting any objects as key. With scripting we're just appointing one as key.

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
LEGEND ,
Dec 22, 2018 Dec 22, 2018

No, I don't have a clue what to do with it.

var array = [items,to,be,aligned]; // what do I put between the brackets? I want it to be the current selection.

var bottomObject = array[array.length-1];

for(each item you want to align) // what do I put within the parenthesis? It should always be the top object.

{

     item.position = [enter new, coordinates]; // what do I put between the brackets? This should be the coordinates of the bottom object.

}

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
LEGEND ,
Dec 26, 2018 Dec 26, 2018

william, I'm back with questions:

var array = [items,to,be,aligned];

// how do I set the array as the current selection?

var bottomObject = array[array.length-1];

for(each item you want to align)

// I always want to align the top object. How do I update the script

     item.position = [enter new, coordinates];

// are these simply the "x", "y" coordinate numbers? Do they have to be converted to "Points"? Are they always in reference to the top/left corner of the artboard or can they be in reference to the "0" point set in the file?

}

I have been trying to find answers to these questions in the EXTK documentation without any success.

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
Community Expert ,
Dec 26, 2018 Dec 26, 2018
  • var array = app.activeDocument.selection;
  • for(var x=0, len = array.length-2 ;x < len; x++) //this loops every selected item except your key object. inside this loop you would set the item position to match the reference point of the key item
  • yes. you can use item.position to read or write the x/y position of any pageItem. You can also access the properties individually with the "left" and "top" properties. I tend to use these instead of position because i feel they're more intuitive. you don't have to remember which index of the array refers to what. it's in the name.

Here's a snippet that takes all the selected artwork and aligns it to the backmost item in the selection. you can easily toggle which alignment axes you'd like to use with the ALIGNMENT_PREFERENCE_[axis] constants at the top of the script. Be aware that clipped images can and will throw off the visual alignment of items because the overflow artwork in a clipping mask is considered a visible part of the artwork and is thus included in the left/top measurements.

function alignToBottomObject()

{

    var docRef = app.activeDocument;

    var sel = docRef.selection; //i changed this from array to be more descriptive

    const ALIGNMENT_PREFERENCE_VERTICAL = true;

    const ALIGNMENT_PREFERENCE_HORIZONTAL = true;

    if(sel.length <= 1)

    {

        alert("Please select at least 2 items.");

        return;

    }

    var bottomObject = sel[sel.length-1];

    var keyCenter = getCenterPoint(bottomObject);

    var curItem,curCenter;

    for(var x=0, len = sel.length-1; x < len; x++)

    {

        curItem = sel;

       

        if(ALIGNMENT_PREFERENCE_HORIZONTAL)

        {

            //align the object horizontally

            curItem.left = keyCenter.h - curItem.width/2;

        }

        if(ALIGNMENT_PREFERENCE_VERTICAL)

        {

            //align the object vertically

            curItem.top = keyCenter.v + curItem.height/2;

        }

    }

    function getCenterPoint(item)

    {

        return {"h":item.left + item.width/2, "v":item.top - item.height/2};

    }

}

alignToBottomObject();

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
LEGEND ,
Dec 26, 2018 Dec 26, 2018
LATEST

Thank you, William! I was getting close, but that doesn't work in scripting.

This does just what I need. I'll keep working to learn from it.

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
Advocate ,
Dec 22, 2018 Dec 22, 2018

Salut !

Pour une sélection d'objets, alignement (left) sur un objet clé

// JavaScript Document for Illustrator

function test() {

  var docRef = app.activeDocument;

    if (!selection.length) return;

    var sel = selection;

    var  currObj, VBounds, pointText, dec = 15, rep;

    var groupText = docRef.groupItems.add();

    for(var i = 0; i < selection.length; i++) {

      currObj =selection;

      VBounds =currObj.visibleBounds;

      pointText = groupText.textFrames.pointText([(VBounds[0]+VBounds[2])/2,VBounds[3]-dec]);

      pointText.contents = i;

      pointText.paragraphs[0].justification = Justification.CENTER

    }

    redraw();

    rep = prompt("N° de l'objet clé",0);

    sortVertically(sel,rep);

      function sortVertically(items,rg) {

          for(var n = 0; n < items.length; n++) {

            if (n != rg) items.left = items[rg].left;

          }

      }

    groupText.remove();

}

if (app.documents.length) {test();}

de elleere

PS si plus de demande, me contacter par Mail

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
LEGEND ,
Dec 22, 2018 Dec 22, 2018

Je vous remercie! Ça marche. J'adore la fonction de numérotation.

Thank you, ALL, for the help.

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