Well, with the help of @Stephen Marsh Here's a script that works. It doesn't have full error checking, so you have to make sure you have a selection made and an artboard selected.
#target photoshop
var doc = activeDocument
var abL = doc.activeLayer
// Store the current ruler units
var originalUnits = app.preferences.rulerUnits;
// Set units to pixels
app.preferences.rulerUnits = Units.PIXELS;
var activeSelection = null;
try { activeSelection = doc.selection.bounds } catch (e) { alert('A selection must be made')}
var abBounds = artboard_rectangle (abL)
var correctedOffset = [];
correctedOffset[0] = activeSelection[0].value-abBounds[0];
correctedOffset[1] = activeSelection[1].value-abBounds[1];
correctedOffset[2] = activeSelection[2].value-abBounds[0];
correctedOffset[3] = activeSelection[3].value-abBounds[1];
// Add guides to match bounds of selection
artboardGuides(correctedOffset[0], "vertical");
artboardGuides(correctedOffset[1], "horizontal");
artboardGuides(correctedOffset[2], "vertical");
artboardGuides(correctedOffset[3], "horizontal");
/*
artboardGuides(bounds[0].value, "vertical");
artboardGuides(bounds[1].value, "horizontal");
artboardGuides(bounds[2].value, "vertical");
artboardGuides(bounds[3].value, "horizontal");
*/
app.preferences.rulerUnits = originalUnits;
function artboardGuides(thePosition, theOrientation) {
var c2t = function (s) {
return app.charIDToTypeID(s);
};
var s2t = function (s) {
return app.stringIDToTypeID(s);
};
var descriptor = new ActionDescriptor();
var descriptor2 = new ActionDescriptor();
var reference = new ActionReference();
var reference2 = new ActionReference();
descriptor2.putUnitDouble(s2t("position"), s2t("pixelsUnit"), thePosition); // Position
descriptor2.putEnumerated(s2t("orientation"), s2t("orientation"), s2t(theOrientation)); // Horizontal or Vertical
descriptor2.putEnumerated(s2t("kind"), s2t("kind"), s2t("document"));
reference.putIndex(s2t("good"), 5);
descriptor2.putReference(s2t("null"), reference);
descriptor2.putInteger(c2t("GdCA"), 0); // Alpha?
descriptor2.putInteger(c2t("GdCR"), 128); // Red
descriptor2.putInteger(c2t("GdCG"), 128); // Green
descriptor2.putInteger(c2t("GdCB"), 128); // Blue
descriptor.putObject(s2t("new"), s2t("good"), descriptor2);
reference2.putClass(s2t("good"));
descriptor.putReference(s2t("null"), reference2);
descriptor.putEnumerated(s2t("guideTarget"), s2t("guideTarget"), s2t("guideTargetSelectedArtboard")); // Artboard guides
executeAction(s2t("make"), descriptor, DialogModes.NO);
}
function artboard_rectangle(layer) {
try {
var r = new ActionReference();
r.putProperty(stringIDToTypeID("property"), stringIDToTypeID("artboard"));
if (layer) r.putIdentifier(stringIDToTypeID("layer"), layer.id);
else r.putEnumerated(stringIDToTypeID("layer"), stringIDToTypeID("ordinal"), stringIDToTypeID("targetEnum"));
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("An artboard must be selected!");
}
}
function isArtboard() {
// modified from a script by greless with hints from jazz-y!
// returns true or false
try {
var d = new ActionDescriptor();
var r = new ActionReference();
r.putEnumerated(stringIDToTypeID('layer'), stringIDToTypeID('ordinal'), stringIDToTypeID('targetEnum'));
var options = executeActionGet(r);
return options.hasKey(stringIDToTypeID('artboard')); // test for the required key
} catch (e) {
//alert(e);
}
}
function artboardGuides(thePosition, theOrientation) {
var c2t = function (s) {
return app.charIDToTypeID(s);
};
var s2t = function (s) {
return app.stringIDToTypeID(s);
};
var descriptor = new ActionDescriptor();
var descriptor2 = new ActionDescriptor();
var reference = new ActionReference();
var reference2 = new ActionReference();
descriptor2.putUnitDouble(s2t("position"), s2t("pixelsUnit"), thePosition); // Position
descriptor2.putEnumerated(s2t("orientation"), s2t("orientation"), s2t(theOrientation)); // Horizontal or Vertical
descriptor2.putEnumerated(s2t("kind"), s2t("kind"), s2t("document"));
reference.putIndex(s2t("good"), 5);
descriptor2.putReference(s2t("null"), reference);
descriptor2.putInteger(c2t("GdCA"), 0); // Alpha?
descriptor2.putInteger(c2t("GdCR"), 128); // Red
descriptor2.putInteger(c2t("GdCG"), 128); // Green
descriptor2.putInteger(c2t("GdCB"), 128); // Blue
descriptor.putObject(s2t("new"), s2t("good"), descriptor2);
reference2.putClass(s2t("good"));
descriptor.putReference(s2t("null"), reference2);
descriptor.putEnumerated(s2t("guideTarget"), s2t("guideTarget"), s2t("guideTargetSelectedArtboard")); // Artboard guides
executeAction(s2t("make"), descriptor, DialogModes.NO);
}
... View more