How to make key object via jsx to perform "Align to Key Object"
Copy link to clipboard
Copied
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
Explore related tutorials & articles
Copy link to clipboard
Copied
no, not possible sorry.
Copy link to clipboard
Copied
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
Copy link to clipboard
Copied
can you change stacking order? for example, you can designate your key object to be the top most object in a selection
Copy link to clipboard
Copied
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?
Copy link to clipboard
Copied
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];
}
Copy link to clipboard
Copied
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.
Copy link to clipboard
Copied
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.
Copy link to clipboard
Copied
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.
}
Copy link to clipboard
Copied
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.
Copy link to clipboard
Copied
- 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();
Copy link to clipboard
Copied
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.
Copy link to clipboard
Copied
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
Copy link to clipboard
Copied
Je vous remercie! Ça marche. J'adore la fonction de numérotation.
Thank you, ALL, for the help.

