Skip to main content
Inspiring
May 17, 2024
Answered

Create script for adding shapes with specified colour on specified layers.

  • May 17, 2024
  • 3 replies
  • 1333 views

Hello everyone, feel free to shoot me down if this is too crazy but I am looking for some help with a script. 

The script would:
1. Set the document to have 9mm bleed

2. Create five 6mm circles, that have a spot black fill called 'Black'
3. Position dots so one is in the top left, top right, bottom left, bottom right, and center left (touching the edge of 9mm bleed).
4. Create five 6mm circles with no fill, but a 0.25pt stroke set to overprint, using a spot green called 'Registration'. 
5. Position these in the same place as the black circles, (over the top) but seperate them into a layer called 'Overprint'.

If any magic users out there have any ideas, I would be very thankful - its taking up so much time when faced with a large quantity of files. 

Many thanks.

This topic has been closed for replies.
Correct answer RobOctopus

From what i've gathered, there is not a clean and easy way to script the setting of a documents bleed. If someone else knows an easy way to implement feel free to add. below is a script that will create the 10 circles you need, 5 filled, and 5 stroked. I would suggest using a different name than "black" for your swatch. it gets messy since there is already a default black swatch.

/*get the artboards dimensions for placing the circles*/
var ArtboardRect = app.activeDocument.artboards[0].artboardRect
var ALeft = ArtboardRect[0]
var ATop = ArtboardRect[1]
var ARight = ArtboardRect[2]
var ABottom = ArtboardRect[3]
var AWidth = ArtboardRect[2]-ArtboardRect[0]
var AHeight = ArtboardRect[3]-ArtboardRect[1]

/*
change the bleed value here to change where the circles need to be placed
change the CircleWidthHeight value to change how large the circles are. they are currently 6mm converted to pts.
look up the UnitValue function to edit these
*/
var Bleed = UnitValue(9,"mm").as("pt")
var CircleWidthHeight = UnitValue(6,"mm").as("pt")
/* adds the spot colors. will probably fail with the black as the name is not unique*/
AddSpot("Black", 0, 0, 0, 100, true)
AddSpot("Registration", 100, 0, 100, 0, true)

/*create new layers for your new circles*/
var FilledCircles = FindLayerCreate(app.activeDocument, "BLACK CIRCLES")
var StrokedCircles = FindLayerCreate(app.activeDocument, "OVERPRINT")

/*here we set the values for making the stroked circles*/
var StrokeValues = {
    stroked: true,
    sWidth: 0.25,
    sColor: app.activeDocument.swatches.getByName("Registration").color,
    sOverprint: true,
    filled: false,
    fColor: new NoColor(),
    fOverprint: false,
    targetLayer: StrokedCircles
}

/*make all 5 stroked circles*/
MakeCircle(ATop+Bleed,ALeft-Bleed,CircleWidthHeight,StrokeValues)
MakeCircle(ATop+Bleed,ARight+Bleed-CircleWidthHeight,CircleWidthHeight,StrokeValues)
MakeCircle(ABottom-Bleed+CircleWidthHeight,ARight+Bleed-CircleWidthHeight,CircleWidthHeight,StrokeValues)
MakeCircle(ABottom-Bleed+CircleWidthHeight,ALeft-Bleed,CircleWidthHeight,StrokeValues)
MakeCircle((ABottom/2)+(CircleWidthHeight/2),ALeft-Bleed,CircleWidthHeight,StrokeValues)

/*set the values for the filled circles*/
var FillValues = {
    stroked: false,
    sWidth: 0,
    sColor: new NoColor(),
    sOverprint: false,
    filled: true,
    fColor: app.activeDocument.swatches.getByName("Black").color,
    fOverprint: false,
    targetLayer: FilledCircles
}

/*make all 5 filled circles*/
MakeCircle(ATop+Bleed,ALeft-Bleed,CircleWidthHeight,FillValues)
MakeCircle(ATop+Bleed,ARight+Bleed-CircleWidthHeight,CircleWidthHeight,FillValues)
MakeCircle(ABottom-Bleed+CircleWidthHeight,ARight+Bleed-CircleWidthHeight,CircleWidthHeight,FillValues)
MakeCircle(ABottom-Bleed+CircleWidthHeight,ALeft-Bleed,CircleWidthHeight,FillValues)
MakeCircle((ABottom/2)+(CircleWidthHeight/2),ALeft-Bleed,CircleWidthHeight,FillValues)


function MakeCircle(Top,Left,CWH,StrokeFillValues){
    var newCircle = StrokeFillValues.targetLayer.pathItems.ellipse(Top,Left,CWH,CWH,false)
    newCircle.stroked = StrokeFillValues.stroked
    newCircle.filled = StrokeFillValues.filled

    newCircle.strokeWidth = StrokeFillValues.sWidth
    newCircle.strokeColor = StrokeFillValues.sColor
    newCircle.strokeOverprint = StrokeFillValues.sOverprint

    newCircle.fillColor = StrokeFillValues.fColor
    newCircle.fillOverprint = StrokeFillValues.fOverprint
}


/**
 * Adds a spot swatch based on CMYK information
 * @9397041 {Object|String} NewColor - if its an object of color information will convert otherwise assumes its a string
 * @9397041 {Number} c - the value of Cyan 0-100
 * @9397041 {Number} m - the value of Magenta 0-100
 * @9397041 {Number} y - the value of Yellow 0-100
 * @9397041 {Number} k - the value of Black 0-100
 * @9397041 {Boolean} SpotProcess - true/false if the spot color is a process or not. if true will be spot
 */
function AddSpot(NewColor, c, m, y, k, SpotProcess) {
    SpotProcess = (SpotProcess === 'true') /*convert to bool*/
    if (typeof NewColor == "object") {
        var name = NewColor[0]
        var c = NewColor[1]
        var m = NewColor[2]
        var y = NewColor[3]
        var k = NewColor[4]
    } else if (typeof NewColor == "string") {
        name = NewColor
    } else {
        alert(NewColor + " is not in a recognizable format for coding")
    }
    try {
        swatch = app.activeDocument.swatches[name]; // if swatch exists....  
        //AddSpot (name+=' 1', c, m, y, k); // ...add 1 to swatch name  
    } catch (e) {
        var newColor = new CMYKColor();
        newColor.cyan = c;
        newColor.magenta = m;
        newColor.yellow = y;
        newColor.black = k;
        var newSpot = app.activeDocument.spots.add()
        newSpot.colorType = SpotProcess ? ColorModel.SPOT : ColorModel.PROCESS;
        newSpot.name = name;
        newSpot.color = newColor;
    }
}

function FindLayerCreate(ParentLayer, ChildLayer) {
    try {
        var NewLayer = ParentLayer.layers[ChildLayer]
    } catch (e) {
        var NewLayer = ParentLayer.layers.add()
        NewLayer.name = ChildLayer
    }
    return NewLayer
}

3 replies

Kazem Rahimi
Inspiring
May 18, 2024

Hello, You can get the result with create and using an Action instead of Script. it would work so easier.

Aidan_CAuthor
Inspiring
May 23, 2024

Unfortunately had quite a few issues with the actions remembering swatch colours - and doesn't seem to want to make rename layers etc. Thanks for the help though.

RobOctopus
RobOctopusCorrect answer
Inspiring
May 17, 2024

From what i've gathered, there is not a clean and easy way to script the setting of a documents bleed. If someone else knows an easy way to implement feel free to add. below is a script that will create the 10 circles you need, 5 filled, and 5 stroked. I would suggest using a different name than "black" for your swatch. it gets messy since there is already a default black swatch.

/*get the artboards dimensions for placing the circles*/
var ArtboardRect = app.activeDocument.artboards[0].artboardRect
var ALeft = ArtboardRect[0]
var ATop = ArtboardRect[1]
var ARight = ArtboardRect[2]
var ABottom = ArtboardRect[3]
var AWidth = ArtboardRect[2]-ArtboardRect[0]
var AHeight = ArtboardRect[3]-ArtboardRect[1]

/*
change the bleed value here to change where the circles need to be placed
change the CircleWidthHeight value to change how large the circles are. they are currently 6mm converted to pts.
look up the UnitValue function to edit these
*/
var Bleed = UnitValue(9,"mm").as("pt")
var CircleWidthHeight = UnitValue(6,"mm").as("pt")
/* adds the spot colors. will probably fail with the black as the name is not unique*/
AddSpot("Black", 0, 0, 0, 100, true)
AddSpot("Registration", 100, 0, 100, 0, true)

/*create new layers for your new circles*/
var FilledCircles = FindLayerCreate(app.activeDocument, "BLACK CIRCLES")
var StrokedCircles = FindLayerCreate(app.activeDocument, "OVERPRINT")

/*here we set the values for making the stroked circles*/
var StrokeValues = {
    stroked: true,
    sWidth: 0.25,
    sColor: app.activeDocument.swatches.getByName("Registration").color,
    sOverprint: true,
    filled: false,
    fColor: new NoColor(),
    fOverprint: false,
    targetLayer: StrokedCircles
}

/*make all 5 stroked circles*/
MakeCircle(ATop+Bleed,ALeft-Bleed,CircleWidthHeight,StrokeValues)
MakeCircle(ATop+Bleed,ARight+Bleed-CircleWidthHeight,CircleWidthHeight,StrokeValues)
MakeCircle(ABottom-Bleed+CircleWidthHeight,ARight+Bleed-CircleWidthHeight,CircleWidthHeight,StrokeValues)
MakeCircle(ABottom-Bleed+CircleWidthHeight,ALeft-Bleed,CircleWidthHeight,StrokeValues)
MakeCircle((ABottom/2)+(CircleWidthHeight/2),ALeft-Bleed,CircleWidthHeight,StrokeValues)

/*set the values for the filled circles*/
var FillValues = {
    stroked: false,
    sWidth: 0,
    sColor: new NoColor(),
    sOverprint: false,
    filled: true,
    fColor: app.activeDocument.swatches.getByName("Black").color,
    fOverprint: false,
    targetLayer: FilledCircles
}

/*make all 5 filled circles*/
MakeCircle(ATop+Bleed,ALeft-Bleed,CircleWidthHeight,FillValues)
MakeCircle(ATop+Bleed,ARight+Bleed-CircleWidthHeight,CircleWidthHeight,FillValues)
MakeCircle(ABottom-Bleed+CircleWidthHeight,ARight+Bleed-CircleWidthHeight,CircleWidthHeight,FillValues)
MakeCircle(ABottom-Bleed+CircleWidthHeight,ALeft-Bleed,CircleWidthHeight,FillValues)
MakeCircle((ABottom/2)+(CircleWidthHeight/2),ALeft-Bleed,CircleWidthHeight,FillValues)


function MakeCircle(Top,Left,CWH,StrokeFillValues){
    var newCircle = StrokeFillValues.targetLayer.pathItems.ellipse(Top,Left,CWH,CWH,false)
    newCircle.stroked = StrokeFillValues.stroked
    newCircle.filled = StrokeFillValues.filled

    newCircle.strokeWidth = StrokeFillValues.sWidth
    newCircle.strokeColor = StrokeFillValues.sColor
    newCircle.strokeOverprint = StrokeFillValues.sOverprint

    newCircle.fillColor = StrokeFillValues.fColor
    newCircle.fillOverprint = StrokeFillValues.fOverprint
}


/**
 * Adds a spot swatch based on CMYK information
 * @9397041 {Object|String} NewColor - if its an object of color information will convert otherwise assumes its a string
 * @9397041 {Number} c - the value of Cyan 0-100
 * @9397041 {Number} m - the value of Magenta 0-100
 * @9397041 {Number} y - the value of Yellow 0-100
 * @9397041 {Number} k - the value of Black 0-100
 * @9397041 {Boolean} SpotProcess - true/false if the spot color is a process or not. if true will be spot
 */
function AddSpot(NewColor, c, m, y, k, SpotProcess) {
    SpotProcess = (SpotProcess === 'true') /*convert to bool*/
    if (typeof NewColor == "object") {
        var name = NewColor[0]
        var c = NewColor[1]
        var m = NewColor[2]
        var y = NewColor[3]
        var k = NewColor[4]
    } else if (typeof NewColor == "string") {
        name = NewColor
    } else {
        alert(NewColor + " is not in a recognizable format for coding")
    }
    try {
        swatch = app.activeDocument.swatches[name]; // if swatch exists....  
        //AddSpot (name+=' 1', c, m, y, k); // ...add 1 to swatch name  
    } catch (e) {
        var newColor = new CMYKColor();
        newColor.cyan = c;
        newColor.magenta = m;
        newColor.yellow = y;
        newColor.black = k;
        var newSpot = app.activeDocument.spots.add()
        newSpot.colorType = SpotProcess ? ColorModel.SPOT : ColorModel.PROCESS;
        newSpot.name = name;
        newSpot.color = newColor;
    }
}

function FindLayerCreate(ParentLayer, ChildLayer) {
    try {
        var NewLayer = ParentLayer.layers[ChildLayer]
    } catch (e) {
        var NewLayer = ParentLayer.layers.add()
        NewLayer.name = ChildLayer
    }
    return NewLayer
}
Aidan_CAuthor
Inspiring
May 23, 2024

This works perfectly thank you so much! Been testing it over the last couple of days! Works so well thank you

Kurt Gold
Community Expert
Community Expert
May 17, 2024

Can you share at least one sample Illustrator file that shows the desired result?

Aidan_CAuthor
Inspiring
May 23, 2024

You are right this would have been helpful! Fortunately, RobOctopus seems to have hit the nail on the head with his script, will remember this moving forward.

Kurt Gold
Community Expert
Community Expert
May 23, 2024

Yes, RobOctopus' script is very good.

 

You may also take a look at the Grommet Marks script by William Campbell. It has some useful settings.

 

Crommet Marks script by William Campbell