Hiding or Showing a Layer in Photoshop is the purpose of the following script tutorial.
http://morris-photographics.com/photoshop/tutorials/scripting2.html
In Photoshop scripting, items are named "objects", but when grouped with many other objects, they are named "collections". Being together in collection means that the objects are in an array. Aspects of the objects that can be transformed constitute its properties. The actual transformation are called methods. Specific steps that are triggered by certain transformations are named events.
I used the same photoshop document that I started to use to write the Photoshop Javascript homework from last week.
This is the script from the tutorial with my comments explaining the code.
var docRef = activeDocument; //storing the property of the active document inside the variable docRef.
var layerRef = docRef.activeLayer;// stores the property of the active layer from within the active document variable
//in the variable layerRef.
layerRef.visible = !layerRef.visible;// switch the active layer on and off. If the layer is visible,
//then layerRef will turn it off and vice versa.
This is the background image(from google) for the scripting test. I outlined the 2006 corvette with a bright green line of color.
After clicking on the layer I drew the green outline on and running the script, the green outline appears and disappears.
Similarly, selecting the background layer and running the script produces the following result:
The background is white when the image is saved from Photoshop.
Next, the author of the tutorial provides a safety net for not previously opening a photoshop document before running the script. There is an array built into Photoshop that contains documents that the user creates. This array, in turn, supplies us with a length property to work with the items in the array.
I customized the following to show the error I wanted to the user to see.
-----------------------------------------------------------------------------------------------------
if (documents.length == 0) //if there aren't any available documents in the array to draw from
{
alert ("Please open a finished Photoshop document and then run the script. Thank you!")
//Dialogue box appears and displays this text.
}
else // runs the following code if the code above is false.
{
var docRef = activeDocument; //storing the property of the active document inside the variable docRef.
var layerRef = docRef.activeLayer;
// stores the property of the active layer from within the active document variable in the variable layerRef.
layerRef.visible = !layerRef.visible;
// switch the active layer on and off. If the layer is visible, then layerRef will turn it off and vice versa.
}
-----------------------------------------------------------------------------------------------------------
It produced this result which was so exciting!
The way Photoshop works, according to the tutorial, is that if a user's document includes a background layer but no additional layers, the solution for successfully running the code above is to write another conditional statement. Apparently, the artLayer object within Photoshop provides user with a changeable property call isBackground.
------------------------------------------------------------------------------------------------------
if (documents.length == 0) //if there aren't any available documents in the array to draw from
{
alert ("Please open a finished Photoshop document and then run the script. Thank you!");
//Dialogue box appears and displays this text.
}
else // runs the following code if the code above is false.
{
var docRef = activeDocument; //storing the property of the active document inside the variable docRef.
var layerRef = docRef.activeLayer;
// stores the property of the active layer from within the active document variable in the variable layerRef.
if (documents.layers.length == 1 && layerRef.isBackgroundLayer == true)
//accessing the number of layers in a document and checking if it equal 1
//and if there is a background layer.
{
alert ("Please open a document that contains a multiple layers.
Documents will background layers will not open.");
//A dialog pops up for the user instructing them how to proceed.
}
else
{
layerRef.visible = !layerRef.visible;
// switch the active layer on and off. If the layer is visible, then layerRef will turn it off and vice versa.
}
}
-----------------------------------------------------------------------------------------------------------
The dialog that was produced before was produced again because the script didn't register my script. However, when I opened an old photoshop document with a locked background layer the last code change did not run.
if (documents.layers.length == 1 && layerRef.isBackgroundLayer == true)
The above line contained the error that there is "no such element".
But the code was a useful exercise nonetheless and I have learned about scripting the inner structure of Photoshop.
No comments:
Post a Comment