Skip to main content
Participant
February 20, 2014
Question

Move layer to an absolute position, not translate

  • February 20, 2014
  • 1 reply
  • 3445 views

I have images that have white scalebars of varying lengths on a black background of which I would like to copy and create new layers with my own personalized scalebars (e.g. height, stroke, color, etc.) 

Using actions I have been able to select the region, select color region (highlights) and create a new layer from copy.  This gets me my new scale bar layer of which I can then manipulate in an action list. 

The issue I am having is that I want to move the scalebar to a specific region of the image, which in actions I can do but only for the scale bar which matches the length of the original template image.  Once I attempt to run my action on another image with a different length it translates using the cooridinates of the original template image, resulting in a scale bar which is off of the mark.

I need to figure out how to write a script that would take the selected layer (in this case a line), set the reference point for the lower left corner, and then set that layer at a specific x y coordinate such that no matter how long or short my scalebar, it would start the layer at the same place in the image.

Any help would be greatly appreciated.

Thanks,

Matt

This topic has been closed for replies.

1 reply

Chuck Uebele
Community Expert
Community Expert
February 20, 2014

With scripting, you still use the translate feature, but you just calculate where you want it placed.  For example, you first would place a layer in a file, and if that layer is the active layer this would be the code along the lines that you might want:

app.preferences.rulerUnits = Units.PIXELS;//use pixel values

var docRef = activeDocument;

var scaleBar = docRef.activeLayer;

var whereIsScaleBar = scaleBar.bounds; //gets the position of the layer as an array in this order:left side, top, right side, bottom

var whereIwantScaleBarX = 10; //the X value of where you want to move the layer

var whereIwantScaleBarY = 10; //the Y value of where you want to move the layer

scaleBar.translate(whereIwantScaleBarX - whereIsScaleBar[0], whereIwantScaleBarY - whereIsScaleBar[3]);//this would be the bottom left 0 being the left and 3 being the bottom.

olszta77Author
Participant
February 20, 2014

OK, actually your comment just jogged something loose in my brain in regards to the calculate response. The scale bars all start on the right hand side at the same spot and span leftwards to different distances.  I can acutally use photoshop actions to take advantage of this and put everything on the right hand side of my image.  I was being stubborn and thinking I wanted it on the left hand side.

Thank you for your script though, I appreciate the quick response.

Matt

JJMack
Community Expert
Community Expert
February 21, 2014

In actions as scripts its important to understand the importance of the current setting of Photoshop units setting.  When set to Pixels  your dealing with absolute pixel numbers. Other settings are all relative to something, When set to percent units are relative to document size.  You can not distinguish a landscape from a portrait by comparing width to height for  they willl both be 100%.  Other units are relative to the documents resolution.   1" in a 300dpi resolution document is 300 pixels in a 72 DPI resolution document it will be 100 pixels.   When you copy and past or place an image into some document with a different resolution  Photoshop will resample the image being pasted  or placed in to match the resolution of the document receiving the image to preserve the size of the images going into the image,   So if you place or past a 300 DPI image into a 72 dpi document the 300 DPI image will be resampled to 72 dpi and you will loose a lot of detail im image quality. Actions and scripts often run into problems sizing things when all things are not taken into consideration.  Size is very difficult to deal with in actions for actions can not retrieve information about document and work with that information. Many actions I have seem have dependencies the creator didn't realize. Actions may work for them only because of the work-flow always produced files that met the unknown dependency.  Other find things go wrong like text is the right size in some of images the action produces and the wrong size in others.

Update: 2019

Place will degrade images being placed into the current Documents, if the current document resolution is not the same at the Image file Print DPI resolution being placed into the document.  A high resolution 300DPI Images will be severely degraded if  placed into a document that current resolution setting is a low resolution setting like 72 DPI.    IMO  You should avoid doing that.       Photoshop does not degrade images that you use  Photoshop's Edit Copy to clipboard and Pasted from Clipboard,  or when you drag and drop layers from one document to an other or you duplicate layers from one document  to an other document.     It is only when you use Photoshop Place feature that your image pixels may be resampled up or down in number because of resolution settings.  You will have more or fewer lower quality pixels rendered for your smart Objects pixels.    Additionally  these pixels may be resampled a second time if the Smart object layer associated transform to fit in canvas because you Photoshop preference is set to resize during Place.  That preference has to do with scaling Smart Object Layers to fit within the canvas not  about resizing image based on resolution settings.   There is not option for that resize. If the resolutions are not the same you image will be resized.  When ever you use place you should make sure the Image File Being Placed in Print Resolution is the same  as the current document resolution setting to avoid degrading your image. 

Copy/Paste, Drag/drop and Duplicate Layers do not resample layer pixels your image will not be degraded by Photoshop.

JJMack