Skip to main content
Known Participant
August 12, 2011
Answered

Setting draw modes through script?

  • August 12, 2011
  • 2 replies
  • 5731 views

Is there any way of setting draw mode through a script?

I can't find any info.

What I'd like is to set the draw mode to paint inside for a selected path through a script instead of clicking twice on the toolbar button or hit Shift+D twice.

This topic has been closed for replies.
Correct answer CarlosCanto

"it can be scripted"

Since I'm new to scripting for Illustrator I would be very happy for any guidance.


Hi here you go, this sample clips two objects. Let me know if you need to "draw" more than one inside.

Select two objects (the mask on top) before running the script.

#target Illustrator

//  script.name = drawInsideViaClippingMask.jsx;
//  script.description = makes a clipping mask, retaining fill/stroke, to simulate "draw inside";
//  script.required = two paths selected, top most path is the clipping mask;
//  script.parent = carlos canto // 8/16/11;
//  script.elegant = false;


if (app.documents.length > 0)
    {
        var idoc = app.activeDocument; // get active document;
        var sel = idoc.selection; // get selection
       
        if (sel.length==2) // continue only if 2 objects are selected
            {
                var igroup = idoc.groupItems.add(); // add a group that will be the clipping mask group

                var imask = sel[0]; // the mask is the object on top
                var ipath = sel[1]; // the "drawn object" is at the bottom

                var idup = imask.duplicate(); // duplicate the mask, to later get the fill and stroke to apply to the clipping path

                ipath.move(igroup, ElementPlacement.PLACEATBEGINNING); // add both path to the group
                imask.move (igroup, ElementPlacement.PLACEATBEGINNING);

                imask.clipping = true; // make the mask the clipping path
                igroup.clipped = true; // clip the everything in the group to the clipping mask
                   
                imask.fillColor = idup.fillColor; // add fill color, same as the dup
                imask.stroked = true; // stroke the mask
                imask.strokeWidth = idup.strokeWidth; // add stroke width, same as the dup
                imask.strokeColor = idup.strokeColor; // add stroke color, seme as the dup

                idup.remove(); // remove the duplicate
            }
        else // show this message if other than 2 objects are selected
            {
                alert("Select both, the Mask and the object that needs to be 'inside'.\rThe Mask needs to be the top most object.");
            }       
     }
else
    {
        alert ("there are no open documents");
    }

2 replies

Known Participant
August 20, 2011

I've edited the script to allow for multiple paths to be pasted inside simultaneously and also to allow paths to be added to an existing clipmap while still retaining fill and stroke.

Remember that the BOTTOM MOST path will be used as the clipping path.

Why? Because I like that workflow better and it was meant to imitate paste inside from Freehand.

Thanks for all your help Carlos Canto.

Here's a link to a short video showing how it works: http://www.govisit.se/pasteInside_for_Illustrator.swf

I have it attached to a quick key (Ctrl+Alt+V) but in the video I go to the scripts menu.

#target Illustrator
//  script.name = pasteInside.jsx
//  script.description = makes a clipping mask, retaining fill/stroke, to simulate "paste inside";
//  script.required = at least two paths selected, bottom most path will be the clipping mask;
//  script.parent = carlos canto // 2011-08-16;
//  edit to allow for multiple paths to be pasted inside simultaneously;
//  edit to allow paths to be added to an existing clipmap;
//  script.edit = jan sandstrom // 2011-08-21;

if (app.documents.length > 0)
    {
        var idoc = app.activeDocument; // get active document;
        var sel = idoc.selection; // get selection
        var ipath = new Array();
        var lastSelectionIndex = sel.length-1;

       if (sel.length>1)  // continue if more than 2 objects are selected
            {
                ipath = sel;
                // Lets see if the selection contains an existing clipping mask. If it does, we just move the new paths into the clipped group.
                if (ipath[ipath.length-1].clipped == true)
                {
                    var existingGroup = ipath[ipath.length-1];
                    ipath.pop(); // Get rid of the last one since that is the clipmap.   
                    for (var i = 0; i < (ipath.length); i++)
     {
                        ipath.move(existingGroup, ElementPlacement.PLACEATBEGINNING);
                    }
                    redraw(); // update view.
                }   
               else // If there is no existing clipping mask we do it this way.
                {
     var igroup = idoc.groupItems.add(); // add a group that will be the clipping mask group
     ipath = sel;

     var imask = sel[lastSelectionIndex]; // The bottom most curve will be the clipping mask.
     ipath.pop(); // Get rid of the last one since that is the clipmap.
     var idup = imask.duplicate(); // duplicate the mask, to later get the fill and stroke to apply to the clipping path

     for (var i = 0; i < (ipath.length); i++){
      ipath.move(igroup, ElementPlacement.PLACEATEND);
     }            
     imask.move(igroup, ElementPlacement.PLACEATBEGINNING); // add the clipping path to the group

     imask.clipping = true; // make the mask the clipping path
     igroup.clipped = true; // clip everything in the group to the clipping mask
                   
     imask.fillColor = idup.fillColor; // add fill color, same as the dup
     imask.stroked = true; // stroke the mask
     imask.strokeWidth = idup.strokeWidth; // add stroke width, same as the dup
     imask.strokeColor = idup.strokeColor; // add stroke color, seme as the dup
     idup.remove(); // remove the duplicate
     redraw(); // update view.
    }
   }
   else // show this message if less than 2 objects are selected
           {
                alert("Select both, the Mask and the objects that needs to be 'inside'.\rThe Mask needs to be the bottom most object.");
           }       
     }
else
    {
        alert ("There are no open documents!");
    }

CarlosCanto
Community Expert
Community Expert
August 21, 2011

you're welcome, glad to help

fruityth1ng
Inspiring
December 2, 2013

http://forums.adobe.com/message/5888844

Carlos, I used your script to do a similar thing.

CarlosCanto
Community Expert
Community Expert
August 12, 2011

it seems there's no way to change the drawing mode, what are you trying to accomplish after that? you could draw normally and make your clippling mask yourself

Known Participant
August 12, 2011

As an old Freehand user I miss the very good "paste inside" command and was thinking that maybe it would be possible to automate

the steps necessary in Illustrator to make it more easy to use.

This is what I was thinking:

First cut out the object you want to paste into the second "path".

Select the "path" that you want to paste the first object inside.

Run the script, that would make the selected path a draw inside form and then paste the first object in place.

Finally it would return to normal draw mode.

If it's not possible through scripting, would it be possible through a plugin with the SDK?

CarlosCanto
Community Expert
Community Expert
August 12, 2011

it can be scripted, before we do that, have you tried clipping masks?

select both objects and hit Ctrl+7, the bottom most object will end up "inside" the top most. The object on top will lose its fill and stroke, if you need it you can select it with the white arrow and re apply the stroke.

is it what you needed?