Skip to main content
Inspiring
December 20, 2015
Answered

Activate Layer by Name From Clipboard

  • December 20, 2015
  • 1 reply
  • 1505 views

this is just a quick follow-up to the 'Move Layer To Cursor' topic- I'm trying to activate a particular layer, and the particular layer's name will be in the clipboard.

So for example, let's say the active layer is "Layer 1"

and the clipboard contains "Layer 5"

So I want to be able to switch over to Layer 5. The reason for this is that the clipboard might contain different layer names at any given time. So it's not as easy as just writing a script to switch to Layer 5. I need to switch to whatever layer is in the clipboard at any given time. The reason for this is a little long-winded so I'll leave that part out for now

Here's a script which I was hoping to alter and make it work:

#target photoshop

var layerRef = app.activeDocument.layers.getByName( 'Layer1' );

app.activeDocument.activeLayer = layerRef;

but I was hoping to change it to something along these lines:

#target photoshop

var layerRef = app.activeDocument.layers.getByName( 'WHATEVER IS IN THE CLIPBOARD' );

app.activeDocument.activeLayer = layerRef;

This topic has been closed for replies.
Correct answer SuperMerlin

I managed to assemble a script which stores the active layer's name as a variable (Layer 1),  it then activates a different layer, and then re-activates Layer 1 again by reading its variable,  not its name:


var curDoc  = app.activeDocument;

var newDoc = app.documents.add

app.activeDocument = curDoc;

    var curLayer = app.activeDocument.activeLayer;

var layerRef = app.activeDocument.layers.getByName( 'Layer 13' );

app.activeDocument.activeLayer = layerRef;

app.activeDocument.activeLayer = curLayer


So basically when you run the above script, it will activate "Layer 13", then re-activate the previous layer you were just on (I was using "Layer 1" as an example). Just make sure your document has a "Layer 13" in it.


So now, "Layer 1" is identified as a variable, which is called "curLayer".


What I'm trying to do is store this variable somewhere, even after the script closes. This is why I was asking about the clipboard. Where else can I store "curLayer" so that when I want to activate this layer a little later on, all I have to do is run a simple script like the following:


app.activeDocument.activeLayer = curLayer





#target Photoshop   

var SCRIPTS_FOLDER =  decodeURI(app.path + '/' + localize("$$$/ScriptingSupport/InstalledScripts=Presets/Scripts"));  

var libPath = decodeURI(SCRIPTS_FOLDER) + "/psClipboard64.dll";  

var  psClip = new ExternalObject("lib:" + libPath);  

var clipData = psClip.getClipboard();  

app.activeDocument.activeLayer = activeDocument.layers.getByName (clipData);

You have to use activeDocument.layers.getByName();

1 reply

Inspiring
December 21, 2015

I've managed to put this together, and although I think it might be close, it's still throwing an error. Basically, I'm ensuring that the clipboard contains, say,  "Layer 5". Then when I run this script, Photoshop should activate the layer "Layer 5". What am I doing wrong here

#target Photoshop 

var SCRIPTS_FOLDER =  decodeURI(app.path + '/' + localize("$$$/ScriptingSupport/InstalledScripts=Presets/Scripts"));

var libPath = decodeURI(SCRIPTS_FOLDER) + "/psClipboard64.dll";

var  psClip = new ExternalObject("lib:" + libPath);

var clipData = psClip.getClipboard();

app.activeDocument.activeLayer = clipData;

JJMack
Community Expert
Community Expert
December 21, 2015

The thing is the Clipboard supports and supports Raster pixels. More or less there are two clipboards. A text clipboard and a raster clipboard. If you make a selection and target a layer you can copy that layer pixels area to the raster clip board. Or you can do a copy merge there the visible composite of the area will be copied to the raster clipboard.  You can also target the layer name to edit it or copy it the the text clipboard.  If you want both the pixel and the layers text name in the clipboard you need to do two copies  to the clipboards. 

However if you scripting  you do not need to to the clipboard are all. You can duplicate a layer and name the duplicated layer any thing you want. The dupe can be in the same document or in some other open document in Photoshop.

JJMack
Inspiring
December 21, 2015

I think I can clarify a little better. Let's say the active layer is "Layer 1".

Next, I will switch layers. Let's say I switch to "Layer 13".

Now, I want to run the script which will activate "Layer 1" again.




Is the clipboard necessary to do this? Or is there another way that "Layer 1" can be stored in Photoshop's memory so that whenever I run the script, it always returns to (activates) "Layer 1" ?


Also, can I select "Layer 1" in a different way, perhaps by its Index# or ID# instead of its name- or does that even matter?