Skip to main content
Inspiring
March 20, 2023
Answered

JSX: How to process groupItems for Symbol Object

  • March 20, 2023
  • 3 replies
  • 1300 views

I need to make hidden group items inside Symbol with specific name.

Is there any way to get groupItems for Symbol instance?

 

var doc = app.activeDocument;
for (var i = 0; i < doc.symbols.length; i++) {
  var symbol = doc.symbols[i];
  // How to get groupItems for symbol???
}

 

This topic has been closed for replies.

3 replies

CarlosCanto
Community Expert
Community Expert
March 21, 2023

sorry I can't think of a hack-way of editing the original symbol.

 

I would go with @m1b advice, can you share why it is not safe to use two versions? 

 

Are you aware that you can make symbols out of other symbols?

You could make one symbol only with the "print" groups, then make another symbol with the "cut" groups. Select both print and cut symbol instances and make a third "cut/print" symbol. Both instances can be visible and on top of each other.

 

Now you can place the rest of the "combined" symbol instances same way as you're doing it now. Then you can manually (or via script) switch back and forth between cut and print views. You would replace the "combined" symbol with either "print" or "cut" symbols. Select all symbol instances on the artboard then in the Control Panel (or the Symbols Panel Menu) click on the Replace Symbol to choose either Print or Cut. The Control Panel button is easier to use than the Symbols Panel Menu.

 

Your script can also Replace symbols before saving to pdf.

 

Would that work for you?

Hacker-CBAuthor
Inspiring
March 21, 2023

I need to print and cut front panel stickers to different kind of devices.

Devices has lot of common things. 

This is reason I use Symbols to avoid duplicating, also this is reason I keep all devices in one AI files. 

 

Document has tens of "Master" Symbols (placed on the Artboards for printing) and tens of "Secondary" symbols (common graphics for each Master Symbol, such as logos, common texts, etc)

 

Each "Master" symbol has cutting and printing layers. All of them are located inside common group (symbol). So I don't care about cut and print can be unsynced (moved accroding each other). 

 

Suggestions to replacing symbols is not solution for me. At least I will have 2 times more symbols, but I already have tens of them. I need one click soultion to generate needed PDFs (Cut and Print for each Artboard).

Currently my script works fine except I can see preview only on separated document. I will share my plugin on github after I finish it.

 

Generally, AI is missing one impotant thing, something like "tags" or "labels" (like in Sketchup) which can be set to any object/gorup/etc... It will be very easy to hide/unhide elements inside symbols when such feature present.

Hacker-CBAuthor
Inspiring
March 23, 2023
CarlosCanto
Community Expert
Community Expert
March 20, 2023

what's the purpose of making changes to a Symbol? Symbols are meant to be used and re used. 

 

Can you explain what you're trying to accomplish, there might be other workarounds

Hacker-CBAuthor
Inspiring
March 20, 2023

I'm using AI for the Cutter purpose.

I need to make two different PDFs as a result: one for printing, one for cutting.

 

All items are defined as symbols. I use symbols because I need to place lot of items in one sheet randomly and quickly modify "master" symbol.

 

All groups, which are definied iniside symbols have special names:

  • "Print" - for print purpose.
  • "Cut" - for cutting purpose.

All other groups I leave inchanged.

 

Currently, as workaround, I'm making copy of all document layers and drawings to the new document and calling 

breakLink() for all doc.symbolItems. I can make doc.groupItems with defined special names hidden/visible in this case.

 

Here is helper functions:

 

//@include "../../lib/underscore-umd.js"

DocUtils = {};

/**
 * Make copy of document
 *
 * @9397041 srcDoc Source document
 * @Returns
 */
DocUtils.duplicate = function(srcDoc) {

    // See https://community.adobe.com/t5/illustrator-discussions/copy-artboard-and-art-to-another-document/m-p/3907417/page/2#M10827

    // Save source docuemtn data
    var srcDocData = {
        documentColorSpace: srcDoc.documentColorSpace,
        units: srcDoc.rulerUnits,
        width: srcDoc.width,
        height: srcDoc.height,
        rulerOrigin: srcDoc.rulerOrigin,
    };

    // Save source artboard data
    var srcAbData = [];
    for (var i = 0; i < srcDoc.artboards.length; i++) {
        var srcAb = srcDoc.artboards[i];
        srcAbData.push({
            name: srcAb.name,
            artboardRect: srcAb.artboardRect,
            rulerOrigin: srcAb.rulerOrigin,
        })
    }

    // This actually does not needed.
    //var docPreset = new DocumentPreset();
    //docPreset.units = srcDocData.units;
    //var dstDoc = app.documents.addDocument(srcDocData.documentColorSpace, docPreset);

    // Create new document
    var dstDoc = app.documents.add(srcDocData.documentColorSpace);
    dstDoc.rulerOrigin = srcDocData.rulerOrigin;

    // Create ArtBoards
    var defaultArtboard = dstDoc.artboards[0];
    for (var i = 0; i < srcAbData.length; i++) {
        var ab = dstDoc.artboards.add(srcAbData[i].artboardRect);
        ab.name = srcAbData[i].name;
        ab.rulerOrigin = srcAbData[i].rulerOrigin;
    }
    defaultArtboard.remove(); // Remove default artboard


    // Duplicate layers structure
    var lockedLayers = [];
    var defaultLayer = dstDoc.layers[0];
    for (var i = 0; i < srcDoc.layers.length; i++) {
        var srcLayer = srcDoc.layers[i];
        var dstLayer = dstDoc.layers.add();

        dstLayer.move(dstDoc, ElementPlacement.PLACEATEND);

        dstLayer.name = srcLayer.name;
        dstLayer.visible = srcLayer.visible;

        //dstLayer.locked = srcLayer.locked; // We lock later
        if (srcLayer.locked) {
            lockedLayers.push(dstLayer);
        }
    }
    defaultLayer.remove(); // Remove default layer


    // Copy page items
    for (var i = 0; i < srcDoc.pageItems.length; i++) {
        var srcItem = srcDoc.pageItems[i];
        if (srcItem.parent instanceof Layer) {
            var dstItem = srcItem.duplicate(dstDoc.layers[srcItem.layer.name], ElementPlacement.PLACEATEND);
            dstItem.left = srcItem.left;
            dstItem.top = srcItem.top;
        }
    }

    // Lock locked layers back
    for (var i = 0; i < lockedLayers.length; i++) {
        lockedLayers[i].locked = true;
    }


    return dstDoc;
};

/**
 * Convert all symbols to shapes
 *
 * @9397041 doc
 */
DocUtils.convertSymbolsToShapes = function(doc){
    for (var i = 0; i < doc.symbolItems.length; i++) {
        var symbolItem = doc.symbolItems[i];
        var symbol = symbolItem.symbol;

        symbolItem.breakLink();

        // FIXME: Set new group name according to symbol.name
    }

    doc.symbols.removeAll();
};

/**
 * Set visibility of groups
 *
 * @9397041 doc Document
 * @9397041 names Array of group names
 * @9397041 visible Visibility
 */
DocUtils.setGroupsVisibility = function(doc, names, visible){
    for (var i = 0; i < doc.groupItems.length; i++) {
        var group = doc.groupItems[i];

        if (_.indexOf(names, group.name) != -1) {
            $.writeln("Set group " + group.name + " visible: " + visible);
            var locked = group.locked;
            if (locked){
                group.locked = false;
            }
            group.hidden = !visible;
            if (locked){
                group.locked = true;
            }
        } else {
            // $.writeln("Skip group " + group.name);
        }
    }
}
Hacker-CBAuthor
Inspiring
March 20, 2023

Here is usage example:

 

var docCopy = DocUtils.duplicate(app.activeDocument);

DocUtils.convertSymbolsToShapes(docCopy);
DocUtils.setGroupsVisibility(docCopy, ['Cut'], true);
DocUtils.setGroupsVisibility(docCopy, ['Print'], false);
Sergey Osokin
Inspiring
March 20, 2023

Unfortunately, scripts don't know how to edit symbol content.

Hacker-CBAuthor
Inspiring
March 20, 2023

Its very poor, thank you 😞