简体   繁体   中英

How to load a symbol library in Illustrator using javascript?

By code, I'm trying to load a symbol library from a reference Illustrator file to another. I would like to do the same load as when you click on 'Other Library' in the UI then you have to choose an Ai file to get all its symbols.

在此处输入图片说明

Does anyone know how to do it? Is there an easy way or should I copy the symbols one after the other?

I have tried to load the other document then copy each symbol to the first document:

var loadSymbolLibrary = function(fromAiFile) {
    var thisDocument = activeDocument;
    app.open(fromAiFile);
    var fromAiDocument = activeDocument;

    var symbols = fromAiDocument.symbols;
    for(var i = 0; i < symbols.length; i++) {
        var symbol = symbols[i];
        thisDocument.symbols.add(symbol);
    }

    fromAiDocument.close();
    activeDocument = thisDocument;
}

But I get an error when adding a symbol to the first document. According to the Adobe documentation, I need a PageItem and a SymbolRegistrationPoint to create a symbol with the add() function.

[EDIT]

I also tried to load the document containing symbols in this way:

    var openOptions = new OpenOptions();
openOptions.openAs = LibraryType.SYMBOLS;
var symbolDocument = app.open(symbolAiFile, null, openOptions);

This opens a symbol window in Illustrator, but I don't know how to deal with it by code as calling symbolDocument.symbols produce a 'The document is no longer open' error.

First off, it will be useful to know for clarity that a SymbolItem is a placed instance of a Symbol and a Document has both of those.

Illustrator will not allow a call to Document.symbolItems.add() if the symbol being passed is not in that document's symbols list. Personally, I consider that a bug. Here is a wrapper class that opens the symbol library as a new document (note the exclusion of relevant openAs property), places the symbol into that document, then copies the symbol item to the other document.

#target illustrator-18


//class
function SymbolLibrary(){

    this.symbolsDoc = null;
    this.len = 0;

    this.begin = function(symbolFileName){
        var file1 = File(symbolFileName);
        //open the symbol library
        var optRef = new OpenOptions();
        //optRef.openAs = LibraryType.SYMBOLS; //broken. just open as regular doc
        this.symbolsDoc = app.open( file1 , null , optRef);
        this.len = this.symbolsDoc.symbols.length;
        return this.symbolsDoc;
    }

    this.list = function(){
        for(var i=0;i<symbolsDoc.symbols.length;i++){
            $.writeln( symbolsDoc.symbols[i] );
        }
        return symbolsDoc.symbols;
    }

    this.place = function(targetDoc,index){
        var symbolItem = this.symbolsDoc.symbolItems.add(this.symbolsDoc.symbols[index]);
        finalItem = symbolItem.duplicate( targetDoc,ElementPlacement.PLACEATEND );
        symbolItem.remove();
        return finalItem;
    }

    this.end = function(){
    this.symbolsDoc.close(SaveOptions.DONOTSAVECHANGES);
    }
}

var filename =  "/Applications/Adobe\ Illustrator\ CC\ 2014/"+
    "Presets.localized/en_US/Symbols/Nature.ai";


//create new document
var mydoc = app.documents.addDocument('',new DocumentPreset());

//instantiate the symbol library handler class
var lib1 = new SymbolLibrary();

lib1.begin(filename);
for(var i=0;i<lib1.len;i++){
    lib1.place(mydoc,i).position = [((i%10)*100),i*10];
}
lib1.end();

$.writeln('#########');

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM