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

Script to auto select Align to Key Object

Explorer ,
Jan 15, 2020 Jan 15, 2020

When selecting two objects, is there a quicker to way to select Align to Key Object?

 

Sometimes lots of numbers get off, and while I have an action to center/align, I have to manually select Align to Key Object for every thing. There has to be a more efficient way to do this, but I can't seem to find a way to assign Align to Key Object to an action.Screen Shot 2020-01-15 at 12.30.07 PM.png

TOPICS
Draw and design , Scripting , Tools , Type
2.4K
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
LEGEND ,
Jan 16, 2020 Jan 16, 2020

I understand "Align to Key Object" is not directly scriptable. Its function must be emmulated in the script.
I use this script to align to a named object. It can be altered to use selection[1] as the key object (myAlignObj).

 

 

 

 

alignToObj(true, 0, 0, "FingerprintAlign");

function alignToObj(vertAlign, h_offset, v_offset, myAlignObj) {
    var aDoc = app.activeDocument;
    var sel = aDoc.selection;
    var keyCenter = getCenterPoint(aDoc.pageItems[myAlignObj]);
    var curItem, curCenter;
    const ALIGNMENT_PREFERENCE_VERTICAL = vertAlign;
    const ALIGNMENT_PREFERENCE_HORIZONTAL = true;

    for (var x = 0, len = sel.length; x < len; x++) {
        curItem = sel[x];
        if (ALIGNMENT_PREFERENCE_HORIZONTAL) {
            //align the object horizontally
            curItem.left = keyCenter.h - curItem.width / 2 + h_offset;
        }
        if (ALIGNMENT_PREFERENCE_VERTICAL) {
            //align the object vertically  
            curItem.top = keyCenter.v + curItem.height / 2 + v_offset;
        }
    }
}
function getCenterPoint(item) {
    return {
        "h": item.left + item.width / 2,
        "v": item.top - item.height / 2
    }
}

 

 

 

Edit: This version will align to the bottom of 2 selected items:

 

 

alignToObj1 (true, 0, 0);

function alignToObj1(vertAlign, h_offset, v_offset) {
    var aDoc = app.activeDocument;
    var sel = aDoc.selection;
    var keyCenter = getCenterPoint(aDoc.selection[1]);
    var curItem, curCenter;
    const ALIGNMENT_PREFERENCE_VERTICAL = vertAlign;
    const ALIGNMENT_PREFERENCE_HORIZONTAL = true;

    for (var x = 0, len = sel.length; x < len; x++) {
        curItem = sel[x];
        if (ALIGNMENT_PREFERENCE_HORIZONTAL) {
            //align the object horizontally
            curItem.left = keyCenter.h - curItem.width / 2 + h_offset;
        }
        if (ALIGNMENT_PREFERENCE_VERTICAL) {
            //align the object vertically  
            curItem.top = keyCenter.v + curItem.height / 2 + v_offset;
        }
    }
}

 

 

 

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
Mentor ,
Jan 17, 2020 Jan 17, 2020

just to expound on what @rcraighead posted, here's a version that's a bit more robust. has some error handling and a bit more flexibility perhaps. (you can decide at the time of function call whether you want to align vertically, horizontally, or both.

 

it aligns everything selected to the bottom object in the selection. This isn't my favorite way of determining the key object, but each different way has it's own problems. it would be better to determine the key object externally and then pass that object (or it's centerpoint) to the function. That way you could determine your key object any way you wanted, and then just align all of the selected items to that point instead of requiring the key object to be at a certain point in the stacking order.

 

//arguments 1 and 2 are vertical and horizontal align boolean preferences
//arguments 3 and 4 are predefined offset values.
    //for example: align to center, then offset 50pts to the right
//use the 
function alignToObj1(h_align, v_align, h_offset, v_offset) {
    if(!app.documents.length)
    {
        alert("Please open a document first.");
        return;
    }

    var aDoc = app.activeDocument;

    if(aDoc.selection.length < 2)
    {
        alert("Please select at least 2 objects.");
        return;
    }

    if(!h_offset) h_offset = 0;
    if(!v_offset) v_offset = 0;


    function getCenterPoint(item) {
        return {
            "h": item.left + item.width / 2,
            "v": item.top - item.height / 2
        }
    }
    
    var sel = aDoc.selection;
    var keyCenter = getCenterPoint(aDoc.selection[aDoc.selection.length-1]);
    var curItem, curCenter;

    for (var x = 0, len = sel.length-1; x < len; x++) {
        curItem = sel[x];
        if (h_align) {
            //align the object horizontally
            curItem.left = keyCenter.h - (curItem.width / 2) + h_offset;
        }
        if (v_align) {
            //align the object vertically  
            curItem.top = keyCenter.v + (curItem.height) / 2 + v_offset;
        }
    }
}

 

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
Explorer ,
Jan 20, 2020 Jan 20, 2020

edit: responded to the wrong post.

 

But for your script nothing happens when I run it. I don't get any error messages of any kind, even when I only have one object selected.

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
Explorer ,
Jan 20, 2020 Jan 20, 2020
LATEST

I'm getting an error when running your script that says:

 

Error 24: getCenterPoint is not a function.

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
Mentor ,
Jan 17, 2020 Jan 17, 2020

I just read your post again, and it seems like you weren't asking fro a script to do this...

 

If you're looking for a quick way to use the UI to change your alignment to "key object", all you need to do is select all of the items you want to align, then single click the object you want to be your key object. that will put a darker looking bounding box around the key object. at that point, any alignment options you choose will behave as though you have Align to Key Object checked.

 

Hope this helps.

 

edit. nevermind. i read your post again and realized i'm illiterate.

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
Explorer ,
Jan 20, 2020 Jan 20, 2020

No worries! It's sort of an uncommon thing I have to do, but when I do have to do it I have to do it a lot.

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