Skip to main content
Participating Frequently
December 11, 2019
Answered

Javascript that will expand the x,y coordinates of a rasterized text layer by 12 pixels?

  • December 11, 2019
  • 2 replies
  • 1245 views

Hey Everyone!

 

Working on switching up an old Applescript to an Action (that involves a little bit of Javascript) and I'm running into a problem. Wondering if somone can help.  For certian photos, I need to add the filename to the bottom right hand corner of the image (black font with white background).  Right now, the script will paste the filename, without the extension, as a text layer.  After changing the font style, size, color, etc I position the font in the bottom right hand corner and rasterize the font. 

What I can't figure out is how to get a rectangular marquee around the font (similar to what would happen if I selected the "Free Transform" option). If I can get a bounding box around the text, I can then run an action to expand the selection by 12 pixels and then fill in with white.  Any thoughts?

Based on the Applescript code (provided below), I'll need to find the x,y and the w, h:

-Gets dimensions for outline of text layer

--Variable is formatted as: {left X dimension, top Y dimension, right X dimension, bottom Y dimension}

set shapeRef to bounds of artLayerRef

 

--Converts bounds to four corner coordinates to create selection

set boxX1 to item 1 of shapeRef

set boxY1 to item 2 of shapeRef

set boxX2 to item 3 of shapeRef

set boxY2 to item 4 of shapeRef

 

set shapeRef to {{boxX1, boxY1}, {boxX2, boxY1}, {boxX2, boxY2}, {boxX1, boxY2}}

 

--Selects area around text
select current document region shapeRef


This topic has been closed for replies.
Correct answer Chuck Uebele

Well, you put everything in a function, so are you calling that function? You don't need the "layer" as an argument for the function, as all the defining of the document and active layer are done inside the function. 

2 replies

Chuck Uebele
Community Expert
Chuck UebeleCommunity ExpertCorrect answer
Community Expert
December 13, 2019

Well, you put everything in a function, so are you calling that function? You don't need the "layer" as an argument for the function, as all the defining of the document and active layer are done inside the function. 

Participating Frequently
December 16, 2019

Thanks Chuck! This was very helpful.  This is my final: 

try {
        var doc = activeDocument;
        var expn = 12;
        var bnd = doc.activeLayer.bounds
        var selA = [
            [bnd[0]-expn,bnd[1]-expn],
            [bnd[2]+expn,bnd[1]-expn],
            [bnd[2]+expn,bnd[3]+expn],
            [bnd[0]-expn,bnd[3]+expn]
        ];
        doc.selection.select(selA);
	docRef.selection.store(selA);
	docRef.selection.load(selA);
    } catch(e) {     
    }
Legend
December 16, 2019
Comment:
The last two lines contain two errors each.
They are not executed, but they are not needed.
If rulerUnits are not pixels, the script will not work correctly.
 
Chuck Uebele
Community Expert
Community Expert
December 11, 2019

This will take a layer, get it's bounds, and make a selection larger by the variable expn, which is set to 12, right now.

var doc = activeDocument;
var expn = 12;
var bnd = doc.activeLayer.bounds
var selA = [[bnd[0]-expn,bnd[1]-expn],[bnd[2]+expn,bnd[1]-expn],[bnd[2]+expn,bnd[3]+expn],[bnd[0]-expn,bnd[3]+expn]];
doc.selection.select(selA);
Participating Frequently
December 12, 2019

Thanks Chuck!  This is super helpful.  I went to write this out and I keep getting an error on line 12. Any thoughts on what I need to add / edit?  

 

function get_layer_bounds(layer) 
{ 
try 
{ var doc = activeDocument; var expn = 12; var bnd = doc.activeLayer.bounds var selA = [[bnd[0]-expn,bnd[1]-expn],[bnd[2]+expn,bnd[1]-expn],[bnd[2]+expn,bnd[3]+expn],[bnd[0]-expn,bnd[3]+expn]]; doc.selection.select(selA); } 
catch(e) {alert("Requires a layer targeted.");} 
}
Chuck Uebele
Community Expert
Community Expert
December 12, 2019

What is the error, and what is exactly on line 12?