Last active
August 22, 2023 10:11
-
-
Save chengluyu/9d726b11c6d94d442376bd7d65b7310e to your computer and use it in GitHub Desktop.
Photoshop Batch Process - Change Text and Export
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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