Highlighted

Creating a specific shape with javascript

Participant ,
Jun 20, 2020

Copy link to clipboard

Copied

I was looking to have a script create the below shape, and place it within a document up to hundreds of times.  It's 2-3 circles and 2 rectangles, but I'm not sure of the best approach.  Would calling a function that creates it each time be most efficient?

 

mark.png

Hi Maple_Stirrup,

 

I think it would be a good time to use Symbols. Drag your artwork into the symbol palette, call it "mySymbol" for example, and try out this script:

 

// make sure there's a symbol in the document called "mySymbol"
mySymbol = app.activeDocument.symbols.getByName("mySymbol");
placeSymbols(mySymbol, 10);

function placeSymbol(symbol, pos) {
    // create a symbol item, centred on pos
    symbolItem = app.activeDocument.symbolItems.add(symbol);
    symbolItem.left = pos[0] - symbolItem.width / 2;
    symbolItem.top = pos[1] - symbolItem.height / 2;
    return symbolItem;
}

function placeSymbols(symbol, howMany) {
    // place a bunch of symbols randomly
    for (var i =0 ; i < howMany ; i ++ ) {
        var x = Math.random() * 100;
        var y = Math.random() * -100;
        placeSymbol(symbol, [x,y]);
    }
}

 

Of course, you'd have to decide how your symbol needed to be positioned. I've just made it random for the illustration.

 

Regards,

Mark

TOPICS
Scripting

Views

123

Likes

Translate

Translate

Report

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

Creating a specific shape with javascript

Participant ,
Jun 20, 2020

Copy link to clipboard

Copied

I was looking to have a script create the below shape, and place it within a document up to hundreds of times.  It's 2-3 circles and 2 rectangles, but I'm not sure of the best approach.  Would calling a function that creates it each time be most efficient?

 

mark.png

Hi Maple_Stirrup,

 

I think it would be a good time to use Symbols. Drag your artwork into the symbol palette, call it "mySymbol" for example, and try out this script:

 

// make sure there's a symbol in the document called "mySymbol"
mySymbol = app.activeDocument.symbols.getByName("mySymbol");
placeSymbols(mySymbol, 10);

function placeSymbol(symbol, pos) {
    // create a symbol item, centred on pos
    symbolItem = app.activeDocument.symbolItems.add(symbol);
    symbolItem.left = pos[0] - symbolItem.width / 2;
    symbolItem.top = pos[1] - symbolItem.height / 2;
    return symbolItem;
}

function placeSymbols(symbol, howMany) {
    // place a bunch of symbols randomly
    for (var i =0 ; i < howMany ; i ++ ) {
        var x = Math.random() * 100;
        var y = Math.random() * -100;
        placeSymbol(symbol, [x,y]);
    }
}

 

Of course, you'd have to decide how your symbol needed to be positioned. I've just made it random for the illustration.

 

Regards,

Mark

TOPICS
Scripting

Views

124

Likes

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
Jun 20, 2020 0
Contributor ,
Jun 20, 2020

Copy link to clipboard

Copied

Hi Maple_Stirrup,

 

I think it would be a good time to use Symbols. Drag your artwork into the symbol palette, call it "mySymbol" for example, and try out this script:

 

// make sure there's a symbol in the document called "mySymbol"
mySymbol = app.activeDocument.symbols.getByName("mySymbol");
placeSymbols(mySymbol, 10);

function placeSymbol(symbol, pos) {
    // create a symbol item, centred on pos
    symbolItem = app.activeDocument.symbolItems.add(symbol);
    symbolItem.left = pos[0] - symbolItem.width / 2;
    symbolItem.top = pos[1] - symbolItem.height / 2;
    return symbolItem;
}

function placeSymbols(symbol, howMany) {
    // place a bunch of symbols randomly
    for (var i =0 ; i < howMany ; i ++ ) {
        var x = Math.random() * 100;
        var y = Math.random() * -100;
        placeSymbol(symbol, [x,y]);
    }
}

 

Of course, you'd have to decide how your symbol needed to be positioned. I've just made it random for the illustration.

 

Regards,

Mark

Likes

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
Reply
Loading...
Jun 20, 2020 1
Participant ,
Jun 20, 2020

Copy link to clipboard

Copied

I apprecaite the reply.  What if I wanted this to work on any machine, a symbol wouldn't work then right?

Likes

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
Reply
Loading...
Jun 20, 2020 0
Contributor ,
Jun 20, 2020

Copy link to clipboard

Copied

I see, you need the script to draw it first. No problem. Extendscript has the basic drawing methods to make your object. (And then turn it into a symbol and position multiple instances.)

 

Have a look at Creating Paths and Shapes. Let me know how you go.

Likes

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
Reply
Loading...
Jun 20, 2020 1
Participant ,
Jun 30, 2020

Copy link to clipboard

Copied

Just throwing in an udpate, I used a lot of your ideas for placing symbols, and it works really really well. : ) 

 

Many thanks!

Likes

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
Reply
Loading...
Jun 30, 2020 1
m1b LATEST
Contributor ,
Jun 30, 2020

Copy link to clipboard

Copied

Very glad it helped! Thanks for letting me know. 🙂

Likes

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
Reply
Loading...
Jun 30, 2020 0