Skip to main content
MidoSemsem
Inspiring
February 19, 2024
Answered

Add specific symbol from specific library

  • February 19, 2024
  • 1 reply
  • 1561 views

Hello Everyone,

I’m looking for a script or function to automatically add a specific symbol from a specific library to my document.
For example, I want to add the symbol “Fish” from theTiki Library.

Any help is appreciated. Thanks!”

This topic has been closed for replies.
Correct answer MidoSemsem

Thank you René, I will do as you wisely suggest and update the script above.

- Mark


Working on my script version, I tried to merge all of the best ideas. The script is now Windows and macOS compatible. I integerated René's first solution to locate the file if it doesn't exist.

 

 

#target illustrator  

(function () {
    try {
var  symFilePath;
doc = app.activeDocument;
if ($.os.indexOf("Windows") != -1) {
      symFilePath = new Folder(app.path + "/Presets/"+ app.locale +"/Symbols");
      } else {
    symFilePath = new Folder(app.path + "/Presets.localized/"+ app.locale +"/Symbols");
}
      var symFile = new File(symFilePath + "/tiki.ai"); //Replace with symbol file name
 if (!symFile.exists) {
            alert("File not found. Click OK to locate.");
            symFile = File.openDialog("Select ai file to open.","AI files:*.ai",false);
        }

    if (symFile != null) {
      var symDoc = app.open(symFile);
      var symbl = symDoc.symbols.getByName("Fish"); //Replace with symbol name
      var symItem = symDoc.symbolItems.add(symbl);
            symItem =  symItem.duplicate(doc);
            symItem.remove();
      //Close reference document
      symDoc.close(SaveOptions.DONOTSAVECHANGES);
    }
    } catch (error) {
        return alert("Symbol "+symbolName+" Not Found");
    }
})();

 

 


Thank you @renél80416020  and @m1b 

1 reply

renél80416020
Inspiring
February 19, 2024

Bonjour,

Les bibliothèques de symboles sont des fichiers.ai

Document actif à modifier

Ouvrir le fichier Tiki.ai
Sur CS6 et Windows, l'emplacement est: C:\Program Files\Adobe\Adobe Illustrator CS6 (64 Bit)\Presets\fr_FR\Symboles

Placer sur le plan de travail une instance du symbole 'Poisson'.

dupliquez cet objet dans le document concerné.

fermer le fichier Tiki.ai sans le modifier.

 

// JavaScript Document for Illustrator
// var folderName = " C:/Program Files/Adobe/Adobe Illustrator CS6 (64 Bit)/Presets/fr_FR/Symboles"
  var docRef = app.activeDocument;
  // Open a file ai using
    var fileRef = File.openDialog("Select ai file to open.","AI files:*.ai",false);
    if (fileRef != null) {
      var docRef1 = app.open(fileRef);
      var symbolRef = docRef1.symbols.getByName("Poisson"); //Fish
      var ns = docRef1.symbolItems.add(symbolRef);
          ns.duplicate(docRef)
      //Close reference document
      docRef1.close(SaveOptions.DONOTSAVECHANGES);
    }

 

 

MidoSemsem
Inspiring
February 20, 2024

Thanks @renél80416020, that's a good solution.

However, the Tiki library is integrated into Illustrator.

How can I insert the symbol without opening the dialog window?

renél80416020
Inspiring
February 20, 2024

Bonjour, voilà une solution...

René

 

// JavaScript Document for Illustrator
// capture symbol.js
// Autour Landry René
// Tue, 20 February 2024 16:41:05 GMT
// INIT --------------------------------
  var sourceFolderPath = "C:/Program Files/Adobe/Adobe Illustrator CS6 (64 Bit)/Presets/fr_FR/Symboles";
  var bibliName = "Tiki";
  var symbolName = "Poisson";  //Fish
// --------------------------------------
  var docRef = app.activeDocument,
      sourceFolder = new Folder(sourceFolderPath),
      contentInput = sourceFolder.getFiles("*.ai"),
      fileRef,
      docRef1;

        for (var i = contentInput.length-1; i >= 0; i--) {
          if (contentInput[i].fsName.indexOf(bibliName+".ai") != -1) {
            fileRef = new File(contentInput[i]);
            docRef1 = open(fileRef);
            break;
          }
        }
    if (docRef1 != undefined) {
      var symbolRef = getSymbl(symbolName); //docRef1.symbols.getByName(symbolName);
      var ns;
            if (symbolRef != undefined) {
              ns = docRef1.symbolItems.add(symbolRef);
              ns =  ns.duplicate(docRef);
              ns.remove();
            }
            else{alert("Object "+symbolName+" Not Found Error");}
      //Close reference document
      docRef1.close(SaveOptions.DONOTSAVECHANGES);
    }
// -------
function getSymbl(nom)
{ // Returns symbol by name
     try {
     var symb = activeDocument.symbols.getByName(nom);
     }  catch (e) { //alert( "Le nom du symbole exist" );
          return undefined;
        }
     return symb;
}
// -------