Skip to main content
Participant
March 19, 2020
Question

Creating an object style with a drop shadow

  • March 19, 2020
  • 1 reply
  • 1427 views

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;

}

 

This topic has been closed for replies.

1 reply

David Popham
Participating Frequently
March 19, 2020

Try this:

app.activeDocument.objectStyles.add ({name:"Shadow", strokeColor:"None", fillColor:"None", strokeWeight:0, transparencySettings:{dropShadowSettings:{mode:ShadowMode.drop, angle: .0083, xOffset: 0.08, yOffset: 0.08, size: 0.6}}});
Community Expert
March 23, 2020

Hi David,

thank you for the code.

Hm. I would set property enableFill to false. Also perhaps enableStroke as well. Otherwise the drop shadow will not be visible when the object style is applied. And perhaps this is also the reason why Melissa cannot see the drop shadow when the object style is applied?

 

var doc = app.documents[0];
var objectStyleName = "Shadow";
var objectStyleShadow = doc.objectStyles.itemByName( objectStyleName );

var objectStyleProperties = 
{
	name : "Shadow" ,
	
	enableFill : false ,
	enableStroke : false ,
	
	transparencySettings :
	{
		dropShadowSettings :
		{
			mode : ShadowMode.drop , 
			angle : .0083 , 
			xOffset : 0.08 , 
			yOffset : 0.08 , 
			size : 0.6
		}
	}
};


// Object style does NOT EXIST in target document:
if( !objectStyleShadow.isValid )
{
	doc.objectStyles.add( objectStyleProperties );
};
// If object style exists, assign the properties that are defined above:
else
{
	objectStyleShadow.properties = objectStyleProperties ;
};

 

Regards,
Uwe Laubender

( ACP )

David Popham
Participating Frequently
March 23, 2020

Hi Uwe,

 

Thanks for following up and providing an update to the script so that it is actually useful. All I did to the original code was change the syntax of

 

"transparencySettings.dropShadowSettings{"

 

 to

 

"transparencySettings:{dropShadowSettings:{"

 

 

in order get it to create the object style with the drop shadow rather than fail silently. I noticed the result was kind of wonky but didn't have time to investigate further. Your solution is very helpful for someone like me who is still transitioning to JavaScript after using AppleScript for many years.