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

I don´t know where to paste a code to make it run

New Here ,
Jun 18, 2023 Jun 18, 2023

Copy link to clipboard

Copied

Hi, I created a code which resizes one image into 24 different sizes, so I would get 24 files but of the same initial image. The problem is I dont know how to run the code and where I should paste it. Heres the code:

// Establish the base directory
var basePath = Folder.selectDialog("Select the base directory");

// Check if a directory was selected
if (basePath != null) {
// Get source file
var sourceFile = File.openDialog("Select the source file", "*.*", false);

// Check if a source file was selected
if (sourceFile != null) {
// Open source file
app.open(sourceFile);
var sourceDoc = app.activeDocument.duplicate(); // Duplicate the document to prevent from any changes to the original one.

// Create folders for each aspect ratio
var ratios = ["2x3", "4x3", "4x5", "5x7", "11x14"];
for (var i = 0; i < ratios.length; i++) {
var ratio = ratios[i];
var ratioFolder = new Folder(basePath + "/" + ratio);
if (!ratioFolder.exists) {
ratioFolder.create();
}

// Get sizes corresponding to each aspect ratio
var sizes = getSizesForRatio(ratio);

// Resize and save images for each size
for (var j = 0; j < sizes.length; j++) {
var size = sizes[j];
var newSize = size.split("x");
var width = Number(newSize[0]);
var height = Number(newSize[1]);

// Resize image
var tempDoc = sourceDoc.duplicate(); // Create a temporary document for each ratio to avoid resizing the original one repeatedly.
tempDoc.resizeImage(UnitValue(width, "inch"), UnitValue(height, "inch"), null, ResampleMethod.BICUBIC);

// Change resolution to 300 PPI
tempDoc.resizeImage(null, null, 300, ResampleMethod.BICUBIC);

// Save image as PNG
var fileName = sourceFile.name.substring(0, sourceFile.name.lastIndexOf("."));
var newFileName = fileName + "_" + ratio + "_" + size + ".png";
var saveFile = new File(ratioFolder + "/" + newFileName);
var pngOptions = new PNGSaveOptions();
pngOptions.interlaced = false;
tempDoc.saveAs(saveFile, pngOptions, true, Extension.LOWERCASE);

// Close resized image without saving changes
tempDoc.close(SaveOptions.DONOTSAVECHANGES);
}
}

// Close original image without saving changes
sourceDoc.close(SaveOptions.DONOTSAVECHANGES);
}
}

// Function to return sizes for a given aspect ratio
function getSizesForRatio(ratio) {
switch (ratio) {
case "2x3":
return ["4x6", "6x9", "8x12", "10x15", "12x18", "16x24", "20x30", "24x36"];
case "4x3":
return ["6x8", "9x12", "12x16", "18x24"];
case "4x5":
return ["4x5", "8x10", "16x20"];
case "5x7":
return ["5x7", "A5", "A4", "A3", "A2", "A1"];
case "11x14":
return ["11x14"];
default:
return [];
}
}

TOPICS
Windows

Views

57

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
Adobe
Community Expert ,
Jun 18, 2023 Jun 18, 2023

Copy link to clipboard

Copied

LATEST
  1. Copy the code text to the clipboard
  2. Open a new blank file in a plain-text editor (not in a word processor)
  3. Paste the code in
  4. Save as a plain text format file – .txt
  5. Rename the saved file extension from .txt to .jsx
  6. Install or browse to the .jsx file to run (see below)

 

https://prepression.blogspot.com/2017/11/downloading-and-installing-adobe-scripts.html#Photoshop

 

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