Skip to main content
Participating Frequently
July 18, 2023
Question

ExtendScript Illustrator

  • July 18, 2023
  • 3 replies
  • 657 views

Hey y'all I don't know if this fourm gets much use but I'll give it a shot. I'm trying to creat a script that pulls information from a text file specifically a name that gets auto generated by another software. I need to pull that name and paste it into a specific layer. Only problem is that the specific layer is in a symbol and I can't for the life of me figure out how to select a symbol and then a layer in that symbol 

This topic has been closed for replies.

3 replies

Kernzy
Inspiring
July 18, 2023

I've had luck using a manual find and replace function to replace text within a "broken symbol".

Ex:
C:/User/documents/NewName.txt contains the text "Frank"
The active document has "####" formatted and placed where I want it.
The script will replace "####" with Frank when the script is run by the user, in my case, when the new document is created.

var doc = app.activeDocument;
//set the text in your file to the variable "newName"
var seachString = /####/g; // g for global search, i for case sensitive search
var textFrames = doc.textFrames;
var thisTF, thisTF;

if (textFrames.length > 0) {
    for (var i = 0 ; i < textFrames.length; i++) {
          thisTF = textFrames[i];
          //alert(textFrames[i].contents)
            if (textFrames[i].contents == "####" ) {
                new_string = textFrames[i].contents.replace(seachString, newName);
                textFrames[i].contents = new_string;
    }
  }
}

 

pixxxelschubser
Community Expert
Community Expert
July 18, 2023

There may be a way. But I don't know if it is practical for you.

 

This is how it might work:

Use your symbol.

Break link to the symbol

Change what needs to be changed.

Save as a new symbol.

m1b
Community Expert
Community Expert
July 18, 2023

Yes @pixxxelschubser—I was thinking the same thing!

 

@tonycastaneda, I wondered if this was a downstream part of the process that no longer required "live" symbols? If so, you could expand them (back into normal page items) and make the change to the text.

 

Here is an example I put together to give you the idea. You can try it out with the attached illustrator file.

- Mark

 

/**
 * Demonstration of expanding (breaking)
 * a symbol item and altering the contents
 * of the first text frame inside it.
 * Note: the symbol item is gone - it is 
 * now a normal page item.
 * @author m1b
 * @discussion https://community.adobe.com/t5/illustrator-discussions/extendscript-illustrator/m-p/13943915
 */
(function () {

    var doc = app.activeDocument,
        targetSymbolName = 'Test',
        symbolItems = getSymbolItemsByName(doc, targetSymbolName);

    if (symbolItems.length == 0)
        throw Error('Could not find any symbol items of "' + targetSymbolName + '".');

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

        var item = expandSymbolItem(doc, symbolItems[i]),
            textFrames = getItems([item], 'TextFrame');

        if (textFrames.length > 0)
            textFrames[0].contents = 'Found!';

    }

})();


/**
 * Returns array of symbol items
 * of the symbol `name`.
 * @author m1b
 * @version 2023-07-18
 * @param {Document} doc - an Illustrator Document.
 * @param {String} name - the name of the SymbolItem's Symbol.
 * @returns {Array<SymbolItem>}
 */
function getSymbolItemsByName(doc, name) {

    var symbolItems = doc.symbolItems,
        found = [];

    for (var i = 0; i < symbolItems.length; i++)
        if (symbolItems[i].symbol.name == name)
            found.push(symbolItems[i]);

    return found;

};


/**
 * Returns items of `type`
 * found inside `items`.
 * @author m1b
 * @version 2023-07-18
 * @param {Array|collection} items - the items to search.
 * @param {String} type - the type of item to return.
 * @returns {Array<type>}
 */
function getItems(items, type) {

    var found = [];

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

        if (items[i].constructor.name == 'GroupItem') {
            found = found.concat(getItems(items[i].pageItems, type));
        }
        else if (items[i].constructor.name === type) {
            found.push(items[i]);
        }

    }

    return found;

};


/**
 * Expands (breaks) a symbol item
 * and returns the expanded page item.
 * @author m1b
 * @version 2023-07-18
 * @param {Document} doc - an Illustrator Document.
 * @param {SymbolItem} item - an Illustrator SymbolItem.
 * @returns {PageItem} - the expanded symbol.
 */
function expandSymbolItem(doc, item) {

    if (doc.constructor.name !== 'Document')
        throw Error('expandSymbolItem: Bad `doc` parameter.');

    if (typeof (item.breakLink) !== 'function')
        throw Error('expandSymbolItem: Bad `item` parameter.');

    var oldSelection = doc.selection;
    doc.selection = [item];
    item.breakLink();
    item = doc.selection[0];
    doc.selection = oldSelection;

    return item;

};
m1b
Community Expert
Community Expert
July 18, 2023

Hi @tonycastaneda, sadly the scripting API doesn't provide access to the internal items of Symbols. Getting text from a file is quite easy though. Maybe you can structure a different way, without using symbols?

- Mark

Participating Frequently
July 18, 2023

Dam... I've been beaning my head against wall trying to make it work for about 3 days now. Sadly the templated file I made is symbol driven as it slices up a single symbol into 3 shapes for textile print with the use masks so I don't have to worry about things not lining up when they get cut and stitched together. The idea was that id be able to shorten the file prep time to seconds rather than a couple of minutes