Exit
  • Global community
    • Language:
      • Deutsch
      • English
      • Español
      • Français
      • Português
  • 日本語コミュニティ
  • 한국 커뮤니티
0

how to create pattern in ai script

Community Beginner ,
Dec 31, 2017 Dec 31, 2017

in aiscript,i create a placeitem(picture),and i want to use it to fill the pathitem.like this:

var r=new patterncolor();

patterncolor.pattern=the pictue i want

pathitem.fillcolor=r;

so,how to convert placeitem to pattern.or how to filled the pathitem by the picture i want?

TOPICS
Scripting
4.4K
Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines

correct answers 1 Correct answer

Community Expert , Jan 03, 2018 Jan 03, 2018

Hi trymoremore, unfortunately we can't create pattern swatches from PlacedItems using new PatternColor().

As a workaround you can record an action to Make a Pattern, then you can play such Action using doScript() function.

to record an action to turn placedItems into Pattern Swatches

1. select your placed item

2. embed it

3. open Actions Panel, start recording

4. Insert Menu Item...(in Actions Panel Flyout Menu)

5. Object->Pattern->Make

6. Exit Pattern Editing Mode (in Layers Panel Flyout Menu)

7. Stop

...
Translate
Adobe
Community Expert ,
Dec 31, 2017 Dec 31, 2017

Hi trymoremore​,

you are here in the illustrator main forum - that's the wrong forum for your question.

How good is your scripting knowledgebase?

Perhaps a Moderator can move this thread to the right forum: Illustrator Scripting

I think you doesn't mean the following kind of pattern. Please explain in detail with screenshot (and perhaps with example ai-file) what you really want.

For the beginning a sample code in [JS] (read and alert the name of swatches with pattern):

var doc = activeDocument;

var theSwatches = doc.swatches;

for (var i=0; i <= theSwatches.length-1; i++) {

    swatchType = theSwatches.color.typename;

    swatchName = theSwatches.name;

    // alert name of pattern swatch

    if (swatchType == "PatternColor") {alert(swatchName)};

}

Have fun and a Happy New Year 2018

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Beginner ,
Dec 31, 2017 Dec 31, 2017

thank you for help! i will set the question in the right fourm.happy new year! 

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Dec 31, 2017 Dec 31, 2017

[moved from Illustrator​ to Illustrator Scripting​]

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Beginner ,
Jan 01, 2018 Jan 01, 2018

thank you for your patience and please forgive me for not responding promptly.

i want to filled the pathitem by the picture i want just like this :1.png

2.png

my idea is create a placeitem ,and convert the placeitem to pattern.then,create a patterncolor and Specify its “pattern” as the pattern the placeitem convertd. at last,use the patterncolor fill the pathitem.

So,what i don't know is how to convert placeitem  to pattern.

this is my code:

var patternColors = new PatternColor();

patternColors.pattern=pattern the placeitem convert;

pathitem.fillColor=patternColors;

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Jan 03, 2018 Jan 03, 2018

Hi trymoremore, unfortunately we can't create pattern swatches from PlacedItems using new PatternColor().

As a workaround you can record an action to Make a Pattern, then you can play such Action using doScript() function.

to record an action to turn placedItems into Pattern Swatches

1. select your placed item

2. embed it

3. open Actions Panel, start recording

4. Insert Menu Item...(in Actions Panel Flyout Menu)

5. Object->Pattern->Make

6. Exit Pattern Editing Mode (in Layers Panel Flyout Menu)

7. Stop Recording

patternMakerAction.PNG

then in your script, with your embedded placed item selected use

app.doScript("yourActionName", "yourActionSetName")

in my example, it would be

app.doScript("recreatePatternMaker", "test")

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Beginner ,
Jan 03, 2018 Jan 03, 2018

thanks for your help! my version of ai is cs6,whether this version can record an action?and whether the script you said is VBscript but not javascript? i am fool in it,please forgive me.

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Jan 03, 2018 Jan 03, 2018

I did not post a script, I just described the method.

yes, you can record the actions and use doScript() with CS6

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Advocate ,
Jan 03, 2018 Jan 03, 2018

bonjour,

Je propose une alternative au motif.

clipped.PNG

var docRef = activeDocument;

var obj = docRef.rasterItems[0]; //docRef.placedItems[0];

var newGroup = docRef.groupItems.add();

var dupobj = obj.duplicate(newGroup,ElementPlacement.PLACEATEND);

var cercle = newGroup.pathItems.ellipse(-300,200,80,80,false,true);

var c1, c2, dx, dy;

c1 = centreObj(cercle);

c2 = centreObj(dupobj); 

dx = c1[0]-c2[0]; 

dy = c1[1]-c2[1]; 

dupobj.translate(dx,dy); 

newGroup.clipped = true;

// ----

function centreObj(objet) {

  var rect = objet.geometricBounds;

  return [(rect[2]+rect[0])/2,(rect[1]+rect[3])/2];

}

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Participant ,
Nov 06, 2025 Nov 06, 2025

@CarlosCanto Saw your note that we're unable to create patterns, it seems so close. The below doesn't create any errors... patterns.add creates a default checkerboard

var newPat = new PatternColor();
newPat.pattern = app.activeDocument.selection[0];
app.activeDocument.patterns.add(newPat);
app.activeDocument.pathItems[0].fillColor.pattern = newPat;

With the pathItems[0], I was just trying to test if you can fill directly with a new pattern. 

Assuming swatches[1] is a pattern, this works to fill a path.

app.activeDocument.pathItems[0].fillColor = app.activeDocument.swatches[1].color

 
Trying to create legacy patterns, rather than use the make pattern menu item, which produces different results.

Referencing 2015 "Illustrator JavaScript Scripting Reference.pdf" pages 139 Pattern, 140 PatternColor and 142 Patterns. Patterns has an add method, Pattern needs a Pattern Color. Hoping that something similar to adding symbols would work? actDoc.symbols.add(actDoc.groupItems[0])

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Nov 06, 2025 Nov 06, 2025

Hi @_wckdTall_ , we're close indded. 

 

the pattern tool has been updated so the instructions I wrote in 2017 no longer work in 2025

[edit] my bad, it still works 

 

the legacy checkered pattern creation still works, so I guess now we coould edit the pattern (the two black squares) or add new content to it. 

 

What kind of pattern do you want to create? can you share a sample file?

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Participant ,
Nov 07, 2025 Nov 07, 2025

No specific pattern, just trying to add legacy swatches directly from selected art on the in the document. I don't want to use the pattern make menu command, as I'm using them to check seams in patterns I receive, legacy or pattern editing mode. Since they're just for testing, I guess I could create a clipping mask, and add a transparent shape behind since pattern editing mode honors those as bounds, but I'd prefer to just add a swatch directly. Also don't want to end up in editing mode as the problem is partially that expanded patterns can be extremely complex and cumbersome.

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Nov 07, 2025 Nov 07, 2025

ok, so the selected art has a legacy pattern but the pattern is not in the swatches panel? 

 

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Participant ,
Nov 11, 2025 Nov 11, 2025

These 2 actions run first to duplicate, and expand the pattern to suppress the dialog.

_wckdTall__0-1762883872971.png

In this case, expand a path filled with a pattern, and then run this code.

    function aiObjBounds(obj) {
        //Get Geometric Bounds 
        /* Left, Top, Right and Bottom 
        + - 1 - +
        |       |
        0       2
        |       |
        + - 3 - +
        */
        var bounds = obj.typename === "Artboard" ? obj.artboardRect : obj.geometricBounds,
            left = bounds[0];
        top = bounds[1];
        right = bounds[2];
        bottom = bounds[3];
        width = right - left;
        height = top - bottom;

        props = {
            bounds: [left, top, right, bottom],
            left: left,
            top: top,
            right: right,
            bottom: bottom,
            width: Math.abs(width),
            height: Math.abs(height),
            obj: obj
        };

        return props;
    }
    function aiLegacyPatternTestSetup() {

        var adB = aiObjBounds(actDoc.artboards[0]);
        var expandedGroup = app.activeDocument.selection[0];
        try {
            app.executeMenuCommand('Selection Hat 9');
            var patternFill = app.activeDocument.selection[0];
            var patNm = patternFill.fillColor.pattern.name;
            expandedGroup.selected = true;
            patternFill.selected = false;
            var patLyr = actDoc.layers.add();
            patLyr.name = "PatternTesting_Legacy";
            patLyr.move(actDoc, ElementPlacement.PLACEATBEGINNING);
            expandedGroup.move(patLyr, ElementPlacement.INSIDE);
            var expandedPattern = expandedGroup.groupItems[0].groupItems[0];
            expandedPattern.move(expandedGroup, ElementPlacement.PLACEBEFORE);
            expandedGroup.remove();
            try {
                var patternSeam = expandedPattern.pathItems[expandedPattern.pathItems.length - 1];
            }
            catch (e) {
                var patternSeam = expandedPattern.groupItems[0].pathItems[0];
                patternSeam.move(expandedPattern, ElementPlacement.PLACEATEND)
                patternSeam.clipped
            }
            var patternSeam = patternSeam.duplicate(expandedPattern, ElementPlacement.PLACEATBEGINNING);
            patternSeam.filled = false;
            patternSeam.stroked = true;
            patternSeam.strokeColor = aiRGBColor(255, 0, 0);
            patternSeam.opacity = 50;
            var pSB = aiObjBounds(patternSeam);
            var offset = 20;
            var boundsArr = [adB.left, adB.top, adB.left + pSB.width, adB.top - pSB.height];
            var emptyFill = aiMkRectangle(patLyr, boundsArr, offset, 0);
            emptyFill.translate(-pSB.width - offset, pSB.height + offset)
            emptyFill.strokeColor = emptyFill.fillColor = actDoc.swatches[0].color;
            emptyFill.fillColor = app.activeDocument.swatches[patNm].color;//
            emptyFill.translate(-offset, offset, false, true)
            emptyFill.name = "Fill Me With Pattern to check seams"
            emptyFill.selected = false;

        }
        catch (e) {
            alert("Pattern Not Detected. Try expanding right before running this script")
        }
    }

The nice to have last step, would be to add the selection to the swatches panel, right now I have to drag it in.

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Nov 11, 2025 Nov 11, 2025

I keep getting a "Pattern Not Detected. Try expanding right before running this script" error.

 

if I understood correctly, it might be as easy as this

// add pattern swatch from Selection
// carlos canto - 11/11/25

// select an object with a patter fill color before running
function main() {
    var idoc = app.activeDocument;
    var s = idoc.swatches.add();
    s.color = selection[0].fillColor;
}

main();
Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Participant ,
Nov 12, 2025 Nov 12, 2025

Thanks! That does work for adding a pattern fill, which are generally already swatches.

What I'm trying to accomplish with the script is to add a legacy pattern as a swatch, from the artwork in the document. Is that possible? 

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Nov 12, 2025 Nov 12, 2025
quote

Thanks! That does work for adding a pattern fill, which are generally already swatches.

What I'm trying to accomplish with the script is to add a legacy pattern as a swatch, from the artwork in the document. Is that possible? 


By @_wckdTall_

 

that's what the snippet does, select a pathItem with a pattern applied to it, the script will create a pattern swatch

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Participant ,
Nov 13, 2025 Nov 13, 2025

Thanks. What I'm trying to do, is make a legacy pattern from art on the artboard, example below. 

_wckdTall__1-1763055055871.png

My script is meant to test the seams of legacy patterns for review, and creating a pattern with pattern editor, will cause additional errors. Not to simply add the fill of an existing object.

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Nov 13, 2025 Nov 13, 2025

got it now, that's why I asked for a sample from the very beginning

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Participant ,
Nov 14, 2025 Nov 14, 2025

Missed that part, thought the full script and screen shot of the action would help. Is it possible to achieve, or inaccessible to scripting?

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Nov 14, 2025 Nov 14, 2025

I've got you

 

// create Pattern Swatch from Selected Art
// carlos canto - 2025

// works best if you define your own repeat, including a no-fill, no-stroke repeat path boundary at the bottom
function main() {
    var idoc = app.activeDocument;
    
    app.executeMenuCommand("Adobe Make Pattern");

    idoc.exitIsolationMode()
}
main();
Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Participant ,
Nov 14, 2025 Nov 14, 2025
LATEST

Thanks! "Adobe Make Pattern" is the pattern editing mode I was trying to avoid, as it can be resource heavy and lead to additional pattern errors converting legacy patterns. If it's the only option though, I could certainly add a clipping mask to the script.

Side note, I was wondering if there was a way to "exitIsolationMode()" earlier today for a separate script, so I'm glad to find out that there is! Couldn't find it in documentation.

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Beginner ,
Feb 13, 2020 Feb 13, 2020

Just found this script that could cover steps 1 & 2:

#target Illustrator

if ( app.documents.length > 0 ) {
    while ( app.activeDocument.placedItems.length > 0 ) {
        placedArt = app.activeDocument.placedItems[0];
        placedArt.embed();
    }
}

 Source: https://graphicdesign.stackexchange.com/a/71642

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines