Skip to main content
Participating Frequently
September 21, 2023
Question

illustrator javascript export jpg spaces replaced by a dash

  • September 21, 2023
  • 1 reply
  • 305 views

The code works very well. The only problem is that there is a hyphen instead of a space. For example,

my-picture-test.jpg.
Is there any way to solve this?

 

 

var doc = app.activeDocument;
exportFileToJPEG();
function exportFileToJPEG() {
    var newpath = doc.path + "/" + doc.name.substring(0, doc.name.length - 2 + "jpg");
    var resolution = 300;
    var exportOptions = new ExportOptionsJPEG();
    var type = ExportType.JPEG;
    var fileSpec = new File(newpath);/// + "jpg");
    exportOptions.antiAliasing = true;
    exportOptions.artBoardClipping = true;
    exportOptions.horizontalScale = resolution * 100 / 72;
    exportOptions.verticalScale = resolution * 100 / 72;
    exportOptions.qualitySetting = 90;
    app.activeDocument.exportFile(fileSpec, type, exportOptions);
}

 

 

This topic has been closed for replies.

1 reply

jduncan
Community Expert
Community Expert
September 21, 2023

Just go ahead and add in the hypens before saving, then rename the saved JPG back to the original name without spaces.

var doc = app.activeDocument;
exportFileToJPEG();
function exportFileToJPEG() {
  var newName = doc.name.replace(/\.[a-zA-Z]{2,3}$/, ".jpg");
  var newNameWithHyphens = newName.replace(/\s/g, "-");
  var fileSpec = new File(doc.path + "/" + newNameWithHyphens);
  var resolution = 300;
  var exportOptions = new ExportOptionsJPEG();
  var type = ExportType.JPEG;
  exportOptions.antiAliasing = true;
  exportOptions.artBoardClipping = true;
  exportOptions.horizontalScale = (resolution * 100) / 72;
  exportOptions.verticalScale = (resolution * 100) / 72;
  exportOptions.qualitySetting = 90;
  app.activeDocument.exportFile(fileSpec, type, exportOptions);
  fileSpec.rename(newName);
}

 

Participating Frequently
September 22, 2023

Exactly what I was looking for, thank you