Skip to main content
malinn6934325
Participating Frequently
May 8, 2015
Answered

Script for randomly replacing symbols

  • May 8, 2015
  • 2 replies
  • 5377 views

Hi!

I'm looking for a script that will allow you to select some objects or symbols in your AI-file and then select some of your symbols from the symbols-library in your file and then randomly replace the original objects/symbols with the symbols selected. I have found a few scripts that does basically this but with random replacement of selected colours, transparency or angles but I need one for randomly replacing symbols.

Can someone help?

This topic has been closed for replies.
Correct answer wckdtall

I revised an alternate workaround to selecting the symbols you want to randomize by placing them in a specific layer. On first run it will create the layer if it can't find it. Also revised the original script to size proportinatly based on height, and place the items within the original parent layer:

var doc = app.activeDocument;
var sym = doc.symbols;
var fullSelection = doc.selection;
try { doc.layers["Symbols_To_Place"] }
catch (e) {
    symLyr = doc.layers.add();
    symLyr.name = "Symbols_To_Place";
    alert("Select Symbols to randomize\nBy placing in \"Symbols_To_Place\" Layer.")
}
var symTgt = doc.layers["Symbols_To_Place"].symbolItems;
var symAry = [];
for (var s = 0; s < symTgt.length; s++) {
    nm = symTgt[s].symbol.name;
    symAry.push(nm);
}
for (var i = 0; i < fullSelection.length; i++) {
    var sel = fullSelection[i];

    var place = doc.symbolItems.add(sym[symAry[Math.floor(Math.random() * symAry.length)]]);
    place.move(sel, ElementPlacement.PLACEBEFORE);
    //Base it off the height
    percent = sel.height / place.height;
    place.height = sel.height;
    place.width = place.width * percent;
    place.top = sel.top;
    place.left = sel.left;
    sel.remove();
    place.selected = true;
}

 

 

 

 

2 replies

Participating Frequently
January 29, 2021

Great script! Could it be modified to use only selected symbols from the Symbols palette?

Silly-V
Legend
January 29, 2021

The problem is that with scripting they don't let us know what symbols are even selected in the Symbols palette. However through ponderous workarounds, we can sort of get there.

If the action of using the Symbol panel's flyout menu's delete or duplicate commands is recordable, we can play it with a script. This means that we can read all the symbols in the document's symbols collection and then perform one of those actions and compare the before/after difference of what was there and not. However, if you do the delete action, you will have to do an Undo to get the symbols back and worse it may mangle some symbol instances you did not wish to disturb if those symbols are removed.

With Duplicate action all you have to do is remove all the symbols which are added that are considered new.

I can try this sometime this weekend and see if it works!

Participating Frequently
January 30, 2021

Silly-V,

Thanks for your response.

Very kind of you to test a workaround.

Take care.

Qwertyfly___
Legend
May 11, 2015

Try this. it will use all symbols in document.

var doc = app.activeDocument;

var sym = doc.symbols;

var sel = doc.selection;

for(var i = 0; i < sel.length; i++){

    var place = doc.symbolItems.add(sym[Math.floor(Math.random() * sym.length)]);

    place.height = sel.height;

    place.width = sel.width;

    place.top = sel.top;

    place.left = sel.left;

    sel.remove();

    place.selected = true;

}

malinn6934325
Participating Frequently
May 11, 2015

That works perfectly!!!

Thanks so much!!!

Participant
March 27, 2018

Hi!

I need the exact action to do with a script. But it is my first time using script, how can I install it?

I tried through extendscript toolkit, but the script doesnt appear in illustrator.