Exit
  • Global community
    • Language:
      • Deutsch
      • English
      • Español
      • Français
      • Português
  • 日本語コミュニティ
  • 한국 커뮤니티
0

Javascript template for cs4 needed

Advocate ,
Mar 07, 2009 Mar 07, 2009
I am in need of some help to create a javascript to write jpeg files from PS-CS4 psd files. What would be most useful is a pointer to a suitable Javascript template to tailor to my needs. The operations seem simple enough, but my only scripting experience is limited to simple ActionScript 3.0 .

I have numerous psd files each of which has 12 layers (some layers are groups). Think of these as 3 scene layers and 9 subject variation layers. I need to select a scene layer, select a subject variation layer and then write a jpeg file of that composition to a folder. The file name wants to be psdname_scenelayername_subjectvariationname.jpg, where each of the names is taken from the open psd file and the layers selected. This is then repeated so that each subject layer (9) is paired with each scene layer (9) yielding 27 separate files. I actually have to do this with various psd files, but I can manually run the script on each psd file for now instead of batch processing. This sounds like setting layer comps and then sending comps to files, but the subject variation layers contain smart objects and adjustment layers that layer comps (apparently) cannot track.

So it seems that on an open psd file I need to:

set the visibility of all layers off
set the visibility of scene A on
set the visibility of subject variation 1 on
save as jpeg
set name (psdname_A_1.jpg)
set jpeg quality
set destination folder
repeat above for each subject variation, changing the jpg file name
repeat above for each scene, changing the jpg file name
end

Any help in getting this automation started would be appreciated.

Thanks,
Paulo
TOPICS
Actions and scripting
1.3K
Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Adobe
Community Expert ,
Mar 12, 2009 Mar 12, 2009
That doesnt sound too bad.
But how do You mark subject variation-layers as opposed to scene-layers?
Do they adhere to a naming-convention or are they stacked in a defined order (possibly in layer sets)?
Because obviously the Script would have to be able to identify the members of the two groups.
Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Advocate ,
Mar 12, 2009 Mar 12, 2009
The answer is naming conventions. All of the subject psd files have the same naming structure. So for each file to be written the visibility of, say for example , Scene 0 and subject 4 would have to be turned on and then saved as a jpeg, somehow using the codes 0 and 4 in the file id.

Paulo
Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Mar 12, 2009 Mar 12, 2009
Provided none of the layers reside in a Layer Set, which would make things a bit more complicated, You could give this a try (»scene« and »subject« both in lower case though, You can change that easily in the Script):


#target photoshop

var myDocument = app.activeDocument;

var docName = myDocument.name;

var baseName = docName.match(/(.*)\.[^\.]+$/)[1];

var docPath = myDocument.path;

var jpgOptions = new JPEGSaveOptions();

jpgOptions.embedProfile = true;

jpgOptions.formatOptions = FormatOptions.STANDARDBASELINE;

jpgOptions.matte = MatteType.NONE;

jpgOptions.quality = 10;

theFolder = Folder.selectDialog("select a folder to save the jpgs to");

var theStart = myDocument.activeHistoryState;

var theScenes = new Array;

var theSubjects = new Array;

for (var m = 0; m < myDocument.layers.length; m++ ) {

var theLayerName = myDocument.layers.name;

switch (theLayerName.slice(0, 5)) {

case "scene": theScenes = theScenes.concat (theLayerName);

break;

case "subje": theSubjects = theSubjects.concat (theLayerName);

break}

};

for (var n = 0; n < myDocument.layers.length; n++ ) {

myDocument.layers.visible = false;

};

for (var o = 0; o < theScenes.length; o++ ) {

myDocument.layers[ theScenes ].visible = true;

for (var p = 0; p < theSubjects.length; p++ ) {

myDocument.layers[ theSubjects

].visible = true;

var theCopy = myDocument.duplicate (theCopy, true);

theCopy.bitsPerChannel = BitsPerChannelType.EIGHT;

theCopy.saveAs((new File(theFolder+"/"+baseName+"_"+theScenes.slice(-1)+"_"+theSubjects

.slice(-1)+".jpg")),jpgOptions,true);

theCopy.close(SaveOptions.DONOTSAVECHANGES);

myDocument.layers[ theSubjects

].visible = false

};

myDocument.layers[ theScenes ].visible = false;

};

myDocument.activeHistoryState = theStart;

// thats it; thanks to xbytor;

// use it at your own risk;

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Advocate ,
Mar 13, 2009 Mar 13, 2009
christoph
Many thanks for this proposed solution. Even if some details need to be changed, having a great starting point like this is a most welcomed contribution. I am currently out of town and away from my office, but when I return at the beginning of the week I will see how this code performs.

I am in your debt. While it did not start out this way, the current state of the project requires the creation of 1080 images and that is far too many without some automation.

Thanks again,
Paulo
Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Mar 15, 2009 Mar 15, 2009
Youre welcome.

But I must admit I cut two corners:
Firstly I didnt sufficiently comment the posted Script, which I could remedy if You consider it necessary.
Secondly I supposed the numbers for Scenes and Subjects would be between zero and nine, so that slicing the last letter of the name should provide discernable identification for the newly saved files. With higher numbers some files would simply get replaced without notice. So it would be prudent to implement some method to grab the whole of the numbers.
Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Advocate ,
Mar 15, 2009 Mar 15, 2009
I think the sparse commenting will not be a problem especially since I will use this code as an opportunity to learn more about Javascript. I also understand your warning about the naming of the files.

I have learned a relevant thing about nomenclature. That is, your warning about "layer sets" is relevant since I do have some "layer groups" in my psd files. Apparently, those designations are the old and new names for the same thing in Photoshop and I will have to deal with those complications. Would using scriptlistener be helpful in learning how to change the visibility of a layer set?

Paulo
Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Mar 15, 2009 Mar 15, 2009
Regarding the nomenclature You can check out the Object Model Viewer in ExtendScript Toolkit or »Photoshop CS4 JavaScript Ref.pdf«.

The visibility of a LayeSet can be changed just like a Layers:
"app.activeDocument.layers["the groups name"].visibility = true"

And the matter can get difficult because a Layer is the child of a Document only if it does not reside in a LayerSet, in which case it is that LayerSets child, meaning one has to address it not as "app.activeDocument.layers["the layers name or index"]" but "app.activeDocument.layers["the groups name or index"].layers["the layers name or index"]" and so on, as far as I know.

How are the LayerSets and the ArtLayers organized with regard to the visibility demands for the jpgs?
Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Advocate ,
Mar 20, 2009 Mar 20, 2009
Christopf,
Sorry for the late reply, just returned to the office.

In any psd file that contains one or more layer sets, the layer sets are all unique, that is, there is no common organization in them as I used whatever function or effects necessary to get the desired overall result. I am not sure if this answers your question or not, but wherever a layer set exists all layers in the set have visibility turned on and it would be the set (or group) whose visibility would be changed. Specifically, any set that exists is a scene and the set name is the scene name.

Paulo
Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Mar 21, 2009 Mar 21, 2009
In that case treating the groups as layers should work out as long as their names follow the same conventions.
The visibility of the layers inside the groups should not get changed by turning the groups visibility on and off.
I guess.
Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Advocate ,
Mar 21, 2009 Mar 21, 2009
LATEST
Great, I think the solution for my problem is coming into focus.
Thanks again,

Paulo
Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines