Skip to main content
mrforged
Participant
February 27, 2018
Answered

Positioning Layer Relative to Guides

  • February 27, 2018
  • 4 replies
  • 1793 views

I have two files I will call file a and file b.  File a is a template, it is 1335x5400 pixels, single layer, and has a single horizontal guide at exactly 1300 pixels from the top.   File b is variable size, single layer, and can have a single horizontal guide anywhere.  I need to take File b, resize it, and copy it into File a, then move it to a specific location.  The location differs depending on whether or not there is a guide in File b.

What I am currently doing is opening File a, then opening File b, resizing the image, duplicating the active layer to File a, switching to File a, repositioning the layer to the desired location, exporting the resulting image,  and then closing both files.  This works great.  However, I have no clue as to how I could possibly duplicate the guides from File b to File a (I am not really sure I would need to do that either) and how to align the layer using this guide.

I would much appreciate it if someone could post example code.  Thank you very much in advance.

This topic has been closed for replies.
Correct answer r-bin

Is this suitable?

app.preferences.rulerUnits = Units.PIXELS;

var doc_a; 

var doc_b;

try { doc_a = activeDocument; } catch(e) {} // assume that doc_a is an active document or define doc_a and doc_b yourself

for (var i = 0; i < documents.length; i++) if (documents.id != doc_a.id) { doc_b = documents; break; }

if (doc_a == undefined) alert("Active document is missing")

else if (doc_b == undefined) alert("The second document is missing")

else

    {

    var offset_a;

    var offset_b;

    activeDocument = doc_b;

    try {

    if (doc_b.guides[0].direction == Direction.HORIZONTAL)

        offset_a = doc_b.guides[0].coordinate;

    }

    catch (e) {}   

    activeDocument = doc_a;

    try {

    if (doc_a.guides[0].direction == Direction.HORIZONTAL)

        offset_b = doc_a.activeLayer.bounds[1] - doc_a.guides[0].coordinate;

    }

    catch (e) {}   

    // duplicate layer from doc_b to doc_a

    activeDocument = doc_b;

    doc_b.activeLayer.duplicate(doc_a.activeLayer, ElementPlacement.PLACEBEFORE);

    // translate layer to position

    activeDocument = doc_a;

    if (offset_a != undefined && offset_b != undefined)

        doc_a.activeLayer.translate(0, doc_a.activeLayer.bounds[1] - offset_b - offset_a)

    else

        alert("Position is undefined!");

    }

P.S. was edited

4 replies

mrforged
mrforgedAuthor
Participant
February 28, 2018

Thank you very much to everyone who helped, particularly r_bin for providing the code that answered the question.  Invaluable.  Thank you all so much.

JJMack
Community Expert
Community Expert
February 28, 2018

mrforged  wrote

I have two files I will call file a and file b.  File a is a template, it is 1335x5400 pixels, single layer, and has a single horizontal guide at exactly 1300 pixels from the top.  

File b is variable size, single layer, and can have a single horizontal guide anywhere. 

I need to take File b, resize it, and copy it into File a,

then move it to a specific location.  The location differs depending on whether or not there is a guide in File b.

We know a lot about file A the number of pixels its canvas width and height  is and a horizontal pixel row 1300 pixels down from its 5400px height.   We do not know the  document resolution setting.

We know little about file B from your diagram we can see it's size is much smaller then file A and from your description  it needs to be resized for file A size and there may be a horizontal row marker in some of the file b files  That should be align with row 1300 in file A else align something other perhaps the image bottom edge.

We do not know any real resize information sizes or aspect rartio.  We know that curren document B need to be resized but have no informataio of what that resize size should be that is required for file A size..

JJMack
JJMack
Community Expert
Community Expert
February 28, 2018

The problem I have with what you write is I have no idea of what you mean by resize document B or what effect it will have on the document horizontally guide.  If you are increasing or decreasing the number of pixels  so it will be the same number or pixels some object in document A has.  And if the single layer in document B is a pixel raster layer why  the resample image would match any image in Document A.   I would think the Image in document B is not being stitched to an image in document A.   Its the resize and the importance of document B  guide if document B is being resized.  Do not know what the resize criteria is.  The diagram does not even show anything about the resize mentioned in the process description. Or even show if something in document a is being matched.  I have no idea as to what this resize and positioning is all about

JJMack
mrforged
mrforgedAuthor
Participant
February 28, 2018

If you think about it, resizing the document in any direction, proportional or not, is irrelevant to the end result.  All that is needed here is to place the two images so that pixel y in one of the images is located at exactly 1300 pixels from the top of the other image.  Really, it doesn't matter if you resize or not, in the end, the result is the same relatively speaking.

Is there a way to read the position of the guides?  If so, can anyone provide an example? I am pretty sure that if I know how to read the position of the guides, I can figure out the rest quickly. 

r-binCorrect answer
Legend
February 28, 2018

Is this suitable?

app.preferences.rulerUnits = Units.PIXELS;

var doc_a; 

var doc_b;

try { doc_a = activeDocument; } catch(e) {} // assume that doc_a is an active document or define doc_a and doc_b yourself

for (var i = 0; i < documents.length; i++) if (documents.id != doc_a.id) { doc_b = documents; break; }

if (doc_a == undefined) alert("Active document is missing")

else if (doc_b == undefined) alert("The second document is missing")

else

    {

    var offset_a;

    var offset_b;

    activeDocument = doc_b;

    try {

    if (doc_b.guides[0].direction == Direction.HORIZONTAL)

        offset_a = doc_b.guides[0].coordinate;

    }

    catch (e) {}   

    activeDocument = doc_a;

    try {

    if (doc_a.guides[0].direction == Direction.HORIZONTAL)

        offset_b = doc_a.activeLayer.bounds[1] - doc_a.guides[0].coordinate;

    }

    catch (e) {}   

    // duplicate layer from doc_b to doc_a

    activeDocument = doc_b;

    doc_b.activeLayer.duplicate(doc_a.activeLayer, ElementPlacement.PLACEBEFORE);

    // translate layer to position

    activeDocument = doc_a;

    if (offset_a != undefined && offset_b != undefined)

        doc_a.activeLayer.translate(0, doc_a.activeLayer.bounds[1] - offset_b - offset_a)

    else

        alert("Position is undefined!");

    }

P.S. was edited

JJMack
Community Expert
Community Expert
February 28, 2018

Guide lines are a Photoshop UI thing not actally part of your image. Some file formats can save all sorts of Photoshop information like layers selections and guides example PSD and PSD files.  You will not be able to save things like Photoshop guideline information in file formats like jpeg, png and gif as far as I know.  If you need to save guide lines  save your document into a Photoshop file format like PSD or PSB.  Tiff also support layers I do not know about saving guide lines..You can set guides anywhere you want to in a Photoshop script.

Why are you using guideline to begin with in your templates and other files? Why are they needed?

You seen to know the template is 1335x5400 pixels, single layer, and  you want to align something 1300 pixels from  the top  top. Why do you need a guide there?

JJMack
Tom Winkelmann
Inspiring
February 28, 2018

JPG & TIF support guides

JJMack
Community Expert
Community Expert
February 28, 2018

I wrote I did not know I never save guideline in jpeg. I wrote tiff support Photoshop things like layers.  What I asked is why do they need guides if they are scripting the process know the image size pixels wide and high  and know they want to align a new image layer some size 1300 px down from the top? They can set guides anywhere they want to in a script.  I wanted to know what for?

JJMack