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

Activate Layer by Name From Clipboard

Enthusiast ,
Dec 20, 2015 Dec 20, 2015

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;

TOPICS
Actions and scripting
1.4K
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

correct answers 1 Correct answer

Guide , Dec 21, 2015 Dec 21, 2015

#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();

Translate
Adobe
Enthusiast ,
Dec 20, 2015 Dec 20, 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;

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 ,
Dec 20, 2015 Dec 20, 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
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
Enthusiast ,
Dec 20, 2015 Dec 20, 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?



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 ,
Dec 20, 2015 Dec 20, 2015

In a you can save the active layer to a varable make other laters active and then make the orignal actve layer the active layer once again using the varable you save it in.

function code() {

  var savactiveLayer = activeDocument.activeLayer;      //////////////////////////////////////////////////////////////////////////////////////////////////////

  var layers = activeDocument.layers;

  if (layers[0].kind != LayerKind.TEXT) { alert("Top layer not a text layer.");}

  else {

  var currSize = layers[0].textItem.size.value ;

  var numberOfImages = getAlphaImageChannels();

  if ( numberOfImages == 0 ) { alert("No Image mapping Alpha channels"); }

                else {

  Position = 8;

  while ( currSize != 0 ) {

  currSize = Number(prompt("Enter text point size. Cancel or 0 to accept current size", currSize ));

  if (isNaN(currSize)) { currSize = 0 ; }

  if ( currSize != 0 ) {

  var Position = Number(prompt("Select Position\n1 2 3\n4 5 6\n7 8 9", Position));

  if (isNaN(Position)) { Position = 8; }

  for (var i = 0; i<numberOfImages; i++) {

  if (layers.kind == LayerKind.TEXT) {

  layers.textItem.size = currSize + "pt" ;

  activeDocument.activeLayer = layers; ////////////////////////////////////////////////////////////////////////////////////////////////////////////////

  loadAlpha("Image " + (numberOfImages-i) );

  switch (Position){

  case 1 : align('AdLf'); align('AdTp'); break;

  case 2 : align('AdCH'); align('AdTp'); break;

  case 3 : align('AdRg'); align('AdTp'); break;

  case 4 : align('AdLf'); align('AdCV'); break;

  case 5 : align('AdCH'); align('AdCV'); break;

  case 6 : align('AdRg'); align('AdCV'); break;

  case 7 : align('AdLf'); align('AdBt'); break;

  case 8 : align('AdCH'); align('AdBt'); break;

  case 9 : align('AdRg'); align('AdBt'); break;

  default : break;

  }

  }

  }

  }

  activeDocument.selection.deselect();

  activeDocument.activeLayer = savactiveLayer;

  refresh();

  }

  }

  }

  activeDocument.activeLayer = savactiveLayer;  //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

}

JJMack
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
Enthusiast ,
Dec 20, 2015 Dec 20, 2015

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




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
Guide ,
Dec 21, 2015 Dec 21, 2015

#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();

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
Enthusiast ,
Dec 21, 2015 Dec 21, 2015

SuperMerlin- that's it- exactly what I was looking for - thank you!!

What I do first is I copy the layer name to the clipboard with the following code:

app.system( "echo "   + activeDocument.activeLayer.name + " | CLIP");

and this way the layer name is now stored. I can now switch to a different layer and do some more work. When I want to re-activate the previous layer, I just run your script. No need to search through hundreds of layers to find the previous layer, just run the script. So I can bounce back and forth between the two layers easily.

On a side note, when I run the code above, it successfully copies the layer name to the clipboard, but it also includes empty spaces before/after the layer name for some reason. So for example, if the layer name is:

"Layer 15"

it copies it to the clipboard as:

"  Layer 15 "

Note the empty spaces before and after Layer 15. I'm not sure if it's adding the empty space only at the beginning or only at the end or both, but either way, it's adding these empty spaces which is causing a problem when running your script- because if it's trying to re-activate "Layer 15", the problem is that the clipboard actually says " Layer 15 " (with the empty spaces) , so it can't find the layer and the script throws an error. I've managed to solve this by trimming those empty spaces in the clipboard via 3rd party software first, but it's a pain.

Long story short - why does the above line of code insert empty spaces before/after the layer name before it copies it to the clipboard?

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
Guide ,
Dec 21, 2015 Dec 21, 2015

Not sure why the spaces are being inserted, but why are you going through dos to put it on the clipboard?

You can use..

psClip.setClipboard(app.activeDocument.activeLayer.name);

To make sure no spaces arre used you can use...

#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.toString.replace(/^\s+|\s+$/,''));

This will remove both trailing and beginning spaces.

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
Enthusiast ,
Dec 21, 2015 Dec 21, 2015

the first script gives the following error:

Error 2: psClip is undefined.

Line: 1

->  psClip.setClipboard(app.activeDocument.activeLayer.name);

and the second script gives this error:

Error 24: clipData.toString.replace is not a function.

Line: 6

->  app.activeDocument.activeLayer = activeDocument.layers.getByName (clipData.toString.replace(/^\s+|\s+$/,''));  

-thanks again for the help by the way.

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
Guide ,
Dec 21, 2015 Dec 21, 2015

The first error is because it is using the external lib and needs to defined.

#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);

psClip.setClipboard(app.activeDocument.activeLayer.name);

The other is my fault, I didn't test it, this should be better now.

#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.toString().replace(/^\s+|\s+$/,''));

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
Enthusiast ,
Dec 21, 2015 Dec 21, 2015
LATEST

awesome- these are really going to come in handy not for myself but I'm sure for other users who want to do something similar. The last script you posted is still throwing a 1302 "no such element" error at line 8 - but it doesn't matter, because the script before it seems to do the trick - it copies the layer name to the clipboard without adding any additional spaces or characters, and does not go through dos. So I think everything has been accomplished - thanks to you

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