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

Script to save different Filetypes with rename.

New Here ,
Oct 24, 2018 Oct 24, 2018

Hello,

I'm trying to create a script to perform the following action

  1. Save current AI file with "_PRINT" appended in the file name
  2. Hide layer called "INFO"
  3. Save (smallest file size) PDF with "_PREVIEW" appended in the file name
    1. this requires the current re-saved "_PRINT" name to be removed before adding "_PREVIEW" or it would result in "_PRINT_PREVIEW" stacked file name.

I have been trying to work off an existing script below i found but am having some trouble.

#target illustrator 

function test(){ 

  var doc = app.activeDocument; 

  var parentPath = Folder(File(doc.fullName).parent); 

  var opts = new PDFSaveOptions(); 

  opts.pDFPreset = "[Smallest File Size]"; 

  doc.saveAs(File(parentPath + "/" + doc.name.replace(/\.\w+$/, "_PREVIEW.pdf")), opts); 

}; 

test();

any help would be appreciated, thank you!

TOPICS
Scripting
2.8K
Translate
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 , Oct 24, 2018 Oct 24, 2018

Try this out robomike​

I tried to put in comments to explain each line the best I could.

#target illustrator-23

// The active document

var doc = app.activeDocument;

// The path of the original document

var originalDocPath = doc.path;

// The name of the original document

var originalDocName = doc.name;

// An array of all the layers in the active document

var allLayers = doc.layers;

// Get just the file name. Ignore the file extension .pdf and .ai

originalDocName = originalDocName.replace(/\.pdf|\.ai/gi, "")

/

...
Translate
Adobe
Community Expert ,
Oct 24, 2018 Oct 24, 2018

Hi robomike​,

these snippets should help you out.

2. hide "Info" layer

var IfoLay = doc.layers.getByName("Info");

IfoLay.visible = false;

redraw();

3.1. save as with new name and suffix

doc.saveAs(File(parentPath + "/" + doc.name.replace("_PRINT.ai", "_PREVIEW.pdf")), opts);

If so, have fun

Translate
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 ,
Oct 24, 2018 Oct 24, 2018

Try this out robomike​

I tried to put in comments to explain each line the best I could.

#target illustrator-23

// The active document

var doc = app.activeDocument;

// The path of the original document

var originalDocPath = doc.path;

// The name of the original document

var originalDocName = doc.name;

// An array of all the layers in the active document

var allLayers = doc.layers;

// Get just the file name. Ignore the file extension .pdf and .ai

originalDocName = originalDocName.replace(/\.pdf|\.ai/gi, "")

// Save the active document as a PDF with _PRINT

doc.saveAs(File(originalDocPath + "/" + originalDocName + "_PRINT.pdf"));

// Look through all layers

for (i = 0; i < allLayers.length; i++) {

    // Find the layer named INFO

    if (allLayers.name == "INFO") {

        // Found the layer named INFO. Now turn it off

        allLayers.visible = false;

    }

// Setup pdf save options

var opts = new PDFSaveOptions();

// Use the preset named Smallest File Size

opts.pDFPreset = "[Smallest File Size]";

// Save the document in the original folder using the original name with the INFO layer off as a PDF with _PREVIEW

doc.saveAs(File(originalDocPath + "/" + originalDocName + "_PREVIEW.pdf"), opts);

}

Translate
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
New Here ,
Oct 24, 2018 Oct 24, 2018

Thank you!

I just tested it and it works, what's interesting is that the section below says it will push out a PDF but it actually creates a AI instead. BUT, the AI file is what i wanted in the first place so it's all good, unless i'm misunderstanding the script.

// Save the active document as a PDF with _PRINT 

doc.saveAs(File(originalDocPath + "/" + originalDocName + "_PRINT.pdf"));

really appreciate it!

Translate
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 ,
Oct 24, 2018 Oct 24, 2018

That was my mistake... I intended to save it as an AI file. I would consider using the code snippet that pixxxel schubser​ provided for the info layer part. That will be much more efficient than looping through all the layers like mine. I updated the script correcting my .ai note and file extension... and added in the more efficient layer snippet from pixxxel schubser.

#target illustrator-23

// The active document

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, "")

// Save the active document as AI with _PRINT

doc.saveAs(File(originalDocPath + "/" + originalDocName + "_PRINT.ai"));

// Get this specific layer

var IfoLay = doc.layers.getByName("INFO");

// Turn it off

IfoLay.visible = false;

// Refresh the artboard

redraw(); 

// Setup pdf save options

var opts = new PDFSaveOptions();

// Use the preset named Smallest File Size

opts.pDFPreset = "[Smallest File Size]";

// Save the document in the original folder using the original name with the INFO layer off as a PDF with _PREVIEW

doc.saveAs(File(originalDocPath + "/" + originalDocName + "_PREVIEW.pdf"), opts);

Translate
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 ,
Oct 24, 2018 Oct 24, 2018

What is your mothers language?

opts.pDFPreset = "[Smallest File Size]";

Do you have a PDF preset with the exact name [Smallest File Size] ?

Translate
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 ,
Oct 24, 2018 Oct 24, 2018

It's built into the Save Adobe PDF menu for presets out of Illustrator isn't it?

Translate
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 ,
Oct 24, 2018 Oct 24, 2018

Not you subieguy2​

(I meant the mother language of the TO)

Translate
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 ,
Oct 24, 2018 Oct 24, 2018

   

Translate
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 ,
Oct 24, 2018 Oct 24, 2018
LATEST

Here is the German version for example

PDF-saveOPts.png

Translate
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