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 the “Tiki” Library.
Any help is appreciated. Thanks!”
2 Correct answers
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
...
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
...
Explore related tutorials & articles
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);
}
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?
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;
}
// -------
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");
}
})();
Copy link to clipboard
Copied
Super!
Bien pour:
var symFilePath = app.path + "/Presets/"+ app.locale +"/Symbols";
René
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é!
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é
Copy link to clipboard
Copied
Thank you René, I will do as you wisely suggest and update the script above.
- Mark
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
Copy link to clipboard
Copied
Thanks for the update @MidoSemsem!.

