Thursday, July 18, 2019

#1 2019-05-15 09:51:31 am

EHCB
Member
From:: UK
Registered: 2017-11-23
Posts: 7
Website

Resizing a layer to fit the bound of a document

Hi all,

EDIT: This is a question about Photoshop CC 2109

After spending a full 6 hours trying different formulas, different approaches and different google phrases I'm ready to a admit I need help.


I need a script that can change the scale of an embedded image to match the width of the document it's in and I just cannot figure out what I need to do to convert the pixel width of the document into a percentage to set my layer. Nothing I've tried has even got it close.

I'd have thought this was a really common operation but I haven't been able to find a single comparable case

Here's as far as I've got with my code, the part with question marks is where I've been desperately trying different formulas all day.


Applescript:

set type units of settings to pixel units
--set variable for layer width
       set LW to (item 3 of bounds of layer "LOGOvec" of current document)
--set variable to document width
       set DocW to (width of current document)
       
--set variable to final percentage to apply to layer
       set UseScale to ???????
       


       scale (layer "LOGOvec") of current document ¬
           horizontal scale UseScale ¬
           vertical scale UseScale ¬
           anchor position (middle center)
       

Last edited by EHCB (2019-05-16 03:54:52 am)


Filed under: , photoshop, Math, resizing

Offline

 

#2 2019-05-15 10:23:26 am

t.spoon
Member
From:: BFE, Massachusetts
Registered: 2013-01-13
Posts: 411

Re: Resizing a layer to fit the bound of a document

What application are you talking about?


Hackintosh built February, 2012 |  Mac OS Sierra
GIGABYTE GA-Z68X-UD3H-B3 | Core i5 2500k | 16 GB DDR3 | GIGABYTE Geforce 1050 TI 4GB
250 GB Samsung 850 EVO | 4 TB RAID
Dell Ultrasharp U3011 | Dell Ultrasharp 2007FPb

Offline

 

#3 2019-05-16 03:54:00 am

EHCB
Member
From:: UK
Registered: 2017-11-23
Posts: 7
Website

Re: Resizing a layer to fit the bound of a document

t.spoon wrote:

What application are you talking about?



Whoops, it's Photoshop!
I cant believe I forgot to include that!

Offline

 

#4 2019-05-16 02:22:46 pm

t.spoon
Member
From:: BFE, Massachusetts
Registered: 2013-01-13
Posts: 411

Re: Resizing a layer to fit the bound of a document

Had to write this really fast, barely had any time to test it, but seems to be working.

I'm assuming you always want it:
- scaled proportionally
- centered on the canvas

Applescript:


tell application "Adobe Photoshop CC 2019"
   set docRef to the current document
   tell docRef
       -- gather data
       set embeddedLayerName to "Test Logo"
       set embedBounds to the bounds of layer embeddedLayerName
       tell embedBounds to set {leftEdge, topEdge, embedWidth, embedHeight} to {item 1, item 2, (item 3) - (item 1), (item 4) - (item 2)}
       set docWidth to the width
       set docHeight to the height
       -- center embedded image on canvas
       set {embedXcenter, embedYcenter} to {(embedWidth / 2 + leftEdge), (embedHeight / 2 + topEdge)}
       set {docXcenter, docYcenter} to {docWidth / 2, docHeight / 2}
       set {translateX, translateY} to {docXcenter - embedXcenter, docYcenter - embedYcenter}
       translate layer embeddedLayerName delta x translateX delta y translateY
       -- test aspect ratio to decide to scale by width or height
       if embedWidth / embedHeight > docWidth / docHeight then
           set scaleFactor to docWidth / embedWidth * 100
       else
           set scaleFactor to docHeight / embedHeight * 100
       end if
       --scale
       scale layer embeddedLayerName horizontal scale scaleFactor vertical scale scaleFactor
   end tell
end tell


Hackintosh built February, 2012 |  Mac OS Sierra
GIGABYTE GA-Z68X-UD3H-B3 | Core i5 2500k | 16 GB DDR3 | GIGABYTE Geforce 1050 TI 4GB
250 GB Samsung 850 EVO | 4 TB RAID
Dell Ultrasharp U3011 | Dell Ultrasharp 2007FPb

Offline

 

#5 2019-05-17 04:54:04 am

EHCB
Member
From:: UK
Registered: 2017-11-23
Posts: 7
Website

Re: Resizing a layer to fit the bound of a document

t.spoon wrote:

Had to write this really fast, barely had any time to test it, but seems to be working.



That works absolutely beautifully, thank you so much!

The frustrating thing is I think I did have the right scaling formula at one point, but I'd also misunderstood how getting the bounds of at object worked

Offline

 

Board footer

Powered by FluxBB

RSS (new topics) RSS (active topics)