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

SaveAs JPEG not working in script - HELP!

Community Beginner ,
Jan 22, 2023 Jan 22, 2023

Copy link to clipboard

Copied

Hi, I am working on a script to add text to a series of JPG files in a folder and overwrite those jpg files with a jpeg quality of 10. I am SOOOOO close to getting it to work but for some reason, the file won't save as JPEG it continues to save as PSD. I am new and trying t use ChatGPT but this is as far as I've gotten, any help is appreciated, thanks.

 

// Declare variables for the text and the text layer
var text, textLayer;

// Open a window for the user to enter text
text = prompt("Enter some text:", "Type your text here");

// Define the folder where the files are located
var sourceFolder = Folder.selectDialog("Select the source folder:");

// Get a list of the files in the folder
var fileList = sourceFolder.getFiles();

// Save folder
var saveFolder = sourceFolder.parent;

// Loop through the list of files
for (var i = 0; i < fileList.length; i++) {
  // Open the file
  var file = open(fileList[i]);

  // Create a new text layer
  var textLayer = file.artLayers.add();
  textLayer.kind = LayerKind.TEXT;
  textLayer.textItem.contents = text;
  textLayer.textItem.position = Array(10, 1064);
  textLayer.textItem.size = 11;
  textLayer.textItem.font = "Californian FB Bold";
  textLayer.textItem.textcolor =  new SolidColor();
  textLayer.textItem.color.rgb.red = 230;
  textLayer.textItem.color.rgb.green = 10;
  textLayer.textItem.color.rgb.blue = 10;

// Set the name of the text layer to "ID"
textLayer.name = "ID";


// Find the layer named "ID"
var idLayer = activeDocument.layers.getByName("ID");

// Set the text of the "ID" layer to the user's text
idLayer.textItem.contents = text;

// Save the file
var jpegOptions = new JPEGSaveOptions();
jpegOptions.quality = 10; // really high
file.saveAs(new File(saveFolder + "/modified_file" + i +".jpg"), jpegOptions, true);

file.close();
}

 

TOPICS
Windows

Views

457

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 2 Correct answers

Community Expert , Jan 22, 2023 Jan 22, 2023

You need to flatten it, or change your preferences so that it will save the old way and not save as copy. You could also script it to save as a copy, if you don't want to or can't change the preferences. 

Votes

Translate

Translate
LEGEND , Jan 23, 2023 Jan 23, 2023

Can you use this as part of an action? Run the script and have the action flatten and save.

Votes

Translate

Translate
Adobe
Community Expert ,
Jan 22, 2023 Jan 22, 2023

Copy link to clipboard

Copied

You need to flatten it, or change your preferences so that it will save the old way and not save as copy. You could also script it to save as a copy, if you don't want to or can't change the preferences. 

Votes

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
LEGEND ,
Jan 23, 2023 Jan 23, 2023

Copy link to clipboard

Copied

Can you use this as part of an action? Run the script and have the action flatten and save.

Votes

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 ,
Apr 02, 2023 Apr 02, 2023

Copy link to clipboard

Copied

Well, what you guys provided solved the issue but now I'm on a new PC and I updated photoshop and I get a different error of user canceled action line 69.

quote

// Declare variables for the text and the text layer
var text, textLayer;

// Open a window for the user to enter text
text = prompt("Enter some text:", "Type your text here");

// Define the folder where the files are located
var sourceFolder = Folder.selectDialog("Select the source folder:");

// Get a list of the files in the folder
var fileList = sourceFolder.getFiles();

// Save folder
var saveFolder = sourceFolder.parent;

// Loop through the list of files
for (var i = 0; i < fileList.length; i++) {
// Open the file
var file = open(fileList[i]);

// Create a new text layer
var textLayer = file.artLayers.add();
textLayer.kind = LayerKind.TEXT;
textLayer.textItem.contents = text;
textLayer.textItem.position = Array(10, 1064);
textLayer.textItem.size = 11;
textLayer.textItem.font = "Californian FB Bold";
textLayer.textItem.textcolor = new SolidColor();
textLayer.textItem.color.rgb.red = 230;
textLayer.textItem.color.rgb.green = 10;
textLayer.textItem.color.rgb.blue = 10;

// Set the name of the text layer to "ID"
textLayer.name = "ID";

// Find the layer named "ID"
var idLayer = activeDocument.layers.getByName("ID");

// Set the text of the "ID" layer to the user's text
idLayer.textItem.contents = text;


var idLayer = file.artLayers.getByName("ID");
if (idLayer.layerEffects !== undefined) {
var dropShadow = idLayer.layerEffects.add();
dropShadow.enabled = true;
dropShadow.name = "Drop Shadow";
dropShadow.mode = LayerEffectMode.NORMAL;
dropShadow.distance = 5;
dropShadow.spread = 0;
dropShadow.size = 1;
dropShadow.color = new SolidColor();
dropShadow.color.rgb.red = 0;
dropShadow.color.rgb.green = 0;
dropShadow.color.rgb.blue = 0;
}

// Pause for 5 seconds
//$.sleep(1000);

//Flatten Image
file.flatten();

// Save the file
var jpegOptions = new JPEGSaveOptions();
jpegOptions.quality = 10;
jpegOptions.embedColorProfile = true;
jpegOptions.formatOptions = FormatOptions.STANDARDBASELINE;
file.save(SaveOptions.SAVECHANGES, jpegOptions);
file.close(SaveOptions.DONOTSAVECHANGES);
}

Can you use this as part of an action? Run the script and have the action flatten and save.


By @Lumigraphics

 

Votes

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 Expert ,
Apr 03, 2023 Apr 03, 2023

Copy link to clipboard

Copied

I'll take a look as t the code when I have time... Also consider Save for Web as an alternative option to get a JPEG.out.

 

EDIT: There are only 58 lines of code, not sure why you get an error for line 69?

 

I tested in v2021 and 2023 without error.

 

BTW: The text is placed off the canvas of my random test image. Unless all your images are the same size, I'd suggest that you add code to align the text to the canvas (lower left?) and to then move it right and up as required.

Votes

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
LEGEND ,
Apr 03, 2023 Apr 03, 2023

Copy link to clipboard

Copied

LATEST

Honestly your script is terrible. You need some error checking and better flow control. I'd consider this a bare starter that needs refinement to actually use.

Votes

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