Fill an object with a swatch in Illustrator CS

I have seen a reference that it is possible to fill an object with a swatch in Illustrator CS, but I have yet to see how this can be done. Another problem may be that the gradient swatch I would like to apply to the object is within a swatch library”not the swatches palette. All I am trying to do is create an orange to red gradient that fills the entire artboard. I may be able to create two rectangles on each side of the artboard of different colors and then select all and create a blend. But I was hoping there was an easier method?

Maybe somebody has some other ideas?

So far this is what I have, which works fine for adding a solid color.

tell application "Illustrator CS"
	activate
	set docRef to the current document
	tell docRef
		set |page width| to width
		set |page height| to height
		set ruler origin to {0, 0}
		set |guides group| to make new group item at beginning
		set {|rectangle width|, |rectangle height|} to {612, 792}
		set |rectangle 1| to make new rectangle at beginning of |guides group| with properties ¬
			{name:"rectangle 1", position:{((|page width| - |rectangle width|) / 2), ((|page height| + |rectangle height|) / 2)}, width:{|rectangle width|}, height:{|rectangle height|}, fill color:{class:CMYK color info, cyan:0, magenta:50, yellow:100, black:0}}
	end tell
end tell

You could have your script create the gradient swatch then apply it to a path item like so.

tell application "Adobe Illustrator"
	activate
	set Doc_Ref to the current document
	tell Doc_Ref
		if not (exists gradient "My Gradient") then
			set This_Gradient to make new gradient at end with properties ¬
				{name:"My Gradient", gradient type:linear}
			set properties of gradient stop 1 of This_Gradient to ¬
				{midpoint:50, ramp point:0.0, color:{cyan:0, magenta:50, yellow:100, black:0}}
			set properties of gradient stop 2 of This_Gradient to ¬
				{midpoint:50, ramp point:100.0, color:{cyan:10, magenta:100, yellow:100, black:0}}
		end if
		if exists path item 1 then
			set fill color of path item 1 to {gradient:gradient "My Gradient"}
		end if
	end tell
end tell

Mark,
You just made my holiday weekend! Thank you so very much. Works like a charm.

-Jeff

The line “set fill color of path item 1 to {gradient:gradient “My Gradient”}” is taken from the scripting guide and does work. Although I could not find an example using just a regular flat color swatch. Also in the guide is “{pattern:pattern “My Pattern”}” however using this has always caused an out of memory issue no matter how simpler a pattern I use and therefore the script will always lock-up with a dialog. I have found that that using the syntax like so always works and don’t know why it wasn’t in the guide like this.

set fill color of path item 1 to color of swatch “My Swatch”
set fill color of path item 2 to color of swatch “My Gradient”
set fill color of path item 3 to color of swatch “My Pattern”

Two other thing to bare in mind are check the doc’s color space before creating swatches and check that they don’t already exist in the doc’s swatches.

Mark,
You were more than helpful. As bad as I am at scripting, I do try to absorb this information. My biggest issue with scripting is that it comes in cycles. I may not touch AppleScripting for about 3-4 months and then I need to refresh my memory when the time comes. Luckily there are folks like you that help me out in such binds.

Now I hate to bother you again, but you seem to know you stuff in regards to AppleScript and Illustrator. Maybe you know the answer: Is there a way to capture the current active layer as a variable and then make that layer active again after the script has finished?

For example, I am creating a backdrop layer and the swatch is filling the artboard. This layer is then locked via the script.

The problem I am having is getting the script to return to the layer that was once the active layer before the script was run.

I have searched, but no luck.

Should I create a new post with this question?

-Jeff

You should be able to use the following:

tell application "Adobe Illustrator"
	set x to current layer of document 1
	
	--Do Stuff
	
	set current layer of document 1 to x
end tell

Though if you are creating layers it would could effect the indexing of the reference, so to make sure this does not happen use the name of the layer:

tell application "Adobe Illustrator"
	set x to name of current layer of document 1
	
	--Do Stuff
	
	set current layer of document 1 to layer x of document 1
end tell

Thank you Jerome,
This is working great, and it was the last bit of the puzzle. However one minor issue still exists.

The only time my script errors is when the bottom-most layer is locked. Is there a “better” way to move a newly created layer to the bottom of the layers palette, even though the last layer is locked? I would hate to unlock all the layers first.

tell application "Illustrator CS"
	activate
	set x to name of current layer of document 1
	set docRef to document 1
	tell docRef
		if not (exists gradient "My Gradient") then
			set This_Gradient to make new gradient at end with properties ¬
				{name:"My Gradient", gradient type:linear}
			set properties of gradient stop 1 of This_Gradient to ¬
				{midpoint:50, ramp point:0.0, color:{cyan:0, magenta:50, yellow:100, black:0}}
			set properties of gradient stop 2 of This_Gradient to ¬
				{midpoint:50, ramp point:100.0, color:{cyan:10, magenta:100, yellow:100, black:0}}
		end if
		--display dialog x
		make new layer with properties {name:"backdrop"}
		move layer "backdrop" to behind last layer
		set ruler origin to {0, 0}
		set {|rectangle width|, |rectangle height|} to {612, 792}
		tell layer "backdrop"
			set |rectangle 1| to make new rectangle with properties ¬
				{name:"rectangle 1", position:{0, 792}, width:{|rectangle width|}, height:{|rectangle height|}, fill color:{class:CMYK color info, cyan:0, magenta:100, yellow:100, black:0}, stroked:false}
		end tell
		set fill color of |rectangle 1| to {gradient:gradient "My Gradient"}
		set locked of layer "backdrop" to true
	end tell
	set current layer of document 1 to layer x of document 1
end tell

The command ‘make new’ allows for a location using this will put the new layer ‘at end’ without the need to unlock the others. this works for me.

tell application "Adobe Illustrator"
	activate
	set x to name of current layer of document 1
	set docRef to document 1
	tell docRef
		if not (exists gradient "My Gradient") then
			set This_Gradient to make new gradient at end with properties ¬
				{name:"My Gradient", gradient type:linear}
			set properties of gradient stop 1 of This_Gradient to ¬
				{midpoint:50, ramp point:0.0, color:{cyan:0, magenta:50, yellow:100, black:0}}
			set properties of gradient stop 2 of This_Gradient to ¬
				{midpoint:50, ramp point:100.0, color:{cyan:10, magenta:100, yellow:100, black:0}}
		end if
		--display dialog x
		make new layer at end with properties {name:"backdrop"}
		-- move layer "backdrop" to behind last layer
		set ruler origin to {0, 0}
		set {|rectangle width|, |rectangle height|} to {612, 792}
		tell layer "backdrop"
			set |rectangle 1| to make new rectangle with properties ¬
				{name:"rectangle 1", position:{0, 792}, width:{|rectangle width|}, height:{|rectangle height|}, fill color:{class:CMYK color info, cyan:0, magenta:100, yellow:100, black:0}, stroked:false}
		end tell
		set fill color of |rectangle 1| to {gradient:gradient "My Gradient"}
		set locked of layer "backdrop" to true
	end tell
	set current layer of document 1 to layer x of document 1
end tell

OMG, I feel like such a dunce. All this time struggling over different options to make this work, when I never needed to move the layer in the first place! I just assumed that moving the layer was necessary since Illustrator by default (without scripting) creates a layer at the top.

I can’t tell you how glad I am that you both helped me with this. This is a very good script.

Thank you.

-Jeff

Hi Mark67,

referring to your script above for creating a new gradient swatch. This works perfectly but my issue is to do the reverse and obtain the fill color of a given path item. I’ve added some code to set a variable to the fill color of path item 1 after creating the gradient with your script and this works but I’ve found that it only works for gradient colors that are named swatches. If the gradient is unnamed, AppleScript gives an error, making it impossible to get the fill color of a path item filled with an unnamed gradient color.

So for example, if you wanted to gather the fill colors of every path item in a document and one path item has an unnamed gradient fill, you will get an error:


set EveryPathFillColor to {}
tell application "Adobe Illustrator"
	activate
	tell current document
		set EveryPathItem to every path item
		repeat with ThisPathItem in EveryPathItem
			set the end of EveryPathFillColor to fill color of ThisPathItem
		end repeat
	end tell
end tell
--Adobe Illustrator got an error: Can't get fill color of path item 1 of layer 1 of document 1.

I read on https://lists.apple.com/archives/Applescript-users/2003/Feb/msg00302.html (from a certain Shane Stanley in 2003) that this is probably a bug in the way Illustrator supports AppleScript. Is there any work around for this? I would like to be able to adjust the gradient stops in every gradient-filled path item in an Illustrator document.