Skip to main content
Inspiring
March 1, 2012
Answered

removing paths

  • March 1, 2012
  • 5 replies
  • 3764 views

How can I select all paths with fill black 100% and remove them?

I tried the "script" below, but it doesn't give faults and the result stays the same (-> no remove of the items)

#target illustrator
var docRef=app.activeDocument;

for(i=0;i<docRef.pageItems.length; i++ ) {
    var pathRef = docRef.pageItems;
    if (pathRef.fillColor.black == 100) {
        pathRef.selected = true;
        pathRef.remove(i);
    };
};

I also tried: pathRef.remove(); but also here, the result is the same....

This topic has been closed for replies.
Correct answer Muppet Mark

Create on the existing file a rectangle with black fill.

The script must delete the path items with white fill and it must delete the text frames with no content.

Result after the script is only that rectangle with black fill.

The script described here below is for deleting stray points, empty textframes,... if that would be helpful for you.

#target illustrator
var idoc = app.activeDocument;
var straypoints = [];

for (i=0; i<idoc.pathItems.length; i++) {
    var ipath = idoc.pathItems;
    if (ipath.pathPoints.length==1)
        straypoints.push(ipath);
}

for (j=0; j<idoc.textFrames.length; j++) {
    var itext = idoc.textFrames;
    if (itext.textRange.length==0)
        straypoints.push(itext);
}

for (k=0; k<straypoints.length; k++)
    straypoints.remove();


Jesse, I had a look at that file and your white path items are 0% of a gray color… Easy enough to get rid of… The text frames are NOT being deleted because they do have content NOT empty… There are either 1 or two invisible characters in each Im not sure how to deal with this just yet ( need to check how to get the character values )… Making a rectangle and filling it black is easy stuff… Here I made one half the size of the doc… Not filled it just yet do you need 100% gray color or 0,0,0,100 CMYK? From this you can see I reset/reuse variables… You only need i,j,k and so on when looping inside of loops… The lines that use $.writeln( someValue ); are how I write back to the console for debugging…

#target Illustrator

function tidyMyFile() {

    if ( app.documents.length == 0 ) { return; }

    var i, count, doc, rec, uil;

    uil = app.userInteractionLevel;

    app.userInteractionLevel = UserInteractionLevel.DONTDISPLAYALERTS;

    doc = app.activeDocument;

    count = doc.pathItems.length;

    $.writeln( count );

    for ( i = count-1; i >= 0; i-- ) {

        if ( doc.pathItems.fillColor.gray == 0 ) { doc.pathItems.remove(); }

    };

    count = doc.textFrames.length;

    $.writeln( count );

    for ( i = count-1; i >= 0; i-- ) {

        if ( doc.textFrames.contents == ' ' || doc.textFrames.contents == '  ' ) { doc.textFrames.remove(); }

    };

    rec = doc.pathItems.rectangle( 0, 0, doc.width/2, -doc.height/2 );

    //rec.fillColor = ;

    app.redraw();

    app.userInteractionLevel = uil;

};

tidyMyFile();

Edit the contents were \s or \s\s

5 replies

ThinkingThings
Inspiring
March 1, 2012

Jesse: Are you in CMYK color mode? Check:   File > Document Color Mode > CMYK Color

If you are in RGB then the fix is different and ES might be bombing out without giving youan error (see provious post)

ThinkingThings
Inspiring
March 1, 2012

Sorry for all the one-sided posts, guys...

but this works now (at least for me):

#target Illustrator

var docRef = app.activeDocument;

for ( var i = docRef.pageItems.length-1; i >= 0; i-- ) {

   

    var pathRef = docRef.pageItems;

   

    if ( pathRef.fillColor.black == 100 ) { pathRef.remove(); app.redraw(); }

};

ThinkingThings
Inspiring
March 1, 2012

Just one change to Mark's code (as it was iterating one time less that it should have, I believe)...

        for ( var i = docRef.pageItems.length-1; i >= 0; i-- ) {

Other than that, I sure don't see why it doesn't work for me..

Inspiring
March 1, 2012

Yup it was… missed that… oops

Inspiring
March 2, 2012

Never got a link to a file… Did you send to the right muppet? That first one in the contributors is just a ghost in the machine…


Why does Adobe make it so complicated about sharing files, creating accounts, creating subaccounts, log in, additional log in,... Now, I hope that you can find the link Mark and other forum users! Next, I am forget how to upload a file again!

In Illustrator do "select all" and that rubbish must be removed by using a script. When I receive houndreds of ai-files and I must clean it up "by hand", then is that a very complicated case.

Many thanks everybody for helping mee.

https://acrobat.com/#d=Eez26yxk-L7I*M9Hw2pODA

ThinkingThings
Inspiring
March 1, 2012

Jesse:

Add (check) a few more levels of debugging in Edit > Preferences > Debugging and work your way back to where you are now - that should give you an idea of what's going on... I prefer to work in Visual Basic where I can see what's going on better -- FWIW -- (not to throw another curve), but the code below works in Visual Basic:

I tried Mark's codce in ES and it didn;t work for me; I'll look into it a little more if you don't find the answer.

========================

Imports AI = Illustrator

Module AdobeForum20120301

Public Sub RemoveBlackFilledBoxes()

Dim app As AI.Application = CreateObject("Illustrator.Application")

Dim aDoc = app.ActiveDocument

For Each pi In aDoc.PageItems

If pi.pageitemtype = 5 Then

Dim pathItem As AI.PathItem = pi

If pathItem.FillColor.Black = 100 Then

pathItem.Delete()

app.Redraw()

End If

End If

Next

End Sub

End Module

Inspiring
March 1, 2012

Jesse, first thing with removing items… Do this in reverse order otherwise when you reach the midpoint of the list i will be out of range… i is going up in value while the number of items is getting less…

pathRef.selected = true; // This line is not needed we do not select items in scripting…

pathRef.remove(i); // Should be…

pathRef.remove();

This worked here…

#target illustrator

var docRef = app.activeDocument;

for ( var i = docRef.pageItems.length-1; i > 0; i-- ) {

   

    var pathRef = docRef.pageItems;

   

    if ( pathRef.fillColor.black == 100 ) { pathRef.remove(); }

};

Inspiring
March 1, 2012

Mark,

Many thanks for the suggestions and explication.

I tried your version of the code, but Illustrator doesn't remove the defined path in Illustrator

The script:

#target illustrator

var docRef = app.activeDocument;

for (var i = docRef.pageItems.length-1; i > 0; i--) {

    var pathRef = docRef.pageItems;

   

    if (pathRef.fillColor.black==100){

        pathRef.remove();

    };

};

The path (rectangle with 100% black) stays in the document instead of removed.

Extendscript doesn't give errors or warnings.

Inspiring
March 1, 2012

Jesse… try and swap the work black for gray I susspect that you are dealing with grayscale color…?

#target illustrator

var docRef = app.activeDocument;

for ( var i = docRef.pageItems.length-1; i > 0; i-- ) {

   

    var pathRef = docRef.pageItems;

   

    if ( pathRef.fillColor.gray == 100 ) { pathRef.remove(); }

};

As you had defined it I would remove any CMYK that contained 100K