Skip to main content
Alessandro Vecchi
Inspiring
May 1, 2019
Answered

Script for border in %

  • May 1, 2019
  • 1 reply
  • 1947 views

Hello,

I want to create a script to add a white border with my website as signature to an entire batch of images with different sizes.

I've researched here and I found a script but when I've tried to customize to my needs returns me error on the 7 line.

As i never used a script nor know how to code, could anyone assist me with this issue please?

I also would like the text to be centralized in "height" and "width" on the bottom white bar.

I've already sweated a lot to customize this simple text to what I would like to achieve.

Thank you very much!

if(documents.length) app.activeDocument.suspendHistory('Add Border', 'main()'); 

function main(){

var startRulerUnits = preferences.rulerUnits;

app.preferences.rulerUnits = Units.PIXELS;

var doc = activeDocument;

var fivePercent = (doc.height/100) * 20;

var OffsetX = twentyPercent;

var OffsetY = twentyPercent/4;

var White = new SolidColor();

White.rgb.hexValue = 'ffffff';

var White = new SolidColor();

White.rgb.hexValue = 'ffffff';

app.backgroundColor=White;

doc.flatten();

app.activeDocument.resizeCanvas((doc.width + (twentyPercent*2)), (doc.height + (twentyPercent*2)), AnchorPosition.MIDDLECENTER);

var newTextLayer = activeDocument.artLayers.add(); 

newTextLayer.kind = LayerKind.TEXT; 

newTextLayer.textItem.kind = TextType.POINTTEXT

newTextLayer.textItem.color = White; 

newTextLayer.textItem.font = "Source Sans Pro Regular";

newTextLayer.textItem.size = 18; 

newTextLayer.textItem.contents = "www.alessandro-vecchi.com"; 

var LB = doc.activeLayer.bounds; 

var LHeight = Math.abs(LB[3].value) - Math.abs(LB[1].value);

var percentage = ((twentyPercent/LHeight)*50);

doc.activeLayer.resize(percentage,percentage,AnchorPosition.MIDDLECENTER);

LB = doc.activeLayer.bounds; 

var X = (activeDocument.width - twentyPercent) - LB[2].value;

var Y = (activeDocument.height - OffsetY) - LB[3];

activeDocument.activeLayer.translate(X,Y);

doc.flatten();

preferences.rulerUnits = startRulerUnits;

}

This topic has been closed for replies.
Correct answer SuperMerlin

if you want to chage to 20 percent change:

var fivePercent = (Math.min(doc.height,doc.width)/100) * 5

to

var fivePercent = (Math.min(doc.height,doc.width)/100) * 20

1 reply

SuperMerlin
Inspiring
May 1, 2019

I think the original code was here:-

Creating a uniform border as a percentage of image size. How?

Does this do what you want?

#target photoshop;

if(documents.length) app.activeDocument.suspendHistory('Add Border', 'main()');

function main(){

var startRulerUnits = preferences.rulerUnits;

app.preferences.rulerUnits = Units.PIXELS;

var doc = activeDocument;

var fivePercent = (Math.min(doc.height,doc.width)/100) * 5;

var OffsetX = fivePercent;

var OffsetY = fivePercent/4;

var Black = new SolidColor();

Black.rgb.hexValue = '000000';

var White = new SolidColor();

White.rgb.hexValue = 'ffffff';

app.backgroundColor=White;

doc.flatten();

doc.resizeCanvas((doc.width + (fivePercent*2)), (doc.height + (fivePercent*2)), AnchorPosition.MIDDLECENTER);

var newTextLayer = activeDocument.artLayers.add();

newTextLayer.kind = LayerKind.TEXT;

newTextLayer.textItem.kind = TextType.POINTTEXT

newTextLayer.textItem.color = Black;

newTextLayer.textItem.font = "Georgia";

newTextLayer.textItem.size = 14;

newTextLayer.textItem.contents = "www.alessandro-vecchi.com";

var LB = doc.activeLayer.bounds;

var LHeight = Math.abs(LB[3].value) - Math.abs(LB[1].value);

var percentage = ((fivePercent/LHeight)*50);

doc.activeLayer.resize(percentage,percentage,AnchorPosition.MIDDLECENTER);

LB = doc.activeLayer.bounds;

var X = (activeDocument.width - (LB[2] - LB[0]))  / 2;

var Y = (activeDocument.height - OffsetY) - LB[3];

doc.activeLayer.translate(X -LB[0],Y);

doc.flatten();

preferences.rulerUnits = startRulerUnits;

}

Alessandro Vecchi
Inspiring
May 1, 2019

Yes!

That’s the original code.

What I’ve tried in my effort of customizing is to change the percentage from 5 to 20 and the border color from black to white, besides the text at the bottom.

In the code I’ve posted there’s some error because photoshop was unable to run the script.

Anyway thank you for your time replying to my post!

SuperMerlin
SuperMerlinCorrect answer
Inspiring
May 1, 2019

if you want to chage to 20 percent change:

var fivePercent = (Math.min(doc.height,doc.width)/100) * 5

to

var fivePercent = (Math.min(doc.height,doc.width)/100) * 20