• Global community
    • Language:
      • Deutsch
      • English
      • Español
      • Français
      • Português
  • 日本語コミュニティ
    Dedicated community for Japanese speakers
  • 한국 커뮤니티
    Dedicated community for Korean speakers
Exit
3

Add specific symbol from specific library

Participant ,
Feb 19, 2024 Feb 19, 2024

Copy link to clipboard

Copied

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!”

TOPICS
Experiment , How-to , Scripting , Tools

Views

309

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines

correct answers 2 Correct answers

Community Expert , Feb 20, 2024 Feb 20, 2024

Hi @MidoSemsem, between René and yourself, you have solved this. However, I noticed that the path to get the preset library file does not work on my installation (MacOS 14.3.1) because the "Presets" folder is called "Presets.localized" for some reason.

 

I wrote my version of this task and of course it is basically the same as René's and uses your path to the library file (this was a big help!) but simply tests both variations on the "Presets" folder name if necessary:

 

/**
 * Load a symbol ite
...

Votes

Translate

Translate
Participant , Feb 21, 2024 Feb 21, 2024

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 +"/Symb
...

Votes

Translate

Translate
Adobe
Advocate ,
Feb 19, 2024 Feb 19, 2024

Copy link to clipboard

Copied

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);
    }

 

 

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Participant ,
Feb 19, 2024 Feb 19, 2024

Copy link to clipboard

Copied

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?

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Advocate ,
Feb 20, 2024 Feb 20, 2024

Copy link to clipboard

Copied

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;
}
// -------

 

 

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Participant ,
Feb 20, 2024 Feb 20, 2024

Copy link to clipboard

Copied

Thanks again @renél80416020 

These scripts were really helpful.
With some minor modifications, I came up with the solution 😊

 

 

 

#target illustrator  

(function () {
    try {
var  doc = app.activeDocument;
      var symFilePath = app.path + "/Presets/"+ app.locale +"/Symbols";
      var symFile = new File(symFilePath + "/tiki.ai"); //Symbol File 
    if (symFile != null) {
      var symDoc = app.open(symFile);
      var symbl = symDoc.symbols.getByName("Fish"); //Symbol 
      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");
    }
})();

 

 

 




Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Advocate ,
Feb 20, 2024 Feb 20, 2024

Copy link to clipboard

Copied

Super!

Bien pour:

var symFilePath = app.path + "/Presets/"+ app.locale +"/Symbols";

René

 

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Feb 20, 2024 Feb 20, 2024

Copy link to clipboard

Copied

Hi @MidoSemsem, between René and yourself, you have solved this. However, I noticed that the path to get the preset library file does not work on my installation (MacOS 14.3.1) because the "Presets" folder is called "Presets.localized" for some reason.

 

I wrote my version of this task and of course it is basically the same as René's and uses your path to the library file (this was a big help!) but simply tests both variations on the "Presets" folder name if necessary:

 

/**
 * Load a symbol item from an Illustrator built-in library file
 * and add it to the active document.
 * @author m1b
 * @discussion https://community.adobe.com/t5/illustrator-discussions/add-specific-symbol-from-specific-library/m-p/14432859/thread-id/397957
 */
(function () {

    if (0 == app.documents.length)
        return alert('Please open a document which will be the destination of the symbol item.');

    var symbolItem = addSymbolItemFromLibrary({
        presetFolderName: 'Symbols',
        libraryName: 'Tiki',
        symbolName: 'Fish',
    });

    if (undefined != symbolItem)
        alert('Success!');

})();


/**
 * Adds the symbol item of symbol from a 'built-in' library file.
 *
 * Example:
 *     var symbolItem = addSymbolItemFromLibrary({
 *         libraryName: 'Tiki',
 *         symbolName: 'Fish',
 *     });
 *
 * @author m1b, inspired by @renél80416020
 * @version 2024-02-21
 * @param {Object} options
 * @param {String} options.libraryName - the symbol library name, eg. "Tiki".
 * @param {String} options.symbolName - the symbol name, eg. "Fish".
 * @param {String} [options.presetFolderName] - the preset folder name (default: 'Symbols').
 * @param {Document} [options.targetDocument] - add the symbol to this document (default: active document).
 * @returns {Symbol|SymbolItem}
 */
function addSymbolItemFromLibrary(options) {

    options = options || {};
    options.presetFolderName = options.presetFolderName || 'Symbols';
    options.targetDocument = options.targetDocument || app.activeDocument;

    var symbolLibraryFile = getPresetsFile(options.presetFolderName, options.libraryName);

    if (undefined == symbolLibraryFile)
        return alert('Could not find symbol library file "' + options.libraryName + '" in presets folder "' + options.presetFolderName + '".');

    try {

        var symbolLibraryDoc = app.open(symbolLibraryFile),
            symbol = symbolLibraryDoc.symbols.getByName(options.symbolName),
            symbolItem = symbolLibraryDoc.symbolItems.add(symbol);

        // copy symbolItem (and symbol) to document
        symbolItem = symbolItem.duplicate(options.targetDocument);

    }

    catch (error) {
        return (1302 === error.number)
            ? alert('Could not find symbol "' + options.symbolName + '".')
            : alert(error);
    }

    finally {

        // close the symbol library document
        symbolLibraryDoc.close(SaveOptions.DONOTSAVECHANGES);

    }

    return symbolItem;

};


/**
 * Returns a library file.
 * @author m1b (from idea by MidoSemsem)
 * @version 2024-02-21
 * @param {String} presetName - the name of the preset type, eg. 'Symbols'.
 * @param {String} libraryName - the name of the library file, with or without '.ai' extension, eg. 'Tiki'.
 * @returns {?File}
 */
function getPresetsFile(presetName, libraryName) {

    var path = app.path + '/###/' + app.locale + '/' + presetName + '/' + libraryName.replace(/\.[^\.]$/, '') + '.ai',
        file = File(path.replace('###', 'Presets'));

    if (!file.exists)
        // alternative path
        file = File(path.replace('###', 'Presets.localized'));

    if (!file.exists)
        // failed
        return;

    return file;

};

Edit 2024-02-21: added @renél80416020's suggestion to allow preset folder name to be specified. Thanks René!

 

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Advocate ,
Feb 21, 2024 Feb 21, 2024

Copy link to clipboard

Copied

Bonjour Mark,

Toujours aussi perfectionniste.

Pour bien faire, j'ai ajouté une option:

Suivant le pays, le mot "symbols" change d'orthographe, pour moi c'est "symboles".

 

var symbolItem = addSymbolItemFromLibrary({
        presetName: 'Symboles',
        libraryName: 'Tiki',
        symbolName: 'Poisson',
    })

 

René

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Feb 21, 2024 Feb 21, 2024

Copy link to clipboard

Copied

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

- Mark

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Participant ,
Feb 21, 2024 Feb 21, 2024

Copy link to clipboard

Copied

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 

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Feb 21, 2024 Feb 21, 2024

Copy link to clipboard

Copied

LATEST

Thanks for the update @MidoSemsem!.

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines