Exit
  • Global community
    • Language:
      • Deutsch
      • English
      • Español
      • Français
      • Português
  • 日本語コミュニティ
  • 한국 커뮤니티
0

Creating an object style with a drop shadow

Community Beginner ,
Mar 19, 2020 Mar 19, 2020

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;

}

 

TOPICS
Scripting
1.6K
Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Enthusiast ,
Mar 19, 2020 Mar 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}}});
Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Mar 23, 2020 Mar 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 )

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Enthusiast ,
Mar 23, 2020 Mar 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.

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Beginner ,
Mar 04, 2024 Mar 04, 2024

Hello David,

Could you please help me writing script for the attached settings drop shadow.

Harish35864171yifm_0-1709606532751.png

 

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Engaged ,
Mar 04, 2024 Mar 04, 2024

Hi @Harish35864171yifm 
Try this:

var myDoc = app.activeDocument;

myDoc.viewPreferences.horizontalMeasurementUnits = MeasurementUnits.PIXELS;

myDoc.viewPreferences.verticalMeasurementUnits = MeasurementUnits.PIXELS;

var myPlainTextObjectStyle = myDoc.objectStyles.add();

with(myPlainTextObjectStyle)

{

name = "Shadow";

enableFill = false;

enableStroke = false;

enableStrokeAndCornerOptions = false;

myPlainTextObjectStyle.transparencySettings.dropShadowSettings.properties =

{

applied : true ,

mode : ShadowMode.DROP,

angle : 90 ,

opacity : 10 ,

blendMode : BlendMode.MULTIPLY ,

xOffset: "0px",

yOffset: "10px",

distance : "10px",

noise : 0,

spread: 20,

size: "20px"
};

}
Thanks,
Prabu
Design smarter, faster, and bolder with InDesign scripting.
Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Beginner ,
Mar 05, 2024 Mar 05, 2024

Hello Anantha Prabhu,
I am getting this error.

Harish35864171yifm_1-1709625585528.png

Kindly help on this issue.

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Beginner ,
Mar 05, 2024 Mar 05, 2024
Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Engaged ,
Mar 05, 2024 Mar 05, 2024
LATEST

@Harish35864171yifm 

Please use below code:

var myDoc = app.activeDocument;

myDoc.viewPreferences.horizontalMeasurementUnits = MeasurementUnits.PIXELS;

myDoc.viewPreferences.verticalMeasurementUnits = MeasurementUnits.PIXELS;

var objectStyleName = "Shadow";

var objectStyleShadow = myDoc.objectStyles.itemByName( objectStyleName );


var objectStyleProperties = 
{
	name : "Shadow" ,
	
	enableFill : false ,
	enableStroke : false ,
	
	transparencySettings :
	{
		dropShadowSettings :
		{
			applied : true ,

			mode : ShadowMode.DROP,

			angle : 90 ,

			opacity : 10 ,

			blendMode : BlendMode.MULTIPLY ,

			xOffset: "0px",

			yOffset: "10px",

			distance : "10px",

			noise : 0,

			spread: 20,

			size: "20px"
}
}
};


if( !objectStyleShadow.isValid )
{
	myDoc.objectStyles.add( objectStyleProperties );
};
// If object style exists, assign the properties that are defined above:
else
{
	objectStyleShadow.properties = objectStyleProperties ;
};

Thanks Uwe and David
Thanks,
Prabu
Design smarter, faster, and bolder with InDesign scripting.
Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines