Hi @Nicky G., oh yeah, that script had lots of bugs! (edit: fixed now) Anyway, no problem, try this version, which attempts to work as per your proposal. 🙂
- Mark
/**
* Change selected "pasteboard" guides to "artboard" guides
* when they intersect with the active artboard.
* @author m1b
* @discussion https://community.adobe.com/t5/illustrator-discussions/script-transfer-guides-to-artboard/m-p/13749896
*/
(function () {
var doc = app.activeDocument,
items = doc.selection,
a = doc.artboards[doc.artboards.getActiveArtboardIndex()].artboardRect,
counter = 0;
if (items.length == 0) {
alert('Please select one or more guides and try again.');
return;
}
for (var i = items.length - 1; i >= 0; i--) {
var item = items[i];
if (
item.guides !== true
|| item.locked == true
|| item.width * item.height > 0
|| !item.hasOwnProperty('pathPoints')
|| item.pathPoints.length !== 2
)
continue;
var b = item.geometricBounds;
if (boundsDoIntersect(a, b)) {
if (item.width == 0) {
setCornerPathPoint(item.pathPoints[0], [b[0], a[1]]);
setCornerPathPoint(item.pathPoints[1], [b[0], a[3]]);
}
else {
setCornerPathPoint(item.pathPoints[0], [a[0], b[1]]);
setCornerPathPoint(item.pathPoints[1], [a[2], b[1]]);
}
counter++;
}
};
app.redraw();
alert('Converted ' + counter + ' guides.');
})();
/**
* Sets a path point's position
* and converts to corner point.
* @param {PathPoint} pathPoint - an Illustrator PathPoint.
* @param {Array<Number>} p - [x, y].
*/
function setCornerPathPoint(pathPoint, p) {
pathPoint.anchor = p;
pathPoint.leftDirection = p;
pathPoint.rightDirection = p;
}
/**
* Returns true when bounds intersect.
* @param {Array[4]} b1 - bounds [l,t,r,b]
* @param {Array[4]} b2 - bounds [l,t,r,b]
* @returns {boolean}
*/
function boundsDoIntersect(b1, b2) {
return !(
b2[0] > b1[2]
|| b2[2] < b1[0]
|| b2[1] < b1[3]
|| b2[3] > b1[1]
);
};
Edit 2023-04-26: minor typo.