Skip to main content
Known Participant
January 22, 2024
Answered

script to save as JPEG in photoshop not working!

  • January 22, 2024
  • 2 replies
  • 628 views

Hi guys,

 

I am new at scripting for Photoshop. 

 

I am using this code for saving as a JPEG but it isn't working:

 

open(new File("C:\\Users\\USER\\Downloads\\Adobe Scripting\\photoshop file test for saveAs.psd"))
jpgFile = new File ("/Temp001.jpeg")
jpgSaveOptions = new JPEGSaveOptions ()
jpgSaveOptions.embedColorProfile = true
jpgSaveOptions.formatOptions = FormatOptions.STANDARDBASELINE
jpgSaveOptions.matte = MatteType.NONE
jpgSaveOptions.quality = 1
app.activeDocument.saveAs (jpgFile, jpgSaveOptions, true, Extension.LOWERCASE)
 
When I run it in VS Code, this is the error I get in Photoshop:
 

 

What is wrong with my code? Please help!

 

Best regards,

Joseph 

This topic has been closed for replies.
Correct answer Stephen Marsh

You can use a relative path (to the logged-in user account ~) for the open step:

 

open(new File("~\\Downloads\\Adobe Scripting\\photoshop file test for saveAs.psd"));

 

Or ExtendScript cross-platform single forward slash directory separators in relative path, rather than escaped Windows OS double back slashes:

 

open(new File("~/Downloads/Adobe Scripting/photoshop file test for saveAs.psd"));

 

Try saving to a user folder such as the Desktop or Downloads rather than to the root system level. Also, even though a four letter JPEG extension is valid, Photoshop doesn't like them, so use a three letter JPG extension instead:

 

jpgFile = new File ("~/Desktop/Temp001.jpg");
jpgSaveOptions = new JPEGSaveOptions();
jpgSaveOptions.embedColorProfile = true;
jpgSaveOptions.formatOptions = FormatOptions.STANDARDBASELINE;
jpgSaveOptions.matte = MatteType.NONE;
jpgSaveOptions.quality = 1;
app.activeDocument.saveAs(jpgFile, jpgSaveOptions, true, Extension.LOWERCASE);

 

2 replies

PedroMacro
Participating Frequently
January 22, 2024

Hi, i made this script some weeks ago, it may help

// Check if Adobe Photoshop application is open
if (app && app.name === "Adobe Photoshop") {
  // Get the active document
  var doc = app.activeDocument;
  
  // Check if a document is open
  if (doc) {
    // Set the location where you want to save the JPEG file
    var saveFolderPath = "C:\\Users\\User\\Desktop\\test\\";
    
    // Check if the save path exists
    var saveFolder = new Folder(saveFolderPath);
    if (!saveFolder.exists) {
      alert("The specified save folder does not exist.");
    } else {
      // Get the document name
      var docName = doc.name;
      
      // Remove the file extension from the document name, if present
      var fileName = docName.replace(/\.[^\.]+$/, "");
      
      // Define the full path for the JPEG file
      var savePath = saveFolderPath + fileName + ".jpeg";
      
      // Save the document at the specified location as JPEG
      var jpegOptions = new JPEGSaveOptions();
      jpegOptions.quality = 10; // Adjust the quality as needed (from 0 to 12)
      doc.saveAs(new File(savePath), jpegOptions, true);
    }
  } else {
    alert("No document is open in Photoshop.");
  }
} else {
  alert("Adobe Photoshop is not open.");
}

 

 

Known Participant
January 23, 2024

Thanks Pedro. Let me check it out. 

Stephen Marsh
Community Expert
Stephen MarshCommunity ExpertCorrect answer
Community Expert
January 22, 2024

You can use a relative path (to the logged-in user account ~) for the open step:

 

open(new File("~\\Downloads\\Adobe Scripting\\photoshop file test for saveAs.psd"));

 

Or ExtendScript cross-platform single forward slash directory separators in relative path, rather than escaped Windows OS double back slashes:

 

open(new File("~/Downloads/Adobe Scripting/photoshop file test for saveAs.psd"));

 

Try saving to a user folder such as the Desktop or Downloads rather than to the root system level. Also, even though a four letter JPEG extension is valid, Photoshop doesn't like them, so use a three letter JPG extension instead:

 

jpgFile = new File ("~/Desktop/Temp001.jpg");
jpgSaveOptions = new JPEGSaveOptions();
jpgSaveOptions.embedColorProfile = true;
jpgSaveOptions.formatOptions = FormatOptions.STANDARDBASELINE;
jpgSaveOptions.matte = MatteType.NONE;
jpgSaveOptions.quality = 1;
app.activeDocument.saveAs(jpgFile, jpgSaveOptions, true, Extension.LOWERCASE);

 

Known Participant
January 23, 2024

Hi Stephen. Let me try this out.