Applescript Adobe Illustrator to Change Colors

I am attempting to take a path item that is on its own layer and change its color multiple times, saving it each time.
I was able to do it with javascript however can’t get it to work in applescript.

Javascript:
(Items are already selected that I intend to change.)


var iL = app.activeDocument.pathItems.length;
var colorSwatches = app.activeDocument.swatchGroups.getByName('newColors');
var allColors = colorSwatches.getAllSwatches();
var colorNames = Array();
for (var i = 0; i < allColors.length; i++){
    colorNames.push(allColors[i].name);
   }
for (x = 0; x<colorNames.length; x++)
    {
        var currentColor = allColors[x].name;
        for (i=0; i<iL; i++)
        {
            var myItem = app.activeDocument.pathItems[i];
            if(myItem.selected)
            {
                myItem.fillColor = app.activeDocument.swatches.getByName(currentColor).color;
            }
        }

(I then go on to save each one)

When trying to cross over into applescript I have the following so far:

 set iL to count every path item of document 1
               set colorSwatches to swatchgroup "newColors" of current document
               set allColors to get all swatches colorSwatches
               set swatchCount to count every item in allColors
               repeat with i from 1 to swatchCount
                    set currentSwatch to item i of allColors
                    repeat with x from 1 to iL
                        set myItem to path item i of document 1
           here is where I have no idea what I am doing... I can't figure out how to test if an item is selected
                    i
f myItem's has selected artwork is equal to true then
                            set fill color of myItem to {swatch:currentSwatch}
                        end if
                    end repeat

                    
               end repeat

Or better yet is there a way I can select every path item of a layer and change its color that way?
Something like the following:

set fill color of every path item of layer "art" of document 1 to {swatch:currentSwatch}

Thanks for any input!

Hi.You’re close, but you need the swatch’s color, rather than just the swatch itself.

tell application "Adobe Illustrator"'s document 1 to set layer "art"'s path items's fill color to (swatch "whatever"'s color)

Thank you!
I have looked and looked and never seen how to do this…
I’m getting closer!

Is there a way to get the swatch color name from the swatchgroup item list?
I have tried


set currentSwatch to name of item i of allColors


Then I could do the following:


tell application "Adobe Illustrator"'s document 1 to set layer "art"'s path items's fill color to (swatch currentSwatch color)

The problem I have is I wont always know the color names… and there could be 2… or 100

My version doesn’t have “swatchgroup” as a constant, and you’ll need to view the dictionary for your version (or Adobe’s scripting guide) to see if that’s valid syntax. If it isn’t, you can either provide a list of specific swatches to use (or not) in a conditional loop, or you can get them all.

set basePath to (path to desktop) as text --wherever you want to save (HFS path)
set indexVar to 1 --for name serialization

tell application "Adobe Illustrator"'s document 1
	repeat with aName in (get swatches's name)
		set (layer "art"'s path items)'s fill color to (swatch aName's color)
		set indexVar to indexVar + 1
		save it in basePath & "append to name" & indexVar
	end repeat
end tell

Marc I feel I am right on the edge of getting this.
I am able to get a running version from your snippet!

the swatchgroup just adds a bit more code.


set colorSwatches to swatchgroup "vinylColors" of current document
set allColors to get all swatches colorSwatches
set swatchCount to count every item in allColors
repeat with i from 1 to swatchCount
  set currentSwatch to item i of allColors
  set (layer "art"'s path items)'s fill color to (swatch currentSwatch's color)



Then finish up the saving part!
Thank you for your help…

I quickly realized that the item isn’t always a path item ofcourse… is there a way to change the fill color of every item of a layer no matter if it is a group//compound path?