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

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

Community Beginner ,
Dec 10, 2019 Dec 10, 2019

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


output_1o8x4G.gifexpand image

TOPICS
Actions and scripting
1.1K
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

correct answers 2 Correct answers

Community Expert , Dec 10, 2019 Dec 10, 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);
Translate
Community Expert , Dec 12, 2019 Dec 12, 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. 

Translate
Community Expert ,
Dec 10, 2019 Dec 10, 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);
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
Community Beginner ,
Dec 12, 2019 Dec 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.");} 
}
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
Community Expert ,
Dec 12, 2019 Dec 12, 2019

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

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
Community Beginner ,
Dec 12, 2019 Dec 12, 2019

I believe line 12 is "catch(e) {alert("Requires a layer targeted.");}" and the error message is "Error 8: Syntax error. Line: 12 -> "

 

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
Community Expert ,
Dec 12, 2019 Dec 12, 2019

Nothing looks wrong with that line. Can you post the full script? Use the </> icon to post the code.

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
Community Beginner ,
Dec 12, 2019 Dec 12, 2019

I think this now works. I needed to delete some trailing characters that I couldn't initially see previously.  When I run this, I don't see a rectangle marquee or that marquee expand by 12 pixels. 

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.");
    }
}

 

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
Community Expert ,
Dec 12, 2019 Dec 12, 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. 

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
Community Beginner ,
Dec 16, 2019 Dec 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) {     
    }
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
People's Champ ,
Dec 16, 2019 Dec 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.
 
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
Community Beginner ,
Dec 16, 2019 Dec 16, 2019

Thanks r-bin! I have another Javascript that runs earlier in the action.  At the beginning of the first Javascript, I change the units to pixels. 

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
Community Expert ,
Dec 16, 2019 Dec 16, 2019
LATEST

r-bin is correct in that the last two line would produce an error, if not in the catch. docRef is undefined and you really don't need to store or load the selection.

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