Skip to main content
Inspiring
December 9, 2022
Question

Applying a Graphic Style via a Script

  • December 9, 2022
  • 2 replies
  • 1152 views

I used your bit of script and its worked really well!  


 

  //Apply Outline to selected shape
    for (var i = 0; i < sel.length; i++) {   
        var gStyle = doc.graphicStyles.getByName("Outline");
        gStyle.applyTo(doc.selection[i]);
    }

 

I've noticed one thing that I can't explain. 
The graphic style is set to "Align Stroke to Outside"  but occasionally it sets the graphic style to "Align Stroke to Center" is there something I can change with my code to ensure it always applies as "Align Stroke to Outside"?

 

Thank you!

This topic has been closed for replies.

2 replies

Inspiring
December 9, 2022

Just jumping in here. When I automated applying styles and had the same issue, tt was on compound paths. I dont remember if it was when they were compound paths or if I converted them to compound paths it resolved the issue. You could test easily enough manually then automate that as a first step if it resolved it.

m1b
Community Expert
Community Expert
December 9, 2022

@mcs_dean, that rings true! @BryanPagenkopf can you confirm that your problem occurs with compoundPathItems? Maybe try making two separate path items with different settings for align stroke, then compound them? Some edge case?

m1b
Community Expert
Community Expert
December 9, 2022

Hi @BryanPagenkopf, I don't think that the scripting API provides access to the "Align Stroke to" options. Hope I am wrong. But also, I couldn't reproduce your problem occasionally it sets the graphic style to "Align Stroke to Center". Do you have a test file that demonstrates this problem? A possible solution is to reset the appearance and then re-apply the graphic style? Just thinking aloud.

- Mark

 

P.S. the line var gStyle = doc.graphicStyles.getByName("Outline") should be outside the loop, otherwise it unnecessarily gets evaluated for every item selected.

Inspiring
December 12, 2022

@m1b Thanks for the P.S. comment, made the change!   Funny thing about my issue...   I can't recreate it now either.  I've tried repeatedly and the graphic style is applying correctly. 

m1b
Community Expert
Community Expert
December 12, 2022

Please post back if you do find the elusive error case—I'm interested to nail it down. One thing to try if you haven't already is to quit Illustrator and VSCode and run again. Sometimes variables can be persistent in memory and this can have the result of making it look like everything is running fine until you next boot Illustrator and a variable is missing or different. - Mark