Skip to main content
Inspiring
Jul 27, 2018 12:30 AM UTC
Answered

Effect:Convert to Shape:Ellipse in JavaScript

Hello, so I am able to kind of read Javascript and have had success editing existing javascripts as well however I have not been able to effectively write one at this time.   Could someone please help me with the how to script the Effect:Convert to Shape:Ellipse?

The only thing I could find is:

     /** @ingroup MenuCommand

     Effect:Convert to Shape:Ellipse */

     #define kLiveEllipseCommandStr "Live Ellipse" 

I have an elaborate action that calls up that menu command about 4 times as well as the Effect:Convert to Shape:Rectangle but i have to enter different values each time it comes up and I would like to be able to script those variables.

Any Help and/or guidance would be very appreciated!

Thank you!!!

This topic is closed to new replies. Start a new post to keep the conversation going.
CarlosCanto
CarlosCantoCorrect answer
Community Expert
Jul 28, 2018 3:11 PM UTC

Hi Bryan, glad to hear you figured out some of your questions.

on your 1st question, you can loop through your selection and apply effect to each element.

// apply Ellipse Effect to selected object

// carlos canto

// https://forums.adobe.com/thread/2517904

var idoc = app.activeDocument;

var sel = idoc.selection;

var w = 10;

var h = 15;

// Convert to shape Ellipse, Relative 10 pts extra Width, 15 pts extra Height

xmlstring2 = '<LiveEffect name="Adobe Shape Effects"><Dict data="R RelHeight ' + h + ' R AbsWidth 36 R CornerRadius 9 R Absolute 0 R AbsHeight 36 I Shape 2 R RelWidth ' + w + ' "/></LiveEffect>';

for (var a=0; a<sel.length; a++) {

    sel.applyEffect(xmlstring2);

}

on the same note, you could change the xml string parameters (w & h) while you go through your selection if that's what you mean in your last comment.

10 replies

Inspiring
Jul 29, 2018 2:16 PM UTC

Below are all 4 of the Separate Effect Scripts:

1 - Offset Stroke

2 - Convert to Shape Rectangle

3 - Convert to Shape Ellipse

4 - Convert to Shape Ellipse

First Effect Works

var idoc = app.activeDocument;

var sel = idoc.selection[0];

var wh = -10.8;

// Offset Path LiveEffectEffect

xmlstring2 = '<LiveEffect name="Adobe Offset Path"><Dict data="R mlim 4 R ofst ' + wh + ' I jntp 2 "/></LiveEffect>';

for (var a=0; a<sel.length; a++) {

    sel.applyEffect(xmlstring2);

}

Second Effect Works

var idoc = app.activeDocument;

var sel = idoc.selection[0];

var wh = -10.8;

//Convert to Shape Rectangle  -.15" Relative

xmlstring2 = '<LiveEffect name="Adobe Shape Effects"><Dict data="I Shape 0 R AbsWidth 36 R AbsHeight 36 R Absolute 0 R CornerRadius 9 R RelHeight' + wh + 'R RelWidth' + wh + '"/></LiveEffect>';

for (var a=0; a<sel.length; a++) {

    sel.applyEffect(xmlstring2);

}

Third Effect Errors

var idoc = app.activeDocument;

var sel = idoc.selection[0];

var wh = 29.52;

//Convert to Shape Ellipse .41" Absolute

xmlstring2 = '<LiveEffect name="Adobe Shape Effects"><Dict data="I Shape 2 R AbsWidth ' + wh + ' R AbsHeight ' + wh + ' R Absolute 1 R CornerRadius 9 R RelHeight 18 R RelWidth 18 "/></LiveEffect>';

for (var a=0; a<sel.length; a++) {

    sel.applyEffect(xmlstring2);

}

Fourth Effect Errors

var idoc = app.activeDocument;

var sel = idoc.selection[0];

var wh = 10.944;

//Convert to Shape Ellipse .152" Absolute

xmlstring2 = '<LiveEffect name="Adobe Shape Effects"><Dict data="I Shape 2 R AbsWidth ' + wh + ' R AbsHeight ' + wh + ' R Absolute 1 R CornerRadius 9 R RelHeight 18 R RelWidth 18 "/></LiveEffect>';

for (var a=0; a<sel.length; a++) {

    sel.applyEffect(xmlstring2);

}

CarlosCanto
Community Expert
Community Expert
Jul 29, 2018 4:54 PM UTC

for a single object, we used

var sel = selection[0]; // the first object in the selection array

for multiple objects we should use

var sel = selection; // all selected objects

then in the loop we use

sel // each object in selection

Inspiring
Jul 30, 2018 9:14 PM UTC

Got it!

I ran the script through Adobe ExtendScript and it is highlighting this line

     var sel = idoc.selection[0];

as an issue.

Which would be why I am getting the error popup for

     sel.applyEffect(xmlstring2);

I removed the [0] from the line var sel = idoc.selection[0];

to read var sel = idoc.selection;

​and it worked!!!

Inspiring
Jul 28, 2018 11:01 AM UTC

There also seems to be a lot less of XML Code data when I apply the Convert to Shape: Rectangle with the JavaScript vs Manually

In the End all I really want to accomplish is to have Illustrator Automatically input the needed values rather than typing in the same values each time I run my action. So if there might be an easier way I'm open to any and all suggestions.

Translate
CarlosCanto
Community Expert
CarlosCantoCommunity ExpertCorrect answer
Community Expert
Jul 28, 2018 3:11 PM UTC

Hi Bryan, glad to hear you figured out some of your questions.

on your 1st question, you can loop through your selection and apply effect to each element.

// apply Ellipse Effect to selected object

// carlos canto

// https://forums.adobe.com/thread/2517904

var idoc = app.activeDocument;

var sel = idoc.selection;

var w = 10;

var h = 15;

// Convert to shape Ellipse, Relative 10 pts extra Width, 15 pts extra Height

xmlstring2 = '<LiveEffect name="Adobe Shape Effects"><Dict data="R RelHeight ' + h + ' R AbsWidth 36 R CornerRadius 9 R Absolute 0 R AbsHeight 36 I Shape 2 R RelWidth ' + w + ' "/></LiveEffect>';

for (var a=0; a<sel.length; a++) {

    sel.applyEffect(xmlstring2);

}

on the same note, you could change the xml string parameters (w & h) while you go through your selection if that's what you mean in your last comment.

Inspiring
Jul 29, 2018 2:45 AM UTC

Okay,

EDIT: I know this is a standard object loop but have no understanding of the -- sel -- that is the part that confuses me. I am getting an error though.

It worked for the Convert to Shape: Rectangle but not the Ellipse.

Below is what I have typed out.

// apply Ellipse Effect to selected object

// carlos canto

// https://forums.adobe.com/thread/2517904

var idoc = app.activeDocument;

var sel = idoc.selection[0];

var wh = 29.52;

//Convert to Shape Ellipse .41" Absolute

xmlstring2 = '<LiveEffect name="Adobe Shape Effects"><Dict data="I Shape 2 R AbsWidth ' + wh + ' R AbsHeight ' + wh + ' R Absolute 1 R CornerRadius 9 R RelHeight 18 R RelWidth 18 "/></LiveEffect>';

for (var a=0; a<sel.length; a++) {

    sel.applyEffect(xmlstring2);

}

//Obsolete Applies the effect to a single object

//sel.applyEffect(xmlstring2);

Inspiring
Jul 28, 2018 10:19 AM UTC

Okay so in order to get the XML data, as recommended I installed Illustrator CS6 and followed your instructions per your online Tutorial: https://aiscripts.com/apply-live-effect-applyeffect-liveeffectxml/   Which worked like a Charm!  Also, I believe I have figured out the answers to my 2nd and 3rd follow up questions, but the 1st question is still throwing me for a loop.

1. I cannot seem to get this to work on all the selected shapes at once, it only applies the effect to the last shape of the (ungrouped) selection.

2. I can accomplish with a simple calculation

3. The number following the word "Shape" indicates the shape selected 0 = Rectangle, 1 = Rounded Rectangle, and 2 = Ellipse

XML excerpts:

//

//Offset Path -.15" Inset

//

</ai:PaintFields>

            <ai:LiveEffects>

              <ai:LiveEffect index="0" isPre="1" major="1" minor="0" name="Adobe Offset Path">

                <ai:Dict data="R mlim 4 R ofst -10.8 I jntp 2 "/>

              </ai:LiveEffect>

            </ai:LiveEffects>

//

//Rectangle  -.15" Relative

//

</ai:PaintFields>

            <ai:LiveEffects>

              <ai:LiveEffect index="0" isPre="1" major="1" minor="0" name="Adobe Shape Effects">

                <ai:Dict data="I Shape 0 R AbsWidth 36 R AbsHeight 36 R Absolute 0 R CornerRadius 9 R RelHeight -10.8 R RelWidth -10.8 ">

                  <ai:Entry name="DisplayString" value="Rectangle" valueType="S"/>

                </ai:Dict>

              </ai:LiveEffect>

            </ai:LiveEffects>

          </ai:ArtStyle>

  //

//1st Ellipse .41" Absolute

//

</ai:PaintFields>

            <ai:LiveEffects>

              <ai:LiveEffect index="0" isPre="1" major="1" minor="0" name="Adobe Shape Effects">

                <ai:Dict data="I Shape 2 R AbsWidth 29.52 R AbsHeight 29.52 R Absolute 1 R CornerRadius 9 R RelHeight 18 R RelWidth 18 ">

                  <ai:Entry name="DisplayString" value="Ellipse" valueType="S"/>

                </ai:Dict>

              </ai:LiveEffect>

            </ai:LiveEffects>

          </ai:ArtStyle>

//

//2nd Ellipse .152" Absolute

//

</ai:PaintFields>

            <ai:LiveEffects>

              <ai:LiveEffect index="0" isPre="1" major="1" minor="0" name="Adobe Shape Effects">

                <ai:Dict data="I Shape 2 R AbsWidth 10.944 R AbsHeight 10.944 R Absolute 1 R CornerRadius 9 R RelHeight 18 R RelWidth 18 ">

                  <ai:Entry name="DisplayString" value="Ellipse" valueType="S"/>

                </ai:Dict>

              </ai:LiveEffect>

            </ai:LiveEffects>

          </ai:ArtStyle>

Translate
CarlosCanto
Community Expert
Community Expert
Jul 27, 2018 5:50 AM UTC

here's something to get you started, you can have your action run the script

select an object before running

// apply Ellipse Effect to selected object

// carlos canto

// https://forums.adobe.com/thread/2517904

var idoc = app.activeDocument;

var sel = idoc.selection[0];

var w = 10;

var h = 15;

// Convert to shape Ellipse, Relative 10 pts extra Width, 15 pts extra Height

xmlstring2 = '<LiveEffect name="Adobe Shape Effects"><Dict data="R RelHeight ' + h + ' R AbsWidth 36 R CornerRadius 9 R Absolute 0 R AbsHeight 36 I Shape 2 R RelWidth ' + w + ' "/></LiveEffect>';

sel.applyEffect(xmlstring2);

Translate
Inspiring
Jul 27, 2018 10:26 AM UTC

That's more than I've been able to accomplish thank you! 

3 follow up questions if I may and sorry for my ignorance:

     1. Will this work on multiple selected items at once  (2 - 200)?

     2. How would this code vary to have the measurements in Inches?

     3. How would this code vary for the Convert to Shape: Rectangle?

Translate