Friday, July 21, 2017

#1 2009-09-06 10:21:42 pm

Jeffkr
Member
Registered: 2005-11-05
Posts: 458

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.

Applescript:

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


Filed under: Illustrator

Offline

 

#2 2009-09-07 03:37:02 pm

Mark67
Member
Registered: 2005-12-22
Posts: 780

Re: Fill an object with a swatch in Illustrator CS

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

Applescript:

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


Filed under: adobe

Offline

 

#3 2009-09-07 06:45:54 pm

Jeffkr
Member
Registered: 2005-11-05
Posts: 458

Re: Fill an object with a swatch in Illustrator CS

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

-Jeff

Offline

 

#4 2009-09-08 04:24:58 am

Mark67
Member
Registered: 2005-12-22
Posts: 780

Re: Fill an object with a swatch in Illustrator CS

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.

Offline

 

#5 2009-09-08 10:30:17 am

Jeffkr
Member
Registered: 2005-11-05
Posts: 458

Re: Fill an object with a swatch in Illustrator CS

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

Offline

 

#6 2009-09-08 11:56:24 am

Jerome
Member
From:: Ohio
Registered: 2003-06-14
Posts: 709
Website

Re: Fill an object with a swatch in Illustrator CS

You should be able to use the following:

Applescript:

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:

Applescript:

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


Filed under: adobe

Offline

 

#7 2009-09-08 04:18:28 pm

Jeffkr
Member
Registered: 2005-11-05
Posts: 458

Re: Fill an object with a swatch in Illustrator CS

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.

Applescript:

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


Filed under: Illustrator

Offline

 

#8 2009-09-09 02:41:09 am

Mark67
Member
Registered: 2005-12-22
Posts: 780

Re: Fill an object with a swatch in Illustrator CS

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.

Applescript:

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


Filed under: adobe

Offline

 

#9 2009-09-09 06:48:28 am

Jeffkr
Member
Registered: 2005-11-05
Posts: 458

Re: Fill an object with a swatch in Illustrator CS

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

Offline

 

#10 2017-07-06 07:09:22 pm

davidhmorgan
Member
From:: Sydney
Registered: 2014-08-20
Posts: 53

Re: Fill an object with a swatch in Illustrator CS

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:

Applescript:


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/Apples … 00302.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.

Last edited by davidhmorgan (2017-07-06 07:18:20 pm)

Offline

 

Board footer

Powered by FluxBB

RSS (new topics) RSS (active topics)