Last active
November 15, 2019 06:22
-
-
Save rjduran/d8e1f6d3a42572c23be8b63126d8c6aa to your computer and use it in GitHub Desktop.
A simple symbol swapping Adobe Illustrator script made to reproduce the functionality of the Replace dropdown for symbols.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// define the document width (in points) | |
var width = 100; | |
// define the document height (in points) | |
var height = 100; | |
// create a new document | |
var docPreset = new DocumentPreset(); | |
var presets = app.startupPresetsList; | |
var preset = presets[0]; // Default: Art & Illustration (Doesn't really matter but addDoocument needs it) | |
docPreset.width = width; | |
docPreset.height = height; | |
docPreset.colorMode = DocumentColorSpace.RGB; | |
var doc = app.documents.addDocument(preset, docPreset, false); | |
// shift origin | |
doc.rulerOrigin = [0,height]; | |
// add to existing layer | |
var layer = doc.layers[0]; | |
// draw a square (rect) w/ fill | |
var square1 = rect(layer, 0, 0, 10, 10); | |
square1.filled = true; | |
square1.stroked = false; | |
square1.fillColor = makeColorRGB(0,0,255); | |
// draw a square (rect) w/ fill | |
var square2 = rect(layer, 0, 0, 10, 10); | |
square2.filled = true; | |
square2.stroked = false; | |
square2.fillColor = makeColorRGB(255,0,0); | |
// make a symbol for the squares | |
// set their names | |
var newSymbol1 = doc.symbols.add(square1); | |
newSymbol1.name = "square1"; | |
var newSymbol2 = doc.symbols.add(square2); | |
newSymbol2.name = "square2"; | |
square1.remove(); | |
square2.remove(); | |
// make instance of square1 symbol | |
var mySymbolItem1 = doc.symbolItems.add(newSymbol1); | |
// set its position | |
mySymbolItem1.position=Array((20),(-20)); | |
// make instance of square2 symbol | |
var mySymbolItem2 = doc.symbolItems.add(newSymbol2); | |
// make instance of square1 symbol to be replaced by instance of square2 | |
var mySymbolItem3 = doc.symbolItems.add(newSymbol1); | |
mySymbolItem3.position=Array((40),(-20)); // set position for instance | |
// update position of this instance to that of mySymbolItem3 | |
mySymbolItem2.position=Array((mySymbolItem3.position[0]),(mySymbolItem3.position[1])); | |
// remove the temp symbol instance | |
mySymbolItem3.remove(); | |
// make color utility function | |
function makeColorRGB(r,g,b){ | |
var c = new RGBColor(); | |
c.red = r; | |
c.green = g; | |
c.blue = b; | |
return c; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment