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?
1 Correct answer
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.
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?
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.
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?
Copy link to clipboard
Copied
I need it to "save" anchoredObjectSettings in a style.
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
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.
Copy link to clipboard
Copied
Also looks like you'll need to dig in and iterate through the subproperties of:
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.
Copy link to clipboard
Copied
No, it just stops in the try with the error I mentioned.
I'm on Win10 and ESTK.
Copy link to clipboard
Copied
Huh. Strange. Does it happen if you try to run the script just through the Scripts Panel?
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!
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.
Copy link to clipboard
Copied
Thank you so much again, Brian!!!
It worked (since the script runs directly from Scripts Panel).

