Creating an object style with a drop shadow
Hello,
I'm modifying an existing free script that adds measurements to a selected object. I'm trying to create an object style that has a drop shadow and I'm not sure how to write the proper syntax for creating this object style.
Here's the code where I'm creating the object style:
It works until I try to add in the part with the transparency settings, drop shadow mode, angle, offset and size. I know I have something in the syntax wrong.
// Make sure the object style "Shadow" exists
try {
app.activeDocument.objectStyles.add({name:"Shadow", strokeColor:"None", fillColor:"None", strokeWeight:0, transparencySettings.dropShadowSettings{mode:ShadowMode.drop, angle: 0.0083, xOffset: 0.08, yOffset: 0.08, size: 0.6 }});
} catch (_) { }
reportObjectStyle = app.activeDocument.objectStyles.item("Shadow");
Here's the full code:
if (app.documents.length && app.selection.length == 1 && app.selection[0].hasOwnProperty("paths"))
{
app.doScript(labelItem, ScriptLanguage.JAVASCRIPT, app.selection[0], UndoModes.ENTIRE_SCRIPT, "Label Image Size");
} else
{
alert ("No valid selection.\nPlease select one single object with the black arrow before running this script.");
}
function labelItem (item)
{
// Save current selected layer because adding one will make it active!
activeLayer = app.activeDocument.layoutWindows[0].activeLayer;
// Make sure the layer "ImageSize" exists
try {
app.activeDocument.layers.add({name:"ImageLabel", ignoreWrap:true, layerColor: [90,192,90], printable: false});
} catch (_) { }
reportLayer = app.activeDocument.layers.item("ImageLabel");
// Make sure the swatch "ImageGreen" exists
try {
app.activeDocument.colors.add({name:"ImageLabel", space:ColorSpace.RGB, colorValue:[90,192,90], colorModel:ColorModel.PROCESS});
} catch (_) { }
reportGreen = app.activeDocument.swatches.item("ImageLabel");
// Make sure the paragraph style "ImageSize" exists
try {
app.activeDocument.paragraphStyles.add({name:"ImageLabel", appliedFont:"Arial", fontStyle: "Bold", fillColor:"Paper", strokeColor:"Black", pointSize:22, leading:23, verticalJustification:VerticalJustification.topAlign});
} catch (_) { }
reportStyle = app.activeDocument.paragraphStyles.item("ImageLabel");
// Make sure the object style "Shadow" exists
try {
app.activeDocument.objectStyles.add({name:"Shadow", strokeColor:"None", fillColor:"None", strokeWeight:0, transparencySettings.dropShadowSettings{mode:ShadowMode.drop, angle: 0.0083, xOffset: 0.08, yOffset: 0.08, size: 0.6 }});
} catch (_) { }
reportObjectStyle = app.activeDocument.objectStyles.item("Shadow");
// Make sure measurement units are in inches
hmmu = app.activeDocument.viewPreferences.horizontalMeasurementUnits;
vmmu = app.activeDocument.viewPreferences.verticalMeasurementUnits;
app.activeDocument.viewPreferences.horizontalMeasurementUnits = MeasurementUnits.INCHES_DECIMAL;
app.activeDocument.viewPreferences.verticalMeasurementUnits = MeasurementUnits.INCHES_DECIMAL;
// .. and Smart Quotes is Off
sqt = app.activeDocument.textPreferences.typographersQuotes;
app.activeDocument.textPreferences.typographersQuotes = false;
// Save all of the previous settings to be able to restore them later :)
// fetch width and height for the numbers, left and bottom for the lines
gb = item.geometricBounds;
// gb now holds [ top, left, bottom, right ] values -- get shorthand values
top = gb[0];
left = gb[1];
bottom = gb[2];
right = gb[3];
width = Math.round(Math.abs(right - left)*1000)/1000;
height= Math.round(Math.abs(bottom - top)*1000)/1000;
square_inches = Math.round(Math.abs(width * height)*100)/100;
// Retrieve the current page.
// Actually, in CS5 and newer, page items have a property for this (parentPage),
// but I don't have that one near, and it's also nicer to make it work in CS4
// (and possibly even in older versions).
pg = item.parent;
while (1)
{
if (pg instanceof InsertionPoint)
pg = pg.parentTextframes[0];
if (pg instanceof Document || pg instanceof Spread || pg instanceof Page)
break;
pg = pg.parent;
}
// Draw text frame for square inches
// Put it at the left of the selection, 0.5" wide and 0.2" off the left side
frh = pg.textFrames.add(reportLayer, {geometricBounds:[ top + 0.0819, left + 0.0819, top + height, right ],
textFramePreferences: {strokeWeight:0, strokeColor:"None", verticalJustification: VerticalJustification.TOP_ALIGN,
ignoreWrap: true } } );
frh.applyObjectStyle(app.activeDocument.objectStyles.item("Shadow"));
//frh.applyObjectStyle("Shadow");
// ... and put text into it.
frh.insertionPoints[0].appliedParagraphStyle = reportStyle;
frh.contents = String(square_inches)+'"';
// Restore old layer selection
app.activeDocument.layoutWindows[0].activeLayer = activeLayer;
// Restore previous global settings
app.activeDocument.viewPreferences.horizontalMeasurementUnits = hmmu;
app.activeDocument.viewPreferences.verticalMeasurementUnits = vmmu;
app.activeDocument.textPreferences.typographersQuotes = sqt;
}
