Skip to main content
Participating Frequently
July 28, 2015
Answered

How to import symbols from a symbol library?

  • July 28, 2015
  • 3 replies
  • 4222 views

I need to import symbols from an Ai file to other documents by script, then place them by replacing objects.

I have no problem with the replacement of a symbol when it's part of the symbols of the document.

What I fail to do, it's the importation of a symbol collection from an Ai file to the current document.

I load the symbol document as a symbol library, in this way:

var openOptions = new OpenOptions();

openOptions.openAs = LibraryType.SYMBOLS;

var symbolDocument = app.open(File('/d/test/symbol_collection.ai'), null, openOptions);

It works well as it opens a symbol collection window in Illustrator.

But I don't know how to get these symbols to copy them into the current document.

When I invoke symbolDocument.symbols, I get a 'The document is no longer open' error.

So, how to copy symbols? How to access this symbol collection loaded in Illustrator other than using the symbolDocument?

Thanks a lot.

This topic has been closed for replies.
Correct answer CarlosCanto

...or, duplicate works as well, you're just missing your destination document

var destinationDoc = app.activeDocument;

var symbolDocument = app.open(File('/c/temp/GuillochePatternTutorial.ai'));

var mySymbol = symbolDocument.symbols["Cube"];

var symbolItem = symbolDocument.symbolItems.add(mySymbol);

var symbolCopy = symbolItem.duplicate(destinationDoc);

3 replies

Participant
February 4, 2016
Silly-V
Legend
February 4, 2016

What do you mean by LibraryType.SYMBOLS being broken with the "openAs" open options property?

It works for me on Windows CC2015 to open a library in a palette (as manually opening one would).

It does not let you put these items inside the document automatically, though, so that's why opening as regular doc is needed for placing the symbols.

Participant
February 4, 2016

Ok, which version are you on?

I'm going to test this on my Mac:

#target illustrator

function test(){

    var f = File("wherever/Web Icons.ai");

    var opts = new OpenOptions;

    opts.openAs = LibraryType.SYMBOLS;

    app.open(f, null, opts);

};

test();


i only tested targets illustrator-18 and illustrator-17, and both produced the bug. i added two more lines of code to your function. Notice how doc1.symbols is not accessible, just one call following app.open(), then comment out the "openAs" line and run it again. I am on OS X Yosemite.

function test(){

    var filename =  "/Applications/Adobe\ Illustrator\ CC\ 2014/"+

    "Presets.localized/en_US/Symbols/Nature.ai";

    var f = File(filename);

    var opts = new OpenOptions;

    opts.openAs = LibraryType.SYMBOLS;

    var doc1 = app.open(f, null, opts);

    $.writeln(doc1.symbols);

};

Silly-V
Legend
July 28, 2015

I would guess you may need to open the symbol document as .ai and duplicate the symbol from it into your target document. That's my guess.

Participating Frequently
August 4, 2015

Yes, that's what I guess too. I just tried to do it but I don't know how to correctly duplicate the symbol from a document to another.

I'm now opening the 'symbolDocument' as a .ai and I get well the Symbol object (which has as a 'parent' the 'symbolDocument'). Then in the target document, I tried:

activeDocument.symbolItems.add(symbol)

but I get "an Illustrator error occured 1398369860 ('SYnD')".

I also tried:

var newSymbol = symbol.duplicate()

before adding the symbol to symbolItems but the parent document of the duplicated is still the symbol document.

I think what I actually need to do is to copy the art object from the symbolDocument, then create a symbol in the target document with it. But I don't know how to access the art object from a Symbol object.

Silly-V
Legend
August 4, 2015

  Ok, looks like copy/pasting may be a resort:

    var symPath = "C:/Users/User/Folder/File.ai";

    app.open(File(symPath));

    var symDoc = app.activeDocument;

    var desiredSymbolName = "MySymbol";

    try{

        var symbolDef = symDoc.symbols.getByName(desiredSymbolName);

        var instance = symDoc.symbolItems.add(symbolDef);

        instance.selected = true;

        app.copy();

        symDoc.close(SaveOptions.DONOTSAVECHANGES);

        app.paste();

    } catch(e){

        alert("Symbol '" + desiredSymbolName + "' was not found.");

    }