I edited a script from this forum (I think originally written by williamadowling) to align selection to bottom object. I want to modify it to add the "Bottom Object" to the selection by name so the user does not need to manually select it. The modification works… if I run the script twice. What am I doing wrong?
The starting selection is the "fingerprint" group. Here's a simplified AI file if it's helpful.
Thanks for your help.
var docRef = app.activeDocument; var sel = docRef.selection; var AlignObj = docRef.pageItems.getByName("PrintAlign"); AlignObj.selected = true; function alignToObj() { var keyCenter = getCenterPoint(AlignObj); var curItem,curCenter; const ALIGNMENT_PREFERENCE_VERTICAL = true; const ALIGNMENT_PREFERENCE_HORIZONTAL = true; for(var x=0, len = sel.length-1; x < len; x++) { curItem = sel[x]; 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}; } } alignToObj();