Copy link to clipboard
Copied
Hello everyone! The title says it all.
How do I program in this script the unit of measure for centimeters in text boxes so that it preserves the resolution of the original of the image and also keeping the aspect ratio if it assigns a value in width or height.
#target photoshop
var d = new Window("dialog", "Fit Image CM", {x:0, y:0, width:260, height:105} ,{closeButton: true});
ctd_gp = d.add("group", {x:5, y:-10, width:260, height:105});
edt_w = ctd_gp.add("edittext", {x:53, y:25, width:50, height:10}, " 25,5"); // Set a size W
txt_w = ctd_gp.add("statictext", {x:10, y:30, width:120, height:10}, " Width: cm");
edt_h = ctd_gp.add("edittext", {x:53, y:55, width:50, height:10}, " 15"); // Set a size H
txt_h = ctd_gp.add("statictext", {x:10, y:60, width:120, height:10}, " Height: cm");
fit_bt = ctd_gp.add("button", {x:150, y:25, width:93,height:25}, "Fit", { name: "ok"});
fit_bt .onClick = function() {
d.close();
}
d.center();
d.show();
test it
...fit_bt.onClick = function()
{
try
{
if (!app.documents.length) return;
var fit_w = Number(edt_w.text.replace(/,/g, "."));
if (fit_w <= 0 || isNaN(fit_w)) { alert("wrong W"); return; }
var fit_h = Number(edt_h.text.replace(/,/g, "."));
if (fit_h <= 0 || isNaN(fit_h)) { alert("wrong H"); return; }
var old_units = app.preferences.rulerUnits;
app.preferences.rulerUnits = Units.CM;
var w = Number(activeDocument.width.value);
Copy link to clipboard
Copied
I'm sleepy so I don't know whether you want somethng like UnvitValue(10, 'cm') when for example rulers are set to pixels?
Copy link to clipboard
Copied
Here in my area it is not common to work with pixels.
It would be the same as this script however, I need the unit of measure to be in centimeters instead of pixels here in my script.
Copy link to clipboard
Copied
test it
fit_bt.onClick = function()
{
try
{
if (!app.documents.length) return;
var fit_w = Number(edt_w.text.replace(/,/g, "."));
if (fit_w <= 0 || isNaN(fit_w)) { alert("wrong W"); return; }
var fit_h = Number(edt_h.text.replace(/,/g, "."));
if (fit_h <= 0 || isNaN(fit_h)) { alert("wrong H"); return; }
var old_units = app.preferences.rulerUnits;
app.preferences.rulerUnits = Units.CM;
var w = Number(activeDocument.width.value);
var h = Number(activeDocument.height.value);
var w1;
var h1;
if (w/h > fit_w/fit_h)
{
w1 = fit_w;
}
else
{
h1 = fit_h;
}
d.close();
activeDocument.resizeImage(w1, h1);
w = Number(activeDocument.width.value);
h = Number(activeDocument.height.value);
w = Math.round(w*10)/10;
h = Math.round(h*10)/10;
app.preferences.rulerUnits = old_units;
if (w > fit_w || h > fit_h) { alert("Fit Error: " + w + " " + fit_w + " " + h + " " + fit_h); }
}
catch (e) { alert(e); }
}
Copy link to clipboard
Copied
Fantastic! Nice work @r-bin This will greatly improve my workflow.
Thank you!
Copy link to clipboard
Copied
I tested it and it worked very well, except when I add a higher value, I see that it did not change the size, was it your choice?
I added these 3 line of code before the function fit_bt.onClick = function () to have the current size of the document.
var doc = activeDocument;
app.preferences.rulerUnits = Units.CM;
edt_w.text =(doc.width.value.toFixed(2));
edt_h.text =(doc.height.value.toFixed(2));
If possible, could you correct this bug?
Thank you so much again.
Copy link to clipboard
Copied
Resize is done in any cases, for large and small values. And what do you want?
You can do a check for the fact that the size is already fitted and do nothing.
#target photoshop
var d = new Window("dialog", "Fit Image CM", {x:0, y:0, width:260, height:105} ,{closeButton: true});
ctd_gp = d.add("group", {x:5, y:-10, width:260, height:105});
edt_w = ctd_gp.add("edittext", {x:53, y:25, width:50, height:10}, " 25,5"); // Set a size W
txt_w = ctd_gp.add("statictext", {x:10, y:30, width:120, height:10}, " Width: cm");
edt_h = ctd_gp.add("edittext", {x:53, y:55, width:50, height:10}, " 15"); // Set a size H
txt_h = ctd_gp.add("statictext", {x:10, y:60, width:120, height:10}, " Height: cm");
fit_bt = ctd_gp.add("button", {x:150, y:25, width:93,height:25}, "Fit", { name: "ok"});
// your new code //////////
var doc = activeDocument;
app.preferences.rulerUnits = Units.CM;
edt_w.text =(doc.width.value.toFixed(2));
edt_h.text =(doc.height.value.toFixed(2));
//////////////////////////
fit_bt.onClick = function()
{
try
{
if (!app.documents.length) return;
var fit_w = Number(edt_w.text.replace(/,/g, "."));
if (fit_w <= 0 || isNaN(fit_w)) { alert("wrong W"); return; }
var fit_h = Number(edt_h.text.replace(/,/g, "."));
if (fit_h <= 0 || isNaN(fit_h)) { alert("wrong H"); return; }
var old_units = app.preferences.rulerUnits;
app.preferences.rulerUnits = Units.CM;
var w = Number(activeDocument.width.value);
var h = Number(activeDocument.height.value);
var w1;
var h1;
if (w/h > fit_w/fit_h) w1 = fit_w;
else h1 = fit_h;
d.close();
if ((w1 && Math.round(w*100)/100 != Math.round(fit_w*100)/100) || (h1 && Math.round(h*100)/100 != Math.round(fit_h*100)/100))
activeDocument.resizeImage(w1, h1);
else
alert("Already fitted!", "Warning")
w = Number(activeDocument.width.value);
h = Number(activeDocument.height.value);
w = Math.round(w*100)/100;
h = Math.round(h*100)/100;
app.preferences.rulerUnits = old_units;
if (w > fit_w || h > fit_h) { alert("Fit to:\t" + fit_w + " x " + fit_h + "\n\nResult:\t" + w + " x " + h, "Fit problem"); }
}
catch (e) { alert(e); }
}
d.center();
d.show();
What did not work for you I did not understand.
Copy link to clipboard
Copied
r-bin escreveu
Resize is done in any cases, for large and small values. And what do you want?
You can do a check for the fact that the size is already fitted and do nothing.
#target photoshop var d = new Window("dialog", "Fit Image CM", {x:0, y:0, width:260, height:105} ,{closeButton: true}); ctd_gp = d.add("group", {x:5, y:-10, width:260, height:105}); edt_w = ctd_gp.add("edittext", {x:53, y:25, width:50, height:10}, " 25,5"); // Set a size W txt_w = ctd_gp.add("statictext", {x:10, y:30, width:120, height:10}, " Width: cm"); edt_h = ctd_gp.add("edittext", {x:53, y:55, width:50, height:10}, " 15"); // Set a size H txt_h = ctd_gp.add("statictext", {x:10, y:60, width:120, height:10}, " Height: cm"); fit_bt = ctd_gp.add("button", {x:150, y:25, width:93,height:25}, "Fit", { name: "ok"}); // your new code ////////// var doc = activeDocument; app.preferences.rulerUnits = Units.CM; edt_w.text =(doc.width.value.toFixed(2)); edt_h.text =(doc.height.value.toFixed(2)); ////////////////////////// fit_bt.onClick = function() { try { if (!app.documents.length) return; var fit_w = Number(edt_w.text.replace(/,/g, ".")); if (fit_w <= 0 || isNaN(fit_w)) { alert("wrong W"); return; } var fit_h = Number(edt_h.text.replace(/,/g, ".")); if (fit_h <= 0 || isNaN(fit_h)) { alert("wrong H"); return; } var old_units = app.preferences.rulerUnits; app.preferences.rulerUnits = Units.CM; var w = Number(activeDocument.width.value); var h = Number(activeDocument.height.value); var w1; var h1; if (w/h > fit_w/fit_h) w1 = fit_w; else h1 = fit_h; d.close(); if ((w1 && Math.round(w*100)/100 != Math.round(fit_w*100)/100) || (h1 && Math.round(h*100)/100 != Math.round(fit_h*100)/100)) activeDocument.resizeImage(w1, h1); else alert("Already fitted!", "Warning") w = Number(activeDocument.width.value); h = Number(activeDocument.height.value); w = Math.round(w*100)/100; h = Math.round(h*100)/100; app.preferences.rulerUnits = old_units; if (w > fit_w || h > fit_h) { alert("Fit to:\t" + fit_w + " x " + fit_h + "\n\nResult:\t" + w + " x " + h, "Fit problem"); } } catch (e) { alert(e); } } d.center(); d.show();
What did not work for you I did not understand.
That's right, I would like to scale for small and large values, nothing more! Thank you
Find more inspiration, events, and resources on the new Adobe Community
Explore Now