Copy link to clipboard
Copied
I'm trying to write a script that renames the artboard with the current documents filename. I've adapted this which works well but it always adds the file extension ".ai" as part of the artboard name. Is it possible to remove the .ai or not add it as part of the script? i.e. filename is "6449456.ai" and artboard is named "6449456"
function artBoardName_serial() {
var doc = app.activeDocument;
for (var i = 0, l = doc.artboards.length; i < l; i++) {
if (doc.artboards[i].active) {
}
// THIS IS WHERE THE RENAMING HAPPENS
doc.artboards[i].name = doc.name;
}
}
function renameArtBoard_MAIN() {
if (app.documents.length == 0) {
alert("No Open / Active Document Found");
} else {
artBoardName_serial();
}
}
renameArtBoard_MAIN();
Copy link to clipboard
Copied
change this line:
doc.artboards[i].name = doc.name;
to this:
doc.artboards[i].name = doc.name.replace(".ai","");
Copy link to clipboard
Copied
Hi,
You can use split function to split as below
doc.artboards[i].name = doc.name.split(".")[0];
Thanks
Copy link to clipboard
Copied
for my interest only:
Copy link to clipboard
Copied
It's a small script as part of an action to export a copy of the PDF to a completed jobs folder for reference. The script I'm using to export the PDF saves the file using the name of the artboard so this just saves me naming it manually. I tried using the save a copy command but it kept overwriting the files each time.
Copy link to clipboard
Copied
Thank you for your answer. But unfortunately you didn't answer my questions.
Is that an action-calls-a-script-that-calls-an-action-that-…-game?
For now I guess the following should answer your first question (without to loop through all artboards) :
var aDoc = app.activeDocument;
var AB = aDoc.artboards;
AB[AB.getActiveArtboardIndex ()].name = aDoc.name.replace (/\..{2,3}$/, "");
If that works for you
have fun
😉
Copy link to clipboard
Copied
Bonjour à tous
Disposant d'un peu de temps (cause confinement), j'ai concocté un petit script.
J'ai fait au plus bref.
Je voulais simplement signaler (surtout aux débutants) qu'il ne faut pas oublier les fonctions de base des chaînes de caractères (String).
Pour le problème du nom du document après extraction de l'extension, une solution est bien adaptée aux noms farfelus que l'on trouve chez certains clients, qui sont parfois dus à des erreurs de frappe. (sachant par exemple que le point est autorisé dans les noms de fichiers)
Quand j'ai réalisé mes premiers scripts, j'ai d'abords étudier le langage JavaSript qui s'apparente à d'autres langages que j'avais pratiqués auparavant. Bref, ensuite je suis passé à extendScript pour Illustrator.
Par prudence j'utilise systématiquement: doc.name.substring(0,doc.name.lastIndexOf('.'))
Pour le petit script qui suit, il s'agit d'un programme de teste.
J'utilise un tableau à deux dimensions où chaque élément est un tableau composé de deux chaînes de caractères.
Un nom de fichier puis la méthode d'extraction à utiliser.
exemple pour le premier élément:
["ellipse.ai[converti]a.ai","docName.replace('.ai','');"]
Vous pouvez en ajouter d'autres dans la liste, mais ne modifiez pas tout ce qui concerne les dimensions.
La police pour les textes est celle par défaut (Illustrator CS6) MyriadPro-Regular.
Vous pouvez ajoutez la ligne suivante:
pointText.textRange.textFont = app.textFonts["MyriadPro-Regular"];
avant de lancer le script: Fenêtre>Plans de travail, un nouveau document sera créé.
//ab name3.jsx
// regard Landry Rene Wed, 6 May 2020 20:15:57 GMT
#target illustrator
function artBoardName_serial_test() {
var limit = 8, info = true, doc, dx = 13, dy = 50, corps = 24, docName, juste, text;
var docnamelists = [ // nom de fichier et methode
["ellipse.ai[converti]a.ai","docName.replace('.ai','');"],
["ellipse.ai[converti]a.ai","docName.split('.')[0];"],
["ellipse.ai[converti]a.ai","docName.replace(/\\.ai$/i,'');"],
["ellipse.ai[converti]a.ai","docName.substring(0,docName.lastIndexOf('.'));"],
["ellipse.ai[converti]a..m-3u8","docName.substring(0,docName.lastIndexOf('.'));"],
["ellipse.ai[converti]a..m-3u8","docName.replace(/\\.{1}[\\w-]{1,}$/i,'');"],
["ellipse.ai[converti]a.m-3u8","docName.replace(/\\..{2,6}$/, '');"],
["ellipse.ai[converti]a..m-3u8","docName.replace(/\\.[\\w-]{2,}$/i,'');"]];
var couleur1 = macmykColor(100,0,100,0); // bonne reponse
var couleur2 = macmykColor(0,100,100,0); // extension
doc = app.documents.add(DocumentColorSpace.CMYK,155,80,docnamelists.length,DocumentArtboardLayout.Column,8);
if (docnamelists.length > limit) doc.rearrangeArtboards(DocumentArtboardLayout.GridByRow,2,8,true);
for (var i = 0; i < doc.artboards.length; i++) {
docName = docnamelists[i][0];
pos = docName.lastIndexOf('.');
juste = docName.substring(0,docName.lastIndexOf('.'));
doc.artboards[i].name = eval(docnamelists[i][1]);
if (info) {alert(docnamelists[i][1]+'\r'+docName+'\r'+doc.artboards[i].name);}
text = pointText(doc,dx,dy,docName+'\r'+doc.artboards[i].name,corps,i);
for (var k = pos; k < text.lines[0].textRanges.length; k++)
{text.lines[0].characters[k].fillColor = couleur2;}
if (doc.artboards[i].name == juste) {text.lines[1].fillColor = couleur1;}
}
function pointText(docRef,dx,dy,cont,corps,idx) {
var Origin, pointText;
docRef.artboards.setActiveArtboardIndex(idx);
Origin = docRef.rulerOrigin;
pointText = docRef.textFrames.pointText([-Origin[0]+dx,-Origin[1]+dy]);
pointText.contents = cont+"\r"+"ab["+idx+"] ou "+(idx+1)+"\tsolution "+(idx+1);
pointText.size = corps;
return pointText;
}
function macmykColor(c,m,j,k) {
var cmykColor = new CMYKColor();
with (cmykColor) {cyan = c; magenta = m; yellow = j; black = k;}
return cmykColor;
}
}
artBoardName_serial_test();
J'allais oubier la présence de deux \\ double antislash
pour les chaînes comme dans
var reg = new RegExp("[\\d]","g"); la chaîne interprétée crée le motif [\d]
De elleere.
Copy link to clipboard
Copied
As a designer who has come across this frustrating naming issue, I was thrilled to find your solution! I'm not sure why this has only recently become a problem for me, I never had problems with my file names in the past (and I've been using Illustrator for over 25 years). I often use duplicated files to work off of which have conflicting artboard/file names. I've always just hit "export" and the file would be named... well, the file name. But recently, perhaps in the last year or so, I've noticed that when I select "export" I have to use the artboard so that my exported file matches the dimensions of my artboard, but I hate that it's naming my file, my artboard name. Gah! This is a nice feature if you're exporting multiple artboards, I suppose, but often, I'm exporting one at a time.
Anyhoo, thanks for the helpful script!