i have totally re-editied My communication and spelling skills are not very good so sorry if you find it hard.
to make it easier to read i am going to shorten and sweeten my post
i have a psd file with groups and layers set as needed
i then created a script to chnage the text accordingly to json inout from a json file code and file example below.
JavaScriptx43431#include json2.js
2//////// ^^^^ loading JSON2 ///////////
3var obj = loadJson('text.json');
4//////// ^^^^ Variable for JSON Text Data ///////////
5var titleGroup = app.activeDocument.layerSets.getByName('text');
6var titleLayer = titleGroup.layers[0];
7var ordinatesLayer = titleGroup.layers[1];
8titleLayer.textItem.contents = obj.title;
9ordinatesLayer.textItem.contents = obj.ord;
10////// ^^^ Locate And change Text using JSON Data ///////////
11var theLayer = app.activeDocument.layerSets.getByName('image');
12var changeLayer = theLayer.layers[0]
13//////// ^^^ var set need to create future functions to grab image location from the json data and replace image ///////////
14
15
16
17saveJpeg(obj.title + 'Finished');
18
19//////// ^^^ Script Action Using Functions Below to Save Altered results ///////////
20
21//////// Functions BELOW!!! /////////
22function loadJson(relPath) {
23var script = new File($.fileName);
24var jsonFile = new File(script.path + '/' + relPath);
25jsonFile.open('r');
26var str = jsonFile.read();
27jsonFile.close();
28return JSON.parse(str);
29}
30
31////// ^^^ load and parse data to set vairiables for use //////
32
33function saveJpeg(name) {
34var doc = app.activeDocument;
35var file = new File(doc.path + '/' + name + '.jpg');
36var opts = new JPEGSaveOptions();
37opts.quality = 10;
38doc.saveAs(file, opts, true);
39}
40
41////// ^^^ save Finished Results /////
42//alert('Your Script has done!!!');
43
JSON Data example.
{“title” : “LONDON”, “ord” : “51.5074° N, 0.1278° W”}
- i then found a piece of code and altered it to my needs (well nearly) the code snippet allows a dialog to open and me to selct the file needed
the problem is i need it to select the image using the title name from the json data to the grab say example LONDON.PNG and then replace it all without a dialog and selection (silent and auto)
below is my uodated code and screenshot of my projects root folder
JavaScript
1
61
61
1
#include json2.js
2
//////// ^^^^ loading JSON2 ///////////
3
var obj = loadJson('text.json');
4
//////// ^^^^ Variable for JSON Text Data ///////////
5
var titleGroup = app.activeDocument.layerSets.getByName('text');
6
var titleLayer = titleGroup.layers[0];
7
var ordinatesLayer = titleGroup.layers[1];
8
titleLayer.textItem.contents = obj.title;
9
ordinatesLayer.textItem.contents = obj.ord;
10
////// ^^^ Locate And change Text using JSON Data ///////////
11
var theLayer = app.activeDocument.layerSets.getByName('image');
12
var changeLayer = theLayer.layers[0]
13
var replacementFile = new File(obj.title + "png");
14
//////// ^^^ var set need to create future functions to grab image location from the json data and replace image ///////////
15
16
17
18
19
changeLayer = replaceContents(replacementFile);
20
saveJpeg(obj.title + 'Finished');
21
22
//////// ^^^ Script Action Using Functions Below to Save Altered results ///////////
23
24
//////// Functions BELOW!!! /////////
25
function loadJson(relPath) {
26
var script = new File($.fileName);
27
var jsonFile = new File(script.path + '/' + relPath);
28
jsonFile.open('r');
29
var str = jsonFile.read();
30
jsonFile.close();
31
return JSON.parse(str);
32
}
33
34
////// ^^^ load and parse data to set vairiables for use //////
35
36
function saveJpeg(name) {
37
var doc = app.activeDocument;
38
var file = new File(doc.path + '/' + name + '.jpg');
39
var opts = new JPEGSaveOptions();
40
opts.quality = 10;
41
doc.saveAs(file, opts, true);
42
}
43
44
////// ^^^ save Finished Results /////
45
//alert('Your Script has done!!!');
46
47
48
function replaceContents (newFile) {
49
50
var idplacedLayerReplaceContents = stringIDToTypeID( "placedLayerReplaceContents" );
51
var desc3 = new ActionDescriptor();
52
var idnull = charIDToTypeID( "null" );
53
desc3.putPath( idnull, new File( newFile ) );
54
var idPgNm = charIDToTypeID( "PgNm" );
55
desc3.putInteger( idPgNm, 1 );
56
executeAction( idplacedLayerReplaceContents, desc3, DialogModes.NO );
57
return app.activeDocument.activeLayer
58
};
59
60
////// ^^^ replace contents //////
61
Advertisement
Answer
JavaScript
1
63
63
1
#include json2.js
2
//////// ^^^^ loading JSON2 ///////////
3
var obj = loadJson('text.json');
4
//////// ^^^^ Variable for JSON Text Data ///////////
5
var titleGroup = app.activeDocument.layerSets.getByName('text');
6
var titleLayer = titleGroup.layers[0];
7
var ordinatesLayer = titleGroup.layers[1];
8
titleLayer.textItem.contents = obj.title;
9
ordinatesLayer.textItem.contents = obj.ord;
10
////// ^^^ Locate And change Text using JSON Data ///////////
11
var theLayer = app.activeDocument.layerSets.getByName('image');
12
var changeLayer = theLayer.layers[0]
13
/////// added and fixed below /////
14
var string = 'C:/Users/apps/Documents/script/';
15
var replacementFile = new File(string + obj.title + '.png');
16
//////// ^^^ var set need to create future functions to grab image location from the json data and replace image ///////////
17
18
19
20
21
changeLayer = replaceContents(replacementFile);
22
saveJpeg(obj.title + 'Finished');
23
24
//////// ^^^ Script Action Using Functions Below to Save Altered results ///////////
25
26
//////// Functions BELOW!!! /////////
27
function loadJson(relPath) {
28
var script = new File($.fileName);
29
var jsonFile = new File(script.path + '/' + relPath);
30
jsonFile.open('r');
31
var str = jsonFile.read();
32
jsonFile.close();
33
return JSON.parse(str);
34
}
35
36
////// ^^^ load and parse data to set vairiables for use //////
37
38
function saveJpeg(name) {
39
var doc = app.activeDocument;
40
var file = new File(doc.path + '/' + name + '.jpg');
41
var opts = new JPEGSaveOptions();
42
opts.quality = 10;
43
doc.saveAs(file, opts, true);
44
}
45
46
////// ^^^ save Finished Results /////
47
//alert('Your Script has done!!!');
48
49
50
function replaceContents (newFile) {
51
52
var idplacedLayerReplaceContents = stringIDToTypeID( "placedLayerReplaceContents" );
53
var desc3 = new ActionDescriptor();
54
var idnull = charIDToTypeID( "null" );
55
desc3.putPath( idnull, new File( newFile ) );
56
var idPgNm = charIDToTypeID( "PgNm" );
57
desc3.putInteger( idPgNm, 1 );
58
executeAction( idplacedLayerReplaceContents, desc3, DialogModes.NO );
59
return app.activeDocument.activeLayer
60
};
61
62
////// ^^^ replace contents //////
63