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

[SCRIPTING] Create object style based on selected object

Enthusiast ,
Jul 30, 2020 Jul 30, 2020

Hello, there.

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

TOPICS
Scripting
3.8K
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

correct answers 1 Correct answer

Community Expert , Jul 30, 2020 Jul 30, 2020

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. 

Translate
Community Expert ,
Jul 30, 2020 Jul 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?

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 ,
Jul 30, 2020 Jul 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.

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 ,
Jul 30, 2020 Jul 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?

 

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 ,
Jul 30, 2020 Jul 30, 2020

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

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 ,
Jul 30, 2020 Jul 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

 

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 ,
Jul 30, 2020 Jul 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.

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 ,
Jul 30, 2020 Jul 30, 2020

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.
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 ,
Jul 30, 2020 Jul 30, 2020

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. 

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 ,
Jul 30, 2020 Jul 30, 2020

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

I'm on Win10 and ESTK.

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 ,
Jul 30, 2020 Jul 30, 2020

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

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 ,
Jul 30, 2020 Jul 30, 2020

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!

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 ,
Jul 30, 2020 Jul 30, 2020

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. 

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 ,
Jul 30, 2020 Jul 30, 2020
LATEST

Thank you so much again, Brian!!!

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

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