Skip to main content
Inspiring
January 26, 2021
Answered

Issue with export to jpg script

  • January 26, 2021
  • 5 replies
  • 704 views

Hi,

I have some issues creating a snippet in my code to export the document as jpg.

// Exports current document to dest as a JPEG file with specified options,
// dest contains the full path including the file name
   var jpg_dest = export_folder  + Artikelnummer.Text + '.jpg';
function exportFileToJPEG(jpg_dest) {
 
    if (doc.length > 0) {
      var jpgexportOptions = new ExportOptionsJPEG();
      jpgexportOptions.antiAliasing = false;
      jpgexportOptions.qualitySetting = 70;
      jpgexportOptions.artBoardClipping = true;
      jpgexportOptions.optimization = true;  
      var jpgtype = ExportType.JPEG;
      var fileSpec = new File(jpg_dest);
  
   doc.exportFile(fileSpec, jpgtype, jpgexportOptions);
      codeStart++;
    } 
}


Here is the part where I try to export the jpg file. Followed the hole script:
Also link to an older post where I explain my goal - https://community.adobe.com/t5/illustrator/titelblock-fill-and-export/m-p/9951543?page=1#M101355

var w = new Window ("dialog", "Titelblock Eingabe");

    w.orientation = "column";

var odesigner = app.activeDocument.textFrames.getByName ("designer");
var ocreationdate = app.activeDocument.textFrames.getByName ("date_of_creation");
var otitle = app.activeDocument.textFrames.getByName ("Titel") ;
var opartnumber = app.activeDocument.textFrames.getByName ("partnumber") ;

    var groupgen = w.add("group",undefined,''); 

        groupgen.alignChildren = "fill"; 

        groupgen.orientation = "row";

               

    var group1 = groupgen.add("panel",undefined, "Erstellung"); 

        group1.orentation = "column"; 

        group1.alignChildren = "fill";     

    var group2 = groupgen.add("panel",undefined, "Nummerierung"); 

        group2.orentation = "column"; 

        group2.alignChildren = "fill";

    var todaydate = new Date();

        var dd = todaydate.getDate();

        var mm = todaydate.getMonth()+1; //January is 0!

        var yyyy = todaydate.getFullYear();

        if(dd<10) {

            dd = '0'+dd

        }

        if(mm<10) {

            mm = '0'+mm

        }

        todaydate= dd + '.' + mm + '.' + yyyy;

    var myDesigner = group1.add ("Panel", undefined, "Designer:");

        myDesigner.alignChildren = "fill";

        myDesigner.orientation      ="column";

        var Designer= myDesigner.add ("edittext", undefined, odesigner.contents);

            Designer.preferredSize = [250,23];

   

    var myDate           = group1.add ("Panel", undefined, "Erstellungsdatum");

           myDate.alignChildren = "fill";

        myDate.orientation      ="column";

        var Datum= myDate.add ("edittext", undefined, todaydate);

            Datum.preferredSize = [250,23];

    var myTitel = group1.add ("panel", undefined, "Titel:");

        myDesigner.alignChildren = "fill";

        myDesigner.orientation      ="column";

        var Titel= myTitel.add ("edittext", undefined, otitle.contents);

            Titel.preferredSize = [250,23];

           

      var myArtikelnummer = group2.add ("panel", undefined, "Artikelnummer:");

        myArtikelnummer.alignChildren = "fill";

        myArtikelnummer.orientation      ="column";

        var Artikelnummer= myArtikelnummer.add ("edittext", undefined, opartnumber.contents);

            Artikelnummer.preferredSize = [250,23];     

           

           

var myButtongroup = w.add("group");

    myButtongroup.alignment ="center";

    myButtongroup.add ("button", undefined, "OK");

    myButtongroup.add ("button", undefined, "Cancel");

   

    w.show ();

   

odesigner.contents = Designer.text;
ocreationdate.contents = Datum.text;
otitle.contents = Titel.text;
opartnumber.contents = Artikelnummer.text;

redraw();

//export

var doc = app.activeDocument; // Name of the active document 

var original_file = doc.fullName; // we need to store the original file and path 

var pdfOption = 'Adobe PDF-Vorgabe 1'; // Name of PDF settings  

var pdfSuff = '.pdf'; // Suffix for PDF 

var arr = doc.name.split("."); 

var extension = ""; 

if (arr.length>1) extension = "." + arr.pop(); 

var filename = arr.join("."); // Just generate a clean file name 

var name_pdfpreview = Artikelnummer.text + pdfSuff; // new name and suffix for PDF 

var export_folder = new  Folder ("C:/Users/fvo/Desktop/AR-Nummern Ablage/" + Artikelnummer.text + "/" ); // Define path where to save PDF 

if (!export_folder.exists)

  export_folder .create();



// Exports current document to dest as a JPEG file with specified options,
// dest contains the full path including the file name
   var jpg_dest = export_folder  + Artikelnummer.Text + '.jpg';
function exportFileToJPEG(jpg_dest) {
 
    if (doc.length > 0) {
      var jpgexportOptions = new ExportOptionsJPEG();
      jpgexportOptions.antiAliasing = false;
      jpgexportOptions.qualitySetting = 70;
      jpgexportOptions.artBoardClipping = true;
      jpgexportOptions.optimization = true;  
      var jpgtype = ExportType.JPEG;
      var fileSpec = new File(jpg_dest);
  
   doc.exportFile(fileSpec, jpgtype, jpgexportOptions);
      codeStart++;
    } 
}


//-------------------------------------------------------------------------------------------

//-------------------------------------------------------------------------------------------

 

// save as PDF and tell function which options to use and how to name the PDF 

function saveCopyAsPDF (setPDF, namePDF) { 

        var destFolder = null; 

            destFolder = export_folder; 

        var options = null;     

            options = new PDFSaveOptions (); 

            options.PDFPreset = setPDF; 

            options.viewAfterSaving = false; // just to be sure not to open Acrobat 

        var targetFile = null; 

            targetFile = new File (destFolder + "/" + namePDF);     

        doc.saveAs (targetFile, options); // here we save the PDF  

     //   doc.close (); // now we close it in Illustrator ... 

     //   app.open (File (original_file)); // ... and re-open the Illustrator file 

} 

saveJpg = function ( doc, filePath, options, artboardIndex, artboardName ) {
	var destFile = new File( export_folder + Artikelnummer.Text + '.jpg' );
    doc.exportFile(destFile, ExportType.JPEG , options);
}

saveCopyAsPDF (pdfOption, name_pdfpreview); 
This topic has been closed for replies.
Correct answer FrederikV

I have found the error... First of all, the filepath was wrong. I found this while exporting as png with a script. It created a png with Filename+undifined.png on top level in the folderstructure.

With fixing this and calling the function, it works just fine.

// Exports current document to dest as a JPEG file with specified options,
// dest contains the full path including the file name
   var jpg_dest = "C:/Users/fvo/Desktop/AR-Nummern Ablage/" + Artikelnummer.text + "/" +Artikelnummer.text + '.jpg';
function exportFileToJPEG(jpg_dest) {
 
    if (app.documents.length > 0) {
      var jpgexportOptions = new ExportOptionsJPEG();
      jpgexportOptions.antiAliasing = false;
      jpgexportOptions.qualitySetting = 70;
      jpgexportOptions.artBoardClipping = true;
      jpgexportOptions.optimization = true;  
      jpgexportOptions.ver
      var jpgtype = ExportType.JPEG;
      var fileSpec = new File(jpg_dest);
  
   app.activeDocument.exportFile(fileSpec, jpgtype, jpgexportOptions);
      
    } 
}

exportFileToJPEG(jpg_dest);

 

5 replies

FrederikVAuthorCorrect answer
Inspiring
January 27, 2021

I have found the error... First of all, the filepath was wrong. I found this while exporting as png with a script. It created a png with Filename+undifined.png on top level in the folderstructure.

With fixing this and calling the function, it works just fine.

// Exports current document to dest as a JPEG file with specified options,
// dest contains the full path including the file name
   var jpg_dest = "C:/Users/fvo/Desktop/AR-Nummern Ablage/" + Artikelnummer.text + "/" +Artikelnummer.text + '.jpg';
function exportFileToJPEG(jpg_dest) {
 
    if (app.documents.length > 0) {
      var jpgexportOptions = new ExportOptionsJPEG();
      jpgexportOptions.antiAliasing = false;
      jpgexportOptions.qualitySetting = 70;
      jpgexportOptions.artBoardClipping = true;
      jpgexportOptions.optimization = true;  
      jpgexportOptions.ver
      var jpgtype = ExportType.JPEG;
      var fileSpec = new File(jpg_dest);
  
   app.activeDocument.exportFile(fileSpec, jpgtype, jpgexportOptions);
      
    } 
}

exportFileToJPEG(jpg_dest);

 

FrederikVAuthor
Inspiring
January 27, 2021

Hi all and thank you for the answers.

I have first tried to call the function with  exportFileToJPEG(jpg_dest);
As this didn't work, I removed the function and this didn't work either.

Any ideas?

femkeblanco
Brainiac
January 26, 2021

Like @renél80416020 said, you've put the exportFile() function inside an exportFileToJPEG() function, but you do not call exportFileToJPEG().

FrederikVAuthor
Inspiring
January 26, 2021

As answered:
The script doesn't create the jpg. No Error occurs. Rest of the script works just fine.

renél80416020
Inspiring
January 26, 2021

Salut!

Ajoutez en ligne 74

exportFileToJPEG(jpg_dest);

femkeblanco
Brainiac
January 26, 2021

What's the problem? Do you get an error?

FrederikVAuthor
Inspiring
January 26, 2021

Oh man, I tried my best to explain the workflow and forgot the error.

My hole script creates the folder and the pdf correct, but no jpg is created. I also tried this snippet with png and it didn't work.
There is no error message.