Hello all,
For a personal hobby I’m automating some tasks I would normally have to do manually. I’ve made a script that calculates artboard guides to a specific size depending on the height and width.
But I need to have the guides on the artboard, not on the canvas due to obvious clutter reasons. Is this possible?
Code included for clarity.
#target photoshop
var doc = activeDocument;
var aLayer = doc.activeLayer;
var ab = artboard_rectangle(aLayer);
var aW = ab[2] - ab[0]; // Artboard width
var aH = ab[3] - ab[1]; // Artboard height
// Calculate the margin (height + width) / 55
var margin = (aW + aH) / 55;
// Place a vertical centered guide on the artboard
var centerX = ab[0] + aW / 2; // Horizontal center of the artboard
doc.guides.add(Direction.VERTICAL, centerX);
// Place a horizontal centered guide on the artboard
var centerY = ab[1] + aH / 2; // Vertical center of the artboard
doc.guides.add(Direction.HORIZONTAL, centerY);
// Place guides for top, bottom, left, and right based on the margin
doc.guides.add(Direction.HORIZONTAL, ab[1] + margin); // Top guide (relative to artboard)
doc.guides.add(Direction.HORIZONTAL, ab[3] - margin); // Bottom guide (relative to artboard)
doc.guides.add(Direction.VERTICAL, ab[0] + margin); // Left guide (relative to artboard)
doc.guides.add(Direction.VERTICAL, ab[2] - margin); // Right guide (relative to artboard)
// Rename the artboard
renameArtboard(aLayer, aW, aH);
function renameArtboard(layer, width, height) {
try {
var artboardName = "_" + width + "x" + height;
layer.name = artboardName;
// Removed alert for renaming
// alert('Artboard renamed to: ' + artboardName);
} catch (e) {
// alert("Error renaming artboard: " + e.message);
}
}
function artboard_rectangle(layer) {
try {
var r = new ActionReference();
r.putProperty(stringIDToTypeID("property"), stringIDToTypeID("artboard"));
r.putIdentifier(stringIDToTypeID("layer"), layer.id); // Target the artboard layer specifically
var d = executeActionGet(r).getObjectValue(stringIDToTypeID("artboard")).getObjectValue(stringIDToTypeID("artboardRect"));
var bounds = new Array();
bounds[0] = d.getUnitDoubleValue(stringIDToTypeID("left"));
bounds[1] = d.getUnitDoubleValue(stringIDToTypeID("top"));
bounds[2] = d.getUnitDoubleValue(stringIDToTypeID("right"));
bounds[3] = d.getUnitDoubleValue(stringIDToTypeID("bottom"));
return bounds;
} catch (e) {
alert(e);
}
}