Skip to content

Instantly share code, notes, and snippets.

@chengluyu
Last active August 22, 2023 10:11
Show Gist options
  • Save chengluyu/9d726b11c6d94d442376bd7d65b7310e to your computer and use it in GitHub Desktop.
Save chengluyu/9d726b11c6d94d442376bd7d65b7310e to your computer and use it in GitHub Desktop.
Photoshop Batch Process - Change Text and Export
// This script works well with Photoshop CC 2017
// Adopted from stackoverflow.com/questions/14571008/photoshop-scripting-changing-text-of-a-text-layer
Array.prototype.forEach = function (fn) {
for (var i = 0; i < this.length; i++) {
fn(this[i], i);
}
};
var doc;
try {
doc = app.activeDocument;
} catch(e) {
}
var txtLayer = doc.artLayers.getByName('content')
var data = [ 'text goes here', 'another text' ]
if (txtLayer) {
data.forEach(function (text, num) {
txtLayer.textItem.contents = '  ' + text;
var ext = '.png',
dir = decodeURI(doc.path) + '/output',
fileFullName = dir + '/' + num + ext,
i = 0;
if (!Folder(dir).exists) {
Folder(dir).create();
}
while (File(fileFullName).exists) {
fileFullName = dir + '/' + num + '-' + (++i) + ext;
}
var file = new File(fileFullName),
opts = new ExportOptionsSaveForWeb();
opts.format = SaveDocumentType.PNG;
opts.PNG8 = true;
// opts.format = SaveDocumentType.JPEG;
// opts.optimized = true;
doc.exportDocument(file, ExportType.SAVEFORWEB, opts);
})
}
// if (doc) {
// doc.close(SaveOptions.DONOTSAVECHANGES);
// }
doc = null;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment