Script add-on that will rename an output pdf file

Community Beginner ,
Jan 12, 2021 Jan 12, 2021

Copy link to clipboard

Copied

I need some help with adding a snippet to my funcional pdf export plugin that would automatically rename  the output pdf file. I tried my best over the weekend and with all the time invested I couldn't make it work. 

The very basic plugin I have been using (see below) creates a pdf copy out of the artboard #1, and saves it under the name of the active file, into the home folder of active file. That's exactly what I need, but I have to go into Finder each time and manually change the name of the new created pdf file. So instead of having plugin automatically creating File.pdf I need it to create File-Placard.pdf. Basically renaming it and adding "-Placard" to the active file name. Everything I tried wouldn't work 100%. 

If anyone has an idea how to make this happen please let me know. 

Thank you for all your help. 

MarkA

#target "illustrator"
var aiApp = app.activeDocument;

dest = aiApp.fullName

if (app.documents.length > 0) {
    var saveOpts = new PDFSaveOptions();
    var fileSpec = new File(dest);
    	saveOpts = new PDFSaveOptions(); 
	saveOpts.artboardRange = 1;
	saveOpts.compatibility = PDFCompatibility.ACROBAT5; 
	saveOpts.generateThumbnails = true; 
	saveOpts.preserveEditability = false;
	saveOpts.pDFPreset = 'Placard';
    aiApp.saveAs(fileSpec, saveOpts)
} 
TOPICS
Scripting

Views

100

Likes

translate

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 1 Correct Answer

Engaged , Jan 12, 2021 Jan 12, 2021
Well done! That's much better that you worked it our yourself and I am glad you are using regular expressions! For the sake of extra learning, here's what I was about to reply, which is a slightly more succinct version of yours. It uses a more complex regular expression that looks for the last period in a string (and anything after it). var suffix = '-Placard'; if (app.documents.length > 0) { var doc = app.activeDocument; dest = String(doc.fullName).replace(/(.(?!\.))+$/, suffix); v...

Likes

translate

Translate

Translate
Community Beginner ,
Jan 12, 2021 Jan 12, 2021

Copy link to clipboard

Copied

OK, as soon as I posted this I figured out exactly what I needed. 

 

#target "illustrator"
var doc = app.activeDocument;
// The path of the original document  
var originalDocPath = doc.path;  
// The name of the original document  
var originalDocName = doc.name;
// Get just the file name. Ignore the file extension .pdf and .ai  
originalDocName = originalDocName.replace(/\.pdf|\.ai/gi, "")
// Get just the file name. Ignore the file extension .pdf and .ai  
originalDocName = originalDocName.replace(/\.pdf|\.ai/gi, "")  
 
{
    var saveOpts = new PDFSaveOptions();
saveOpts = new PDFSaveOptions(); 
saveOpts.artboardRange = 1;
saveOpts.compatibility = PDFCompatibility.ACROBAT5; 
saveOpts.generateThumbnails = true; 
saveOpts.preserveEditability = false;
saveOpts.pDFPreset = 'Placard';
 
// Save the document in the original folder using the original name with _LR suffix  
doc.saveAs(File(originalDocPath + "/" + originalDocName + "-Placard.pdf"), saveOpts);  
app.activeDocument.close(SaveOptions.DONOTSAVECHANGES);

Likes

translate

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
Engaged ,
Jan 12, 2021 Jan 12, 2021

Copy link to clipboard

Copied

Well done! That's much better that you worked it our yourself and I am glad you are using regular expressions! For the sake of extra learning, here's what I was about to reply, which is a slightly more succinct version of yours. It uses a more complex regular expression that looks for the last period in a string (and anything after it).

 

var suffix = '-Placard';

if (app.documents.length > 0) {
    var doc = app.activeDocument;
    dest = String(doc.fullName).replace(/(.(?!\.))+$/, suffix);
    var saveOpts = new PDFSaveOptions();
    var fileSpec = new File(dest);
    saveOpts = new PDFSaveOptions();
    saveOpts.artboardRange = 1;
    saveOpts.compatibility = PDFCompatibility.ACROBAT5;
    saveOpts.generateThumbnails = true;
    saveOpts.preserveEditability = false;
    saveOpts.pDFPreset = 'Placard';
    aiApp.saveAs(fileSpec, saveOpts)
}

 

- Mark 

Likes

translate

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 Beginner ,
Jan 13, 2021 Jan 13, 2021

Copy link to clipboard

Copied

Looks great. Thank you very much. 

Likes

translate

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