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

Need a script that resizes layer to a specific size using the largest dimension [SOLVED]

Engaged ,
Nov 28, 2023 Nov 28, 2023

Using Actions doesn't help and I'm not familiar with scripts.

So basically I have to resize 300 layers to 3000px using the largest size (width or height, whatever is bigger).

 

I saw scripts to resize images (not layers), so that won't do the job. I saw other solutions for very particular scenarios, but not what I'm looking for either, and since I'm familiar with scripting, I won't be able to adjust it to my needs.

 

Any chance that someone can help me with this?

I would really appreciate it, as this would save me so much time for this very tedious task and I need to deliver this asap

 

EDIT: I found the script that does this, but it needs a subtle change as described by the script's author @Stephen Marsh (thank you for your contribution, Stephen).

Use the following script (the original script used "Math.max" and I changed it to "Math.min" to fit the largest dimension to the canvas):

 

 

 

 

// https://forums.adobe.com/thread/1968642
// https://forums.adobe.com/message/8022190#8022190
#target photoshop    
var oldPref = app.preferences.rulerUnits    
app.preferences.rulerUnits = Units.PIXELS;    
var doc = activeDocument;   
var iLayer = doc.activeLayer;    
doc.activeLayer = iLayer;    
var scale = Math.min(doc.width/(iLayer.bounds[2]-iLayer.bounds[0]),doc.height/(iLayer.bounds[3]-iLayer.bounds[1])); // Optionally change Math.max to Math.min to fit canvas short side
iLayer.resize (scale*100,scale*100);    
iLayer.translate(doc.width/2-(iLayer.bounds[0]+iLayer.bounds[2])/2,doc.height/2-(iLayer.bounds[1]+iLayer.bounds[3])/2);
app.preferences.rulerUnits = oldPref;

 

 

To make it clear what the Math.max and Math.min do:

SCR-20231128-ncwn.png

SCR-20231128-ndbm.png

SCR-20231128-ndgb.png

   

 

TOPICS
Actions and scripting , macOS
244
Translate
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 ,
Nov 28, 2023 Nov 28, 2023

Just to be clear, I didn't write the script, I just posted the code with the reference to the original forum posts where it was found. Due to multiple forum software changes, those links don't work anymore.

 

https://community.adobe.com/t5/photoshop-ecosystem-discussions/scale-layer-to-current-canvas-size-ph...

 

Another similar script:

 

https://community.adobe.com/t5/photoshop-ecosystem-discussions/fit-layer-to-canvas-keeping-aspect-ra...

Translate
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
Engaged ,
Nov 29, 2023 Nov 29, 2023
LATEST

My bad. I thought you created it.

Anyway, that script worked like a charm, so thank you for sharing!

Translate
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