Skip to main content
Inspiring
July 15, 2017
Answered

Illustator - select pathItems based on fill color

  • July 15, 2017
  • 1 reply
  • 5469 views

I am seeking a vbscript to select all pathItems based on its fillcolor.  Here's what I have but obviously wrong but I think I am on the right "path", ha!  I can't figure out how to say "if the pathitems fill color match my fillcolor, then select it and move on to the next"

Here's what I have:

Set FSO = CreateObject("Scripting.FileSystemObject")

Set App = CreateObject("Illustrator.Application")

Set SourceFolder = FSO.GetFolder("C:\SOCAL\Section_31\LandCover")

Set FillColor = CreateObject("Illustrator.RGBColor")

     FillColor.Red = 28

     FillColor.Green = 99

     FillColor.Blue = 48

If (App.Documents.Count > 0) Then

        Set DocPathItems = App.ActiveDocument.PathItems

        If (DocPathItems.Count > 0) Then

            For Each CurrentPath in DocPathItems

               If CurrentPath.PathItems = FillColor Then

                    CurrentPath.Selected = True

              Else

              End If

          Next

       End If

End If

This topic has been closed for replies.
Correct answer clutch-cargo

Oh, forgot to add a the script will entail several programs that  require vbscript.  But I must say this little adventure is getting more familiar with javascript    Now if you happen to attack what you have in vbscript I would be the last to complain, ha!


So here is the script so far.  It works great with the first Find Fill Color but at line

FindFill2 = App.ExecuteMenuCommand("Find Fill Color menu item")

the 2nd fill color changes color correctly but it never selects any matching paths.  Can you see where I am goin wrong?

Set App = CreateObject("Illustrator.Application")
Set FSO = CreateObject("Scripting.FileSystemObject")
Set SourceFolder = FSO.GetFolder("C:\SOCAL\Section_32\Land Cover")
Dim dFolder : dFolder = "C:\SOCAL\Section_32\Veg DXFs LC"
Dim FileRef, docRef, docRef2

For Each FileRef In SourceFolder.Files

      Set myDoc = App.Open(FileRef.Path,2)
      App.DoScript "LandCover-1", "Land Cover Auto"
      App.DoJavaScriptFile "C:\Custom_Trace.js"
      App.DoScript "LandCover-2", "Land Cover Auto"

'========================== fillcolor pine ========================

      Set iFillColor = CreateObject("Illustrator.RGBColor")
            iFillColor.Red     = 28
            iFillColor.Green  = 99
            iFillColor.Blue    = 48
 
      Set myDoc.DefaultFillColor = iFillColor
      FindFill = App.ExecuteMenuCommand("Find Fill Color menu item")
      Dim docSelected
      docSelected = App.DoJavaScript("var doc = app.activeDocument;doc.selection.length;")

' ========================= saving file =============================
' note - here I check if any paths selected, if Yes I save file.  If no, I just close file

      If docSelected > 0 Then
            App.DoScript "LandCover-3", "Land Cover Auto"
            dFile = dFolder + "\SOCAL_CK67_pineLC.dxf"
            Set docRef = App.ActiveDocument
            Set dxfExport = CreateObject("Illustrator.ExportOptionsAutoCAD")
            dxfExport.ExportFileFormat = 0    ' 0 = aiDXF
            docRef.Export dFile, 8, DXFexport
            docRef.Close 2        ' aiDoNotSaveChanges
            Set docRef = Nothing

      Else
            Set docRef = App.ActiveDocument
       docRef.Close 2
       Set docRef = Nothing

      End If

'========================== 2nd fillcolor scrub ========================

      Set myDoc2 = App.Open(FileRef.Path,2)
      App.DoScript "LandCover-1", "Land Cover Auto"
      App.DoJavaScriptFile "C:\Custom_Trace.js"
      App.DoScript "LandCover-2", "Land Cover Auto"
   
      Set iFillColor2 = CreateObject("Illustrator.RGBColor")
            iFillColor2.Red     = 204
            iFillColor2.Green = 186
            iFillColor2.Blue    = 124
 
      Set myDoc2.DefaultFillColor = iFillColor2
      FindFill2 = App.ExecuteMenuCommand("Find Fill Color menu item")
      Dim docSelected2
      docSelected2 = App.DoJavaScript("var doc = app.activeDocument;doc.selection.length;")

      If docSelected2 > 0 Then
            App.DoScript "LandCover-3", "Land Cover Auto"
            dFile = dFolder + "\SOCAL_CK67_scrubLC.dxf"
            Set docRef2 = App.ActiveDocument
            Set dxfExport = CreateObject("Illustrator.ExportOptionsAutoCAD")
            dxfExport.ExportFileFormat = 0    ' 0 = aiDXF
            docRef2.Export dFile, 8, DXFexport
            docRef2.Close 2       ' aiDoNotSaveChanges
            Set docRef2 = Nothing
 
      Else
            Set docRef2 = App.ActiveDocument
            docRef2.Close 2
       Set docRef2 = Nothing
 
      End If
Next

' Note - I would add more loops (5 total) to find five different fill colors.

1 reply

Inspiring
July 16, 2017

After further research I tried a different method but I get an Error Exception Occurred, 80020009, Source (null) at line 12.  It sounds like it is not finding the pathitems or layers?

Here's my latest attempt:

     Set iApp   = CreateObject("Illustrator.Application") 

     Set iDoc   = iApp.ActiveDocument 

     Set iLayer = iApp.ActiveDocument.Layers 

     Set iPath  = iApp.ActiveDocument.PathItems

     Set iFillColor = CreateObject("Illustrator.RGBColor")

          iFillColor.Red   = 28

          iFillColor.Green = 99

          iFillColor.Blue  = 48

 

    For i = iDoc.PathItems.Count To 1 Step -1          'loop thru all pathItems backwards 

        Set iPath = idoc.PathItems(i)                           'get path 

        If iPath.FillColor = iFillColor Then                    'check for color, not sure what to use for 'FillColor'?

               iPath.selected = True

        End If 

    Next

Inspiring
July 25, 2017

Ok, I guess no one around codes in VBscript.  So, if someone could create a JavaScript (or maybe even a VBA), perhaps I can study it and create a VBscript from it.  Again seeling to loop through all PathItems and select those that match a certain RGB color, like my example above.  Any help would be most appreciated.

pixxxelschubser
Community Expert
Community Expert
July 25, 2017

Hi clutch-cargo

Hmmh?

You never marks your solved questions as answered. Why not?

But anyway. Try this Javascript. It's not required to loop through all the items in your document.

var aDoc = app.activeDocument;

var rgbCol = new RGBColor();

rgbCol.red = 28;

rgbCol.green = 99;

rgbCol.blue = 48;

aDoc.defaultFillColor = rgbCol;

app.executeMenuCommand("Find Fill Color menu item");

Have fun

But be sure - if you adds a second or a third (and so on) fillcolor by Illustrators appearance panel - no script will find this additional fill colors.