Skip to main content
lfcorullon13651490
Legend
July 30, 2020
Answered

[SCRIPTING] Create object style based on selected object

  • July 30, 2020
  • 2 replies
  • 4412 views

Hello, there.

Any idea on how to create an object style based on the settings of selected object with javascript?

This topic has been closed for replies.
Correct answer brian_p_dts

If I run it from the Scripts Panel, there is no error.

But the anchoredObjectSettings doesn't apply... =(
I think I will set each property one by one in the script code.

 

Thank you soooo much, Brian!


Yeah, you can check 
    

if (prop == "anchoredObjectSettings")
    aNewStyle[prop].properties = select[prop].properties;

 Or iterate through each property in case there are more conflicts. 

You'd have to do with the other settings/prefs I mentioned above. 

2 replies

brian_p_dts
Community Expert
Community Expert
July 30, 2020

You can try this. Might want to exclude certain properties that you don't want to carry over. Some won't carry over (ie if they are read only): 

 

var select = app.selection[0];
var aNewStyle = app.activeDocument.objectStyles.add();
var styleProps = aNewStyle.properties; 
for (var prop in styleProps) {
   if (select.hasOwnProperty(prop)) {
        try {
            aNewStyle[prop] = select[prop];
        } catch(e) {
            //likely a read-only prop
            $.writeln(e);
        }
   }
}
select.appliedObjectStyle = aNewStyle;
//don't know how you want to name it

 

lfcorullon13651490
Legend
July 30, 2020

Thank you so much, @brianp311.

I tried it and ESTK gave me an error saying "objectExportOptions" is a read only property.

So, I think the try/catch is not working as expected.

Steve Werner
Community Expert
Community Expert
July 30, 2020

You've just given a very good description of the best way to create an object style. Object styles are usually applied to text or graphic frames. Apply all the settings you want to use on a particular frame (fill, stroke, text frame options, paragraph styles (often applied with next style), effects, and so on). Then on the Object Styles panel, choose New Object Style... and you've created an object style based on the settings of the selected object. Applying that object style to another frame applies the same attributes.

 

Is there some reason that this is not working for you?

lfcorullon13651490
Legend
July 30, 2020

I want this in scripting.

If I do it manually, it works perfect.

But, even if I have the object selected, creating an objStyle with javascript simply creates a "blank" style.

Steve Werner
Community Expert
Community Expert
July 30, 2020

You probably should have written that in your initial description. I missed that the title included "[SCRIPTING]". Sorry, I'm not a scripter.

 

Why do you want a script to do it, by the way?