Quantcast
Channel: Adobe Community : Popular Discussions - Photoshop Scripting
Viewing all articles
Browse latest Browse all 12244

Photoshop CS6 using javaScript to truncate alpha channel name

$
0
0

Hello,

 

I'm a production artist and I work with PSD files that were created in Adobe Scene7 Image Authoring Tool. These PSDs contain a background layer along with 1-20 alpha channels. My script has to make a new blank layer for every alpha channel in the document. Then it fills the new layer with light gray. So far, my code accomplishes this. However, I'd like to apply the name of the alpha channel to the layer, but I need the name to be truncated. Every alpha channel starts with one or more characters followed by a backslash and then finishes with one or more characters. Here's an example:

 

An alpha channel might be named:  Floor\floor

 

In this example I need my layer name to be just:  floor. This means all character to the left of the backslash, including the backslash itself needs to be discarded. I was using the subSring() statement to do this. When I try to step through the code, line by line in ExtendScript, I immediately get an error that says Unterminated String Constant and Line 31 of my code is highlighted. I suspect it doesn't like the way I wrote the backslash character, although I surrounded it in double quotes to define it as a string.

 

Can anyone tell me why I'm getting this error?

 

Below is my code with lots of comments to walk you through the process. I wrote where the error occurs in red type.

I'm new to JavaScript so I'm not sure my while loop is accurate.

 

#target photoshop

// The #target photoshop makes the script run in PS.

 

// declare variable to contain the active document

var myDoc=app.activeDocument;

 

// declare variable to contain the number of alpha channels, excluding the RGB channels

var alphaChan = myDoc.channels.length - 3;

alert(alphaChan + " alpha channels exist");

 

// create loop to make new layers based on number of alpha channels, fill layer with gray and apply alpha channel name to new layer

for (a=0 ; a<alphaChan ; a+=1){

// make new blank layer

myDoc.artLayers.add();

 

// fill blank layer with gray

var color = new SolidColor();

color.rgb.red = 161;

color.rgb.green = 161;

color.rgb.blue= 161;

myDoc.selection.fill(color);

 

//variable stores alpha channel name

var alphaName = myDoc.channels[3+a];

 

// variable stores lenght of alpha channel name

var lz = alphaName.length;

 

// declare index variable to initialize position of 1st  character of alpha channel name

var x= 0 ;

 

// truncate alpha channel name by removing all characters preceding the "\" symbol

while (alphaName.subString(x) != "\"){          (ExtendScript gives an error for this line and highlights the backslash and surrounding quotation marks)

    alphaName = alphaName.subString((x+1),z);

    x+=1;

    z-=1;

return alphaName;

    }       

// remove the backslash from alpha channel name

alphaName = alphaName.subString((x+1),z);

    

  

//  apply truncated alpha channel name to corresponding layer

myDoc.artLayers[a].name = alphaName;

}


Viewing all articles
Browse latest Browse all 12244

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>