
April 20th, 2007, 07:48 AM
|
|
Contributing User
|
|
Join Date: Apr 2007
Posts: 1
Time spent in forums: < 1 sec
Reputation Power: 0
|
|
|
Help with a photoshop javascript
Can someone please help me modify this javascript to edit two two text layers in photoshop from a database. I am able to get one layer to work, but not two. Here is the script. Or can this be done with scripting in photoshop cs2. I am trying to use a database of names and a layer in photoshop to make new images using the names in the database.
thank you very much.
here is the script.
/*
ADOBE PHOTOSHOP(R) SCRIPT TO CREATE PERSONALIZED IMAGES
BACKGROUND:
This script can generate personalized images (.JPG) from a native Photoshop file (.PSD)
and a plain-text, tab-delimited database.
URL
URL (for the Document Object Model)
*/
/*
NOTE: THIS SCRIPT IS MEANT FOR PHOTOSHOP VERSION 7
*/
/*
STEPS:
1. Put your photoshop file (.psd), database (.txt) and this script (.js) in one folder, for example your desktop.
2. Create a folder 'images' (without quotes) in this folder. For example, you now have a folder images on your
desktop
3. Open Photoshop and your .psd file.
4. Make sure it has at least one textlayer. Open the layer's properties (right mouse click on the layer) and give it
a name. For example, give your textlayer the name 'PSM' (Without quotes).
5. Create a plain-text, tab delimited database with the following structure:
ID NAME
1 Anna
2 Bob
3 Caroline
Be sure not to remove the first row with the field headers (id & Name). It's important that your database
does not contain more than two columns (only Id and Name). Field names are not case-sensitive.
6. Change the user-defined variables in this script (SEE BELOW POINT 8), and save the script.
Database refers to the name of your database.
Pathname is the pathname of the images folder.
Textlayername is the name of the Photoshop text layer.
7. In Photoshop, goto Menu File -> Automate -> Script. Now, a dialog screen appears. Click on the Browse Button, and
select this script. Click on OK.
8. The pictures will now be generated
USER_DEFINED VARIABLES
Please add your database name and pictures path:
*/
var database = "Database.txt";
var pathname = "images";
var textlayername = "PSM";
var JPGQuality = 12;
// IMPORTANT: USE THE BACKSLASH ESCAPE CHARACTER FOR
// ESCAPING BACKSLASHES IN THE PATHNAME OR DATABASE VARIABLE
//
// If you want to have the images generated in the folder 'c:\temp\last_month',
// than the correct syntax is:
//
// var pathname = "C:\\temp\\last_month\\images";
if(documents.length==0){
alert("Error: The native photoshop file (*.psd) must be opened.")
}else{
// Set preferences
var defaultRulerUnits = preferences.rulerUnits;
preferences.rulerUnits = Units.PIXELS;
// Set job variables
var docWidth=activeDocument.width;
var docHeight=activeDocument.height;
var docResolution=activeDocument.resolution;
var docMode = activeDocument.mode;
var counter = 0;
// You can choose your preferred DOcumentFill by toggling on/off the right comment marks
// var docFill = DocumentFill.TRANSPARENT;
// var docFill = DocumentFill.BACKGROUNDCOLOR;
var docFill = DocumentFill.WHITE;
// Add a new PSD document, based on current document, and call this document 'Compilation'
var psmTemplate = activeDocument;
var newDoc = documents.add(docWidth, docHeight, docResolution, "Compilation",docMode, docFill);
// Return the focus to initial PSD document
activeDocument = psmTemplate;
// Open database
var dbText = new File(database);
dbText.open ('r');
// Loop through database
while (!dbText.eof) {
// Read line
var line=dbText.readln();
var arrLine=new Array();
// Split line, use tab as delimiter
arrLine = line.split("\t");
// Assign variables to field values
var ln_number=arrLine[0];
var ln_name=arrLine[1];
// Skip first line
if (counter>0) {
// Optionally: you may want to enable a quick-and-dirty CopyFitting
//
// if (ln_name.length >= 15) {
// psmTemplate.layers[textlayername].textItem.size = 48
// }
// Text Merging, you can replace Dear by your own text.
psmTemplate.layers[textlayername].textItem.contents = "Dear " + ln_name;
// Flatten template
psmTemplate.flatten();
// Add background noise; you can comment this option off if you like
psmTemplate.layers['Background'].applyAddNoise(1.00,NoiseDistribution.UNIFORM,0);
// Copy the changed file to compilation file
psmTemplate.selection.selectAll();
psmTemplate.selection.copy();
activeDocument = newDoc;
newDoc.paste();
newDoc.layers[0].name = ln_name;
// Save compilation file to JPG
// You can also save to: EPS, TIFF, PDF, GIF, PNG, and more.
// See the Document Object Model on how to accomplish this.
// URL
var imgName=pathname+"\\img"+ln_number+".jpg"
saveFile = new File(imgName);
saveOptions = new JPEGSaveOptions();
saveOptions.embedColorProfile = true;
saveOptions.formatOptions = FormatOptions.STANDARDBASELINE;
saveOptions.matte = MatteType.NONE;
saveOptions.quality = 12; //ranges from 0 to 12
newDoc.saveAs(saveFile, saveOptions, true,Extension.LOWERCASE);
// Free resources by deleting previous image layer from memory
newDoc.layers[0].remove();
}
// increase counter
counter++;
// swap focus to initial document
activeDocument = psmTemplate;
// Reset initial document to initial state
psmTemplate.activeHistoryState = psmTemplate.historyStates[0];
}
}
// Restore initial settings
preferences.rulerUnits = defaultRulerUnits;
|