A Script For Adding Borders and Captions To Photos

Automator can be used to script some third-party apps. As an example, here's how to write a script for Pixelmator Pro that adds a border and caption to a photo you select in the Photos app.

Here’s the script:

// remember the Photos app
var photosApp = Application.currentApplication();

// get Pixelmator Pro
var pixelmatorPro = Application("Pixelmator Pro");
pixelmatorPro.includeStandardAdditions = true;

var currentlyOpenedDocuments = pixelmatorPro.documents.length;

// send the keystroke to launch the external editor
var seApp = Application('System Events');
seApp.keystroke('\r', { using: 'command down' });

// wait for the document to open
while (true) {
	if (pixelmatorPro.documents.length > currentlyOpenedDocuments) break;
	delay(0.1);
}

// prompt for the caption
var inputDialog = pixelmatorPro.displayDialog('Caption?', {defaultAnswer: "Test Caption"});
var captionText = inputDialog.textReturned;

// get variables and calculate new size
var borderWidth = 20;
var doc = pixelmatorPro.documents[0];
var photo = doc.layers()[0]
var photoWidth = photo.width();
var photoHeight = photo.height();
var newWidth = photoWidth + borderWidth*2;
var newHeight = photoHeight + borderWidth*2;

// resize the document, adding room for a border
doc.resizeCanvas({width: newWidth, height: newHeight});

// add rectangle behind photo for border
var borderLayer = pixelmatorPro.RectangleShapeLayer({width: newWidth, height: newHeight});
doc.layers.push(borderLayer);
borderLayer.styles.fillColor = [0,0,0];

// create a text caption
var captionLayer = pixelmatorPro.TextLayer({textContent: captionText});
doc.layers.unshift(captionLayer);

// set the font, color an size of text
var captionContent = captionLayer.textContent;
captionContent.font = "Gill Sans";
captionContent.color = [65535,65535,65535];
captionContent.size = photoWidth/20;

// position the caption to the lower left
captionLayer.position = {"x":borderWidth + photoWidth * 0.01, "y":(newHeight - borderWidth - captionLayer.height())};

// add an outline and shadow to the caption
captionLayer.styles.strokeWidth = 2;
captionLayer.styles.strokeColor = [0,0,0];
captionLayer.styles.shadowDistance = 0;
captionLayer.styles.shadowAngle = 315;
captionLayer.styles.shadowOpacity = 100;

// save and close, waiting a little before closing
seApp.keystroke('s', { using: 'command down' });
delay(2);
seApp.keystroke('w', { using: 'command down' });

// go back to the Photos app
photosApp.activate();

Comments: 4 Responses to “A Script For Adding Borders and Captions To Photos”

    michael carlson
    4 years ago

    ok so I am not a genius - but just watching this video on how to add text to a photo want to make me cry - i mean come on - that is just way too complicated.

    4 years ago

    Michael: It IS complicated. Sometimes I cover complicated topics. I try to do a variety.

    Jezzah
    3 years ago

    Hi Gary - thanks for this but ... when I reproduce your scrpt I get an error that says Automator not allowed to send keystrokes. Any clue what is going wrong? Thanks !!

    3 years ago

    Jezzah: Go into System Preferences, Security & Privacy, Privacy, Accessibility and turn on (or add) Automator.

Comments Closed.