Highlighted

[SCRIPTING] Create object style based on selected object

Enthusiast ,
Jul 30, 2020

Copy link to clipboard

Copied

Hello, there.

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

Adobe Community Professional
Correct answer by brianp311 | Adobe Community Professional

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. 

Topics

Scripting

Views

129

Likes

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more

[SCRIPTING] Create object style based on selected object

Enthusiast ,
Jul 30, 2020

Copy link to clipboard

Copied

Hello, there.

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

Adobe Community Professional
Correct answer by brianp311 | Adobe Community Professional

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. 

Topics

Scripting

Views

130

Likes

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
Adobe Community Professional ,
Jul 30, 2020

Copy link to clipboard

Copied

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?

Likes

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
Reply
Loading...
Enthusiast ,
Jul 30, 2020

Copy link to clipboard

Copied

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.

Likes

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
Reply
Loading...
Adobe Community Professional ,
Jul 30, 2020

Copy link to clipboard

Copied

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?

 

Likes

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
Reply
Loading...
Enthusiast ,
Jul 30, 2020

Copy link to clipboard

Copied

I need it to "save" anchoredObjectSettings in a style.

Likes

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
Reply
Loading...
Adobe Community Professional ,
Jul 30, 2020

Copy link to clipboard

Copied

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

 

Likes

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
Reply
Loading...
Enthusiast ,
Jul 30, 2020

Copy link to clipboard

Copied

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.

Likes

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
Reply
Loading...
Adobe Community Professional ,
Jul 30, 2020

Copy link to clipboard

Copied

Also looks like you'll need to dig in and iterate through the subproperties of: 

textFramePreferences
baselineFrameGridOptions
anchoredObjectSettings
textWrapPreferences
Edit: Or just copy the .properties object of each from one to the other.

Likes

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
Reply
Loading...
Adobe Community Professional ,
Jul 30, 2020

Copy link to clipboard

Copied

Was it an error or the catch $.writeln statement? It iterated through fine for me (Catalina 10.15.6, ID 15.1.1). I run through VS Code ESTK Debugger. 

Likes

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
Reply
Loading...
Enthusiast ,
Jul 30, 2020

Copy link to clipboard

Copied

No, it just stops in the try with the error I mentioned.

I'm on Win10 and ESTK.

Likes

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
Reply
Loading...
Adobe Community Professional ,
Jul 30, 2020

Copy link to clipboard

Copied

Huh. Strange. Does it happen if you try to run the script just through the Scripts Panel? 

Likes

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
Reply
Loading...
Enthusiast ,
Jul 30, 2020

Copy link to clipboard

Copied

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!

Likes

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
Reply
Loading...
Adobe Community Professional ,
Jul 30, 2020

Copy link to clipboard

Copied

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. 

Likes

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
Reply
Loading...
Enthusiast ,
Jul 30, 2020

Copy link to clipboard

Copied

Thank you so much again, Brian!!!

It worked (since the script runs directly from Scripts Panel).

Likes

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
Reply
Loading...