Skip to content

Instantly share code, notes, and snippets.

@Juriy
Created November 4, 2016 07:12
Show Gist options
  • Save Juriy/e90d77ca580434a3cc1a34b483835b8d to your computer and use it in GitHub Desktop.
Save Juriy/e90d77ca580434a3cc1a34b483835b8d to your computer and use it in GitHub Desktop.
#include json2.js
// [+] Learn how to change text
// [+] How to save JPEG
// [+] How to read JSON
(function main() {
var lessons = loadJson('lessons.json');
for (var i = 0; i < lessons.length; i++) {
var lesson = lessons[i];
processLesson(lesson);
}
})();
function processLesson(lesson) {
var doc = app.activeDocument;
var titleGroup = doc.layerSets.getByName('title');
var titleLayer = titleGroup.layers[0];
titleLayer.textItem.contents = lesson.title;
var todoGroup = doc.layerSets.getByName('todo');
for (var i = 0; i < lesson.todo.length; i++) {
var layer = todoGroup.layers[i];
titleLayer.textItem.contents = lesson.todo[i];
}
titleGroup.visible = false;
todoGroup.visible = false;
saveGroup(titleGroup, lesson.id + '-title');
saveGroup(todoGroup, lesson.id + '-todo');
}
function saveGroup(group, name) {
group.visible = true;
saveJpeg(name);
group.visible = false;
}
function loadJson(relPath) {
var script = new File($.fileName);
var jsonFile = new File(script.path + '/' + relPath);
jsonFile.open('r');
var str = jsonFile.read();
jsonFile.close();
return JSON.parse(str);
}
function saveJpeg(name) {
var doc = app.activeDocument;
var file = new File(doc.path + '/gen/' + name + '.jpg');
var opts = new JPEGSaveOptions();
opts.quality = 10;
doc.saveAs(file, opts, true);
}
[
{
"id": "how-to-script-photoshop",
"title": "How to Script Photoshop",
"todo": [
"Learn how to load script",
"Write Hello World"
]
},
{
"id": "saving-files",
"title": "Saving Files",
"todo": [
"Save file from script",
"Learn how to work with paths"
]
},
{
"id": "loading-json",
"title": "Loading JSON",
"todo": [
"Read files from disk",
"Load external JSON library"
]
}
]
@themodmin
Copy link

Could the json file be loaded from a url instead of locally?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment