Some other good info to take in consideration sorry for bothering. Just for you to have better ideas.
1) Distance between the three artboards will never change, that makes the position of (X,Y) a constant value to set the position of the objects, it will be a template that will not change or move artboards. Distance will be the same.
2) Objects will have a name, like "Car Front, Car Back, Car Board" so they need to position in a specific place in the board, the only thing it will change is what I am going to insert in those squares (yes, the squares are kind of a reference to insert images and using illustrator variables + a excel database to change the contents).
3) If possible, though the code is AMAZING and I already saved it with it's proper copyrights, I would like to those three squares (named for example Car Front, Car Back, Car Board) to always get the fixed location in the artboard, does not matter if I need to find the correct off set value to move.
Can you help me up please? Thank you SO MUCH!
Hi @AntonioPacheco, actually this way is simpler to code than the last script. Here you go. I've made an array that you can edit to include name, artboard number, position, and alignment points. See what you think.
- Mark
var itemsToPosition = [
{
name: 'Car Front',
artboard: 1,
artboardAlignmentPoint: topLeftPoint,
itemAlignmentPoint: topLeftPoint,
position: [10, 20]
},
{
name: 'Car Back',
artboard: 2,
artboardAlignmentPoint: topLeftPoint,
itemAlignmentPoint: topLeftPoint,
position: [50, 20],
},
{
name: 'Car Board',
artboard: 3,
artboardAlignmentPoint: topLeftPoint,
itemAlignmentPoint: topLeftPoint,
position: [70, 70],
},
];
/*
Alignment points:
topLeftPoint
topPoint
topRightPoint
leftPoint
centerPoint
rightPoint
bottomLeftPoint
bottomPoint
bottomRightPoint
*/
var items = positionNamedItems(
{
doc: app.activeDocument,
findWhat: itemsToPosition
}
);
// you can now do other things
// with the positioned items
// here, for example ...
app.selection = [];
items['Car Back'].selected = true;
/**
* Finds a page item and positions it
* in relation to the specified artboard.
* @author m1b
* @version 2022-09-30
* Example findWhat:
* [
* {
* name: 'Car Front',
* artboard: 1,
* artboardAlignmentPoint: topLeftPoint,
* itemAlignmentPoint: topLeftPoint,
* position: [10, 20]
* }
* ]
* Note: the alignmentPoints are functions that,
* given an item or artboard, return a point.
* @param {Object} options
* @param {Document} options.doc - an Illustrator Document.
* @param {Array<Object>} [options.findWhat] - search for this in item name (default: match all items).
* @returns {Array<PageItem>} the positioned items.
*/
function positionNamedItems(options) {
var doc = options.doc,
findWhat = options.findWhat,
results = {};
if (
doc == undefined
|| findWhat == undefined
)
return [];
// find named items
for (var i = 0; i < doc.pageItems.length; i++)
for (var j = 0, count = findWhat.length; j < findWhat.length; j++)
if (doc.pageItems[i].name.search(findWhat[j].name) != -1)
// found one!
findWhat[j].item = doc.pageItems[i];
// collect artboards
var abs = doc.artboards;
// position each item
for (var i = 0; i < findWhat.length; i++) {
if (findWhat[i].item == undefined) {
alert('No page item found for "' + findWhat[i].name + '".');
continue;
}
var item = findWhat[i].item,
ab = abs[findWhat[i].artboard - 1],
itemAlignmentPoint = findWhat[i].itemAlignmentPoint || topLeftPoint,
artboardAlignmentPoint = findWhat[i].artboardAlignmentPoint || topLeftPoint,
offset = findWhat[i].position;
// get the alignmentPoints align
var itemPoint = itemAlignmentPoint(item),
artboardPoint = artboardAlignmentPoint(ab);
// translation needed to align itemPoint with artboardPoint
var delta = [artboardPoint[0] - itemPoint[0], artboardPoint[1] - itemPoint[1]];
// move the item, including offset
item.translate(delta[0] + offset[0], delta[1] - offset[1]);
// add to results
results[findWhat[i].name] = item;
}
return results;
};
/**
* Returns bounds of item.
* @author m1b
* @version 2022-09-29
* @param {PageItem|Artboard} obj - a page item or artboard.
* @returns {Array<Number>} - [left, top, right, bottom]
*/
function getBounds(obj) {
var bounds;
if (obj.hasOwnProperty('visibleBounds'))
bounds = obj.visibleBounds;
else if (obj.hasOwnProperty('artboardRect'))
bounds = obj.artboardRect;
return bounds;
};
/**
* Returns the item's top left point.
* @author m1b
* @version 2022-09-29
* @param {PageItem|Artboard} obj - a page item or artboard.
* @returns {Array<Number>} - [x, y]
*/
function topLeftPoint(obj) {
var bounds = getBounds(obj);
if (bounds != undefined)
return [bounds[0], bounds[1]];
};
/**
* Returns the item's top point.
* @author m1b
* @version 2022-09-29
* @param {PageItem|Artboard} obj - a page item or artboard.
* @returns {Array<Number>} - [x, y]
*/
function topPoint(obj) {
var bounds = getBounds(obj);
if (bounds != undefined)
return [bounds[0] + (bounds[2] - bounds[0]) / 2, bounds[1]];
};
/**
* Returns the item's top right point.
* @author m1b
* @version 2022-09-29
* @param {PageItem|Artboard} obj - a page item or artboard.
* @returns {Array<Number>} - [x, y]
*/
function topRightPoint(obj) {
var bounds = getBounds(obj);
if (bounds != undefined)
return [bounds[2], bounds[1]];
};
/**
* Returns the item's left point.
* @author m1b
* @version 2022-09-29
* @param {PageItem|Artboard} obj - a page item or artboard.
* @returns {Array<Number>} - [x, y]
*/
function leftPoint(obj) {
var bounds = getBounds(obj);
if (bounds != undefined)
return [bounds[0], bounds[1] + (bounds[3] - bounds[1]) / 2];
};
/**
* Returns the item's center point.
* @author m1b
* @version 2022-09-29
* @param {PageItem|Artboard} obj - a page item or artboard.
* @returns {Array<Number>} - [x, y]
*/
function centerPoint(obj) {
var bounds = getBounds(obj);
if (bounds != undefined)
return [
bounds[0] + (bounds[2] - bounds[0]) / 2,
bounds[1] + (bounds[3] - bounds[1]) / 2,
];
};
/**
* Returns the item's right point.
* @author m1b
* @version 2022-09-29
* @param {PageItem|Artboard} obj - a page item or artboard.
* @returns {Array<Number>} - [x, y]
*/
function rightPoint(obj) {
var bounds = getBounds(obj);
if (bounds != undefined)
return [bounds[2], bounds[1] + (bounds[3] - bounds[1]) / 2];
};
/**
* Returns the item's bottom left point.
* @author m1b
* @version 2022-09-29
* @param {PageItem|Artboard} obj - a page item or artboard.
* @returns {Array<Number>} - [x, y]
*/
function bottomLeftPoint(obj) {
var bounds = getBounds(obj);
if (bounds != undefined)
return [bounds[0], bounds[3]];
};
/**
* Returns the item's bottom point.
* @author m1b
* @version 2022-09-29
* @param {PageItem|Artboard} obj - a page item or artboard.
* @returns {Array<Number>} - [x, y]
*/
function bottomPoint(obj) {
var bounds = getBounds(obj);
if (bounds != undefined)
return [bounds[0] + (bounds[2] - bounds[0]) / 2, bounds[3]];
};
/**
* Returns the item's bottom right point.
* @author m1b
* @version 2022-09-29
* @param {PageItem|Artboard} obj - a page item or artboard.
* @returns {Array<Number>} - [x, y]
*/
function bottomRightPoint(obj) {
var bounds = getBounds(obj);
if (bounds != undefined)
return [bounds[2], bounds[3]];
};
Edit: added check for missing items and improved results output.