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

How can I apply any effects to a layer using scripts in Illustrator?

Community Beginner ,
Sep 13, 2020 Sep 13, 2020

Copy link to clipboard

Copied

I'm writing a script (in JSX) for Illustrator CC 2020, but there seems to be no way to apply an effect (like Gaussian blur) to a layer.

Of course it is possible to do this manually, but I'm looking for the right code to do this from my script.

Any suggestions?

 

NB: I searched all documents on https://www.adobe.com/devnet/illustrator/scripting.html, like the Scripting Guide, as well as the Illustrator Reference for Javascript. But nothing there... Neither on this forum, nor Adobe's website, Stackoverflow, Google, Bing, anywhere :'(

 

So far, I have this:

var myLayer = app.activeDocument.layers.getByName("Layer1");

// apply effect to myLayer

TOPICS
Scripting , SDK

Views

1.3K

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
community guidelines

correct answers 1 Correct answer

Guide , Sep 14, 2020 Sep 14, 2020

Effects are applied to selected paths, not layers. Once you've selected the paths in question, you can use executeMenuCommand("menuCommandString") to apply effects.  The snippet below selects the paths in the layer called "Layer 1" and applies a "Zig Zag" effect, the menuCommandString for which is "Live Zig Zag". 

 

var items = app.activeDocument.pageItems;
for (var i = 0; i < items.length; i++) {
  if (items[i].layer.name == "Layer 1") {
    items[i].selected = true;
  }
}
app.executeMenuCommand(
...

Votes

Translate

Translate
Adobe
Guide ,
Sep 14, 2020 Sep 14, 2020

Copy link to clipboard

Copied

Effects are applied to selected paths, not layers. Once you've selected the paths in question, you can use executeMenuCommand("menuCommandString") to apply effects.  The snippet below selects the paths in the layer called "Layer 1" and applies a "Zig Zag" effect, the menuCommandString for which is "Live Zig Zag". 

 

var items = app.activeDocument.pageItems;
for (var i = 0; i < items.length; i++) {
  if (items[i].layer.name == "Layer 1") {
    items[i].selected = true;
  }
}
app.executeMenuCommand("Live Zig Zag");

 

For a list of menuCommandStrings for executeMenuCommand(), see https://ten5963.wordpress.com/illustrator-ccver-22-menu-commands-list/  (It's unfortunate that you choose Gaussian blur, because the menuCommandString for it doesn't work, at least not for me in CS6.)

 

executeMenuCommand() is an inconvenient way to apply effects because it brings up a dialogue window for you to enter parameters.  Another (more complicated) way to apply effects is applyEffect(). If you wish to pursue this, see

Votes

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
community guidelines
Guide ,
Sep 14, 2020 Sep 14, 2020

Copy link to clipboard

Copied

This applies Gaussian blur to the paths in the layer called "Layer 1" using applyEffect.  For details, see the links above. 

var items = app.activeDocument.layers["Layer 1"].pathItems;
XMLstring = XMLstring = '<LiveEffect name="Adobe PSL Gaussian Blur"><Dict data="R blur 9.974 I PrevDres 300 "/></LiveEffect>';
for (var i = 0; i < items.length; i++) {
  items[i].applyEffect(XMLstring);
}

 

Votes

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
community guidelines
Community Expert ,
Sep 14, 2020 Sep 14, 2020

Copy link to clipboard

Copied

Effects (and graphic styles) can be applied at three different levels: at the object level, the group level and at the layer level. Just to clarify.

 

I'm not sure if it is possible to apply them at the layer level via scripting.

Votes

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
community guidelines
Community Beginner ,
Sep 15, 2020 Sep 15, 2020

Copy link to clipboard

Copied

LATEST

I tried the following:

 

var xmlstring = '<LiveEffect name="Adobe PSL Gaussian Blur"><Dict data="R blur 50 "/></LiveEffect>';
app.activeDocument.selection[0].applyEffect(xmlstring);

 

Now the effect gets applied fine, but the visual effect stays within the bounding box of the object (e.g. large blurs get cut off). Also, if iterating over all the items in the selection and applying the effect, the blur is calculated for each item separately. So that looks off if you just want the entire thing to have a blur. So we need to set it on the whole thing.

 

app.activeDocument.activeLayer.visible = false; // Works, but:
app.activeDocument.activeLayer.applyEffect(xmlstring); // does NOT work

 

Applying the effect directly to a layer didn't work unfortunately, but there is a workaround. Group everything on the layer inside a group (named "Test" for example) and use the following script on that group:

 

app.activeDocument.groupItems.getByName("Test").applyEffect(xmlstring);

 

This works great! No more individual blurs stacked on top of eachother. However, the blur is still clipped just a little outside the bounding box of the group. To prevent that, you could add a giant rectangle with opacity of 0, to make the group's bounding box larger.

Votes

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
community guidelines
Community Beginner ,
Sep 14, 2020 Sep 14, 2020

Copy link to clipboard

Copied

Thank you so much!!

 

I'm still baffled at how badly Adobe has documented their stuff (and the lack of tutorials they provide)!

Any idea where I could find the latest menu commands list (for Illustrator CC 2020)?

 

From your links and this post (https://community.adobe.com/t5/illustrator/get-menu-command-strings/m-p/8539169?page=1#M216220), I found a way to get the menu command:

(Explained here for future lost programmers 😉 )

1) Create a new Action set

2) Then choose "Insert Menu Item"

3) Search for the right menu item and add it to the action set.

4) Click "Save Actions" from the small menu in the Action panel

5) Open the saved .aia file in some text editor

6) Find the 'event', in my case it was under "/event-2 {"

7) In my case '/parameter-1' had a '/value' of "4c6976652041646f62652050534c20476175737369616e20426c7572"

8) Convert that value from HEX to ASCII (I used: https://www.rapidtables.com/convert/number/hex-to-ascii.html) and get the menu command. In this case "Live Adobe PSL Gaussian Blur". (Which worked when used in Illustrator CC 2020.)

 

I still get the popup dialog with the options to adjust. So if anyone knows how to set these and skip the dialog, that would be awesome !

Votes

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
community guidelines
Community Beginner ,
Sep 14, 2020 Sep 14, 2020

Copy link to clipboard

Copied

The '.applyEffect(xmlstring)' worked as well! (And without the dialog popups.)

Great hassle to find the right filter names and parameters however.. Hope Adobe can create a better reference for this. As well as include the above mentioned methods in their documentation (I checked, it's not there..).

 

Anyway, thanks a LOT for your example codes, references and the right keywords to search for!

Votes

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
community guidelines
Guide ,
Sep 15, 2020 Sep 15, 2020

Copy link to clipboard

Copied

You're welcome.  (And thank you for method of finding the menuCommandStrings; I did not know that.)

 

And, yes, this is nowhere to be found in the Adobe documents, because I think they are unofficial experiments (literally "off the books"). The fact that we know about them and are told about them on this forum by fellow users goes to show what a goldmine of help this forum is. It goes without saying (but I'm saying it): We are all grateful to the "oldtimers" here who discovered, shared and documented all of this.

 

 

Votes

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
community guidelines