Ausgabe
Ich habe es komplett überarbeitet. Meine Kommunikations- und Rechtschreibfähigkeiten sind nicht sehr gut. Tut mir leid, wenn Sie es schwer finden.
Um das Lesen zu erleichtern, werde ich meinen Beitrag kürzen und versüßen
-
Ich habe eine PSD-Datei mit Gruppen und Ebenen, die nach Bedarf eingestellt sind
-
Ich habe dann ein Skript erstellt, um den Text entsprechend aus einem JSON-Dateicode und einem Dateibeispiel unten in json inout zu ändern.
#include json2.js //////// ^^^^ loading JSON2 /////////// var obj = loadJson('text.json'); //////// ^^^^ Variable for JSON Text Data /////////// var titleGroup = app.activeDocument.layerSets.getByName('text'); var titleLayer = titleGroup.layers[0]; var ordinatesLayer = titleGroup.layers[1]; titleLayer.textItem.contents = obj.title; ordinatesLayer.textItem.contents = obj.ord; ////// ^^^ Locate And change Text using JSON Data /////////// var theLayer = app.activeDocument.layerSets.getByName('image'); var changeLayer = theLayer.layers[0] //////// ^^^ var set need to create future functions to grab image location from the json data and replace image /////////// saveJpeg(obj.title + 'Finished'); //////// ^^^ Script Action Using Functions Below to Save Altered results /////////// //////// Functions BELOW!!! ///////// 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); } ////// ^^^ load and parse data to set vairiables for use ////// function saveJpeg(name) { var doc = app.activeDocument; var file = new File(doc.path + '/' + name + '.jpg'); var opts = new JPEGSaveOptions(); opts.quality = 10; doc.saveAs(file, opts, true); } ////// ^^^ save Finished Results ///// //alert('Your Script has done!!!');
Beispiel für JSON-Daten.
{“title” : “LONDON”, “ord” : “51.5074° N, 0.1278° W”}
- Ich habe dann ein Stück Code gefunden und es an meine Bedürfnisse angepasst (naja, fast). Das Code-Snippet ermöglicht das Öffnen eines Dialogs und die Auswahl der benötigten Datei
Das Problem ist, dass ich es brauche, um das Bild mit dem Titelnamen aus den JSON-Daten zum Grab-Beispiel LONDON.PNG auszuwählen und dann alles ohne Dialog und Auswahl zu ersetzen (leise und automatisch).
Unten ist mein geänderter Code und ein Screenshot des Stammordners meines Projekts
#include json2.js
//////// ^^^^ loading JSON2 ///////////
var obj = loadJson('text.json');
//////// ^^^^ Variable for JSON Text Data ///////////
var titleGroup = app.activeDocument.layerSets.getByName('text');
var titleLayer = titleGroup.layers[0];
var ordinatesLayer = titleGroup.layers[1];
titleLayer.textItem.contents = obj.title;
ordinatesLayer.textItem.contents = obj.ord;
////// ^^^ Locate And change Text using JSON Data ///////////
var theLayer = app.activeDocument.layerSets.getByName('image');
var changeLayer = theLayer.layers[0]
var replacementFile = new File(obj.title + "png");
//////// ^^^ var set need to create future functions to grab image location from the json data and replace image ///////////
changeLayer = replaceContents(replacementFile);
saveJpeg(obj.title + 'Finished');
//////// ^^^ Script Action Using Functions Below to Save Altered results ///////////
//////// Functions BELOW!!! /////////
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);
}
////// ^^^ load and parse data to set vairiables for use //////
function saveJpeg(name) {
var doc = app.activeDocument;
var file = new File(doc.path + '/' + name + '.jpg');
var opts = new JPEGSaveOptions();
opts.quality = 10;
doc.saveAs(file, opts, true);
}
////// ^^^ save Finished Results /////
//alert('Your Script has done!!!');
function replaceContents (newFile) {
var idplacedLayerReplaceContents = stringIDToTypeID( "placedLayerReplaceContents" );
var desc3 = new ActionDescriptor();
var idnull = charIDToTypeID( "null" );
desc3.putPath( idnull, new File( newFile ) );
var idPgNm = charIDToTypeID( "PgNm" );
desc3.putInteger( idPgNm, 1 );
executeAction( idplacedLayerReplaceContents, desc3, DialogModes.NO );
return app.activeDocument.activeLayer
};
////// ^^^ replace contents //////
Lösung
#include json2.js
//////// ^^^^ loading JSON2 ///////////
var obj = loadJson('text.json');
//////// ^^^^ Variable for JSON Text Data ///////////
var titleGroup = app.activeDocument.layerSets.getByName('text');
var titleLayer = titleGroup.layers[0];
var ordinatesLayer = titleGroup.layers[1];
titleLayer.textItem.contents = obj.title;
ordinatesLayer.textItem.contents = obj.ord;
////// ^^^ Locate And change Text using JSON Data ///////////
var theLayer = app.activeDocument.layerSets.getByName('image');
var changeLayer = theLayer.layers[0]
/////// added and fixed below /////
var string = 'C:/Users/apps/Documents/script/';
var replacementFile = new File(string + obj.title + '.png');
//////// ^^^ var set need to create future functions to grab image location from the json data and replace image ///////////
changeLayer = replaceContents(replacementFile);
saveJpeg(obj.title + 'Finished');
//////// ^^^ Script Action Using Functions Below to Save Altered results ///////////
//////// Functions BELOW!!! /////////
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);
}
////// ^^^ load and parse data to set vairiables for use //////
function saveJpeg(name) {
var doc = app.activeDocument;
var file = new File(doc.path + '/' + name + '.jpg');
var opts = new JPEGSaveOptions();
opts.quality = 10;
doc.saveAs(file, opts, true);
}
////// ^^^ save Finished Results /////
//alert('Your Script has done!!!');
function replaceContents (newFile) {
var idplacedLayerReplaceContents = stringIDToTypeID( "placedLayerReplaceContents" );
var desc3 = new ActionDescriptor();
var idnull = charIDToTypeID( "null" );
desc3.putPath( idnull, new File( newFile ) );
var idPgNm = charIDToTypeID( "PgNm" );
desc3.putInteger( idPgNm, 1 );
executeAction( idplacedLayerReplaceContents, desc3, DialogModes.NO );
return app.activeDocument.activeLayer
};
////// ^^^ replace contents //////
Beantwortet von – Ashley Taylor
Antwort geprüft von – Katrina (FixError Volunteer)