That is really good timing of yours!
Thank you very much for sharing your code, however I seem to be too dumb to implement it into my tryâŚ
Would you maybe be able to go the extra mile and tell me whatâs wrong?
I now do not get any reaction when pushing a button, nor do I get any console log from the UXP developer tool. I am sorry for not being smarter.
I am sure its a really easy beginnerâs mistake:
// #### helper functions ####
function createOutputFolders(projectfolder){
try { new Folder(projectfolder + "09_Fuer_Exporter/936x676/").create(); } catch(e){}
try { new Folder(projectfolder + "09_Fuer_Exporter/1602x2000/").create(); } catch(e){}
try { new Folder(projectfolder + "09_Fuer_Exporter/2000x2000/").create(); } catch(e){}
}
function computeCropBounds(cropDirection, input_width, input_height, new_size){
switch (cropDirection) {
// Crop to the top left corner
case "TL": var left = 0; var top = 0; var right = new_size[0]; var bottom = new_size[1]; break;
// Crop to the top horizontally centered
case "T": var left = (input_width - new_size[0]) / 2; var top = 0; var right = input_width - left; var bottom = new_size[1]; break;
// Crop to the top right corner
case "TR": var left = input_width - new_size[0]; var top = 0; var right = input_width; var bottom = new_size[1]; break;
// Crop to the left side, vertically centered
case "ML": var left = 0; var top = (input_height - new_size[1]) / 2; var right = new_size[0]; var bottom = input_height - top; break;
// Crop to center
case "M": var left = (input_width - new_size[0]) / 2; var top = (input_height - new_size[1]) / 2; var right = input_width - left; var bottom = input_height - top; break;
// Crop to the right side, vertically centered
case "MR": var left = input_width - new_size[0]; var top = (input_height - new_size[1]) / 2; var right = input_width; var bottom = input_height - top; break;
// Crop to the bottom left corner
case "BL": var left = 0; var top = input_height - new_size[1]; var right = new_size[0]; var bottom = input_height; break;
// Crop to the bottom - horizontally centered
case "B": var left = (input_width - new_size[0]) / 2; var top = input_height - new_size[1]; var right = input_width - left; var bottom = input_height; break;
// Crop to the bottom right corner
case "BR": var left = input_width - new_size[0]; var top = input_height - new_size[1]; var right = input_width; var bottom = input_height; break;
}
return [left, top, right, bottom]
}
async function cropDocument(cropL, cropT, cropR, cropB, output_width, output_height) {
require("photoshop").app.activeDocument.crop({ left: cropL, top: cropT, right: cropR, bottom: cropB }, 0, output_width, output_height);
}
try {
const makeSnapshot = async (name) => {
return batchPlay(
[
{
_obj: "make",
_target: [
{
_ref: "snapshotClass",
},
],
from: {
_ref: "historyState",
_property: "currentHistoryState",
},
name: name,
using: {
_enum: "historyState",
_value: "fullDocument",
},
_isCommand: true,
_options: {},
},
],
{}
);
};
const revertToSnapshot = async (name) => {
return batchPlay(
[
{
_obj: "select",
_target: [
{
_ref: "snapshotClass",
_name: name,
},
],
_isCommand: true,
_options: {
dialogOptions: "dontDisplay",
},
},
],
{}
);
};
const saveJPEG = async (saveFolder, newFilename, quality) => {
try {
const doc = app.activeDocument;
const theNewFile = await saveFolder.createFile(newFilename, {
overwrite: true,
});
const saveFile = await lfs.createSessionToken(theNewFile);
return batchPlay(
[
{
_obj: "save",
as: {
_obj: "JPEG",
extendedQuality: quality,
matteColor: {
_enum: "matteColor",
_value: "none",
},
},
in: {
_path: saveFile,
_kind: "local",
},
documentID: doc.id,
lowerCase: true,
saveStage: {
_enum: "saveStageType",
_value: "saveBegin",
},
_isCommand: true,
_options: {
dialogOptions: "dontDisplay",
},
},
],
{}
);
} catch (error) {
console.log(error);
}
};
} catch (error) {
console.error(error);
console.trace(error);
}
// #### handle click events ####
function handleClick(cropDirection) {
(async () => {
const app = require("photoshop").app;
const batchPlay = require("photoshop").action.batchPlay;
const lfs = require("uxp").storage.localFileSystem;
var document = app.activeDocument;
var input_width = document.width;
var input_height = document.height;
var filepath = String(document.path);
var projectfolder = filepath.substr(0, filepath.lastIndexOf("02_Capt_Export"));
//alert(projectfolder);
createOutputFolders(projectfolder);
///////////////////////////////////////////////////////////////////// 936*676 /////////////////////////////////////////////////////////////////////
await makeSnapshot("start");
var factor = input_width / 936;
var temp_height = 676 * factor;
var new_size = [input_width, temp_height];
if (temp_height > input_height){ // if input image is really wide, but not very tall, otherwise it would crop white onto the top and bottom
factor = input_height / 676;
temp_width = 936 * factor;
new_size = [temp_width, input_height];
}
var cropBounds = computeCropBounds(cropDirection, input_width, input_height, new_size);
require("photoshop").core.executeAsModal(() => cropDocument(cropBounds[0], cropBounds[1], cropBounds[2], cropBounds[3], 936, 676));
await saveJPEG(projectfolder + "09_Fuer_Exporter/" + document.width + "x" + document.height + "/", document.name, 10);
//////////////////////////////////////////////////////////////////// 1602*2000 ////////////////////////////////////////////////////////////////////
await revertToSnapshot("start");
// REVERT THE IMAGE
var factor = input_height / 2000;
var temp_width = 1602 * factor;
var new_size = [temp_width, input_height];
if (temp_width > input_width){ // if input image is really tall, but not very wide, otherwise it would crop white onto the sides
factor = input_width / 1602;
temp_height = 2000 * factor;
new_size = [input_width, temp_height]
}
var cropBounds = computeCropBounds(cropDirection, input_width, input_height, new_size);
require("photoshop").core.executeAsModal(() => cropDocument(cropBounds[0], cropBounds[1], cropBounds[2], cropBounds[3], 1602, 2000));
await saveJPEG(projectfolder + "09_Fuer_Exporter/" + document.width + "x" + document.height + "/", document.name, 10);
//////////////////////////////////////////////////////////////////// 2000*2000 ////////////////////////////////////////////////////////////////////
await revertToSnapshot("start");
var factor = input_width / 2000;
var temp_height = 2000 * factor;
var new_size = [input_width, temp_height];
if (temp_height > input_height){ // if input image is really wide, but not very tall, otherwise it would crop white onto the top and bottom
factor = input_height / 2000;
temp_width = 2000 * factor;
new_size = [temp_width, input_height];
}
var cropBounds = computeCropBounds(cropDirection, input_width, input_height, new_size);
require("photoshop").core.executeAsModal(() => cropDocument(cropBounds[0], cropBounds[1], cropBounds[2], cropBounds[3], 2000, 2000));
await saveJPEG(projectfolder + "09_Fuer_Exporter/" + document.width + "x" + document.height + "/", document.name, 10);
//document.closeWithoutSaving();
})();
}
// #### start ####
document.getElementById("btnCropTL").addEventListener("click", () => handleClick("TL"));
document.getElementById("btnCropT").addEventListener("click", () => handleClick("T"));
document.getElementById("btnCropTR").addEventListener("click", () => handleClick("TR"));
document.getElementById("btnCropML").addEventListener("click", () => handleClick("ML"));
document.getElementById("btnCropM").addEventListener("click", () => handleClick("M"));
document.getElementById("btnCropMR").addEventListener("click", () => handleClick("MR"));
document.getElementById("btnCropBL").addEventListener("click", () => handleClick("BL"));
document.getElementById("btnCropB").addEventListener("click", () => handleClick("B"));
document.getElementById("btnCropBR").addEventListener("click", () => handleClick("BR"));
(Setting the api version to 2 makes the plugin unloadable, but I at least set it to Version 5)
Maybe its not a good idea to require batchPLay and lfs inside an asynchronous function?