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

Script to copy Layer/Group to another Open Document?

Enthusiast ,
Jun 14, 2014 Jun 14, 2014

Hi, I know this topic has been covered many times before. But to clarify, here's what I'm wondering:

Copy the active layer/group to another open document in Photoshop. The simple way is to go to "Layer/Duplicate Layer..." . But this is very tedious. For starters, when you choose that method, and then select the document that you'd like to copy to via the pull-down menu, it successfully copies the layer/group to that document, but it doesn't auto-switch (activate) to the document it was just copied to. You have to hit "Ok", and then manually search through the open documents list via (Window/*choose your document*) to get there. This is a nightmare when you're trying to do this many times.

Here is what I am wondering:

Is it possible to:

a) copy the active layer/group to the clipboard/memory

b) switch to another existing document in Photoshop *via a custom menu* which shows all existing documents

c) once switched to the new existing document, paste that layer/group from the clipboard into that document

..is that possible?

At the very least, are there any scripts that anyone knows of that allows the user to switch between existing documents via a custom menu instead of going to Window/*choose your document* ?

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

Community Expert , Jun 14, 2014 Jun 14, 2014

Hi @boo radley 2013,

do you mean something like this?

// LayerPaste2anotherDoc.jsx

// https://forums.adobe.com/thread/1496531

// regards pixxxelschubser

var aDoc = app.activeDocument;

var AllDocs = app.documents;

var actLay = aDoc.activeLayer;

if (AllDocs.length > 1) {

var itemDoc = null;

actLay.copy ();

var win = new Window("dialog","Copy the active layer");

this.windowRef = win;

win.Txt1 = win.add ("statictext", undefined, "Paste in which open document?");

win.NewList=win.add ("dropdownlist

...
Translate
Adobe
Enthusiast ,
Jun 14, 2014 Jun 14, 2014

to further simpliy this:

are there any scripts that:

will open up a custom menu, and show a list of all existing documents that are currently loaded into Photoshop. This menu will allow the user to select the document they wish toi switch to, and at the same time copy that document name to the clipboard/memory?

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 ,
Jun 14, 2014 Jun 14, 2014

Hi @boo radley 2013,

do you mean something like this?

// LayerPaste2anotherDoc.jsx

// https://forums.adobe.com/thread/1496531

// regards pixxxelschubser

var aDoc = app.activeDocument;

var AllDocs = app.documents;

var actLay = aDoc.activeLayer;

if (AllDocs.length > 1) {

var itemDoc = null;

actLay.copy ();

var win = new Window("dialog","Copy the active layer");

this.windowRef = win;

win.Txt1 = win.add ("statictext", undefined, "Paste in which open document?");

win.NewList=win.add ("dropdownlist", undefined, AllDocs);

win.NewList.selection = 0;

itemDoc = win.NewList.selection.index;

win.cancelBtn = win.add("button", undefined, "Abbruch");

win.quitBtn = win.add("button", undefined, "Ok");

win.defaultElement = win.quitBtn;

win.cancelElement = win.cancelBtn;

win.quitBtn.onClick = function() {

win.close();

}

win.NewList.onChange= function () {

    itemDoc = win.NewList.selection.index;

    return itemDoc;

    }

win.show();

app.activeDocument = app.documents[itemDoc];

app.activeDocument.paste();

app.refresh();

} else {

    alert ("No other documents open")

    }

Have fun

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 ,
Jun 14, 2014 Jun 14, 2014

Thats it!! Awesome!!! Totally what I was looking for - this is a very useful script, i hope a lot of people see this one because they will all love it- thank you my friend!!

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 ,
Jun 14, 2014 Jun 14, 2014

one question though: is there a way to have this script work with both layers *and* groups? Right now it's only working with layers. If not, then maybe have two seperate scripts, one for layers and one for 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
Community Expert ,
Jun 15, 2014 Jun 15, 2014

Sorry, but to copy a group to another document is much more complex. Don't use Extend Script for this. Better is to work with Script listener Plugin (AM code) for this.

Or with Extend Script you should loop through the elements of your group and copy part by part. But there are still some things to consider.

What is if you have nested groups or deep nested groups inside of a group?

What is if you have adjustment layers?

What is if you have smart objects?

And sorry for my bad english. I hope you understand nevertheless what I mean.

One thing more:

You've never marked one of your threads as correct - why not?

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 ,
Jun 15, 2014 Jun 15, 2014

You've never marked one of your threads as correct - why not?

From a quick look at the general Forum it seems two of twenty threads are marked as correctly answered over there … personally I suspect more than that would deserve to be considered answered, so the Photoshop Scripting Forum may have posters that are more observant than average already.

I guess if you are willing to take the time and try to help other users on these Fora you better get used to a lot of them not taking the "Best Practices" too seriously.

Best practices: Usage

And as for the points that are awarded for a "correct answer" I guess it probably evens out in the long run, everybody else’s correct answers are just as likely to not be marked accordingly.

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 ,
Jun 15, 2014 Jun 15, 2014

Hi c.pfaffenbichler,

I often use the forum search. I preferred reading the correct answers and (in the past) as helpful marked answers, especially in long threads. Others are probably the same opinion.

What photoshop version do you use?

var idT = charIDToTypeID( "T  " );

gives an error in CS3 and CS5.1 under Windows.

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 ,
Jun 16, 2014 Jun 16, 2014
I often use the forum search. I preferred reading the correct answers and (in the past) as helpful marked answers, especially in long threads. Others are probably the same opinion.

Fair enough, the Fora would benefit from these features being used as intended, but with recent changes the whole Forum experience has been degraded anyway …

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
New Here ,
Jun 06, 2015 Jun 06, 2015

hi,

Is there any way to copy selected layers and groups to all the open documents which are having the same name, without prompting us to select the particular target document.

For example open documents name pattern as below mentioned

87654321.A1B3C4D2E6.FRNT.extension

     (A)                (B)            (C)


If target documents name ( A1B3C4D2E6 (B) ) matches with active document name ( A1B3C4D2E6 (B) ) then selected layers and groups in the active document should get copied to those target documents without any dialogue window.


Thanks in advance

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 ,
Jun 07, 2015 Jun 07, 2015

If I understand the naming convention correctly this should get you an array of the open documents that meet the requirement.

You could use the array to perform whatever tasks on those documents.

If you are likely to have large numbers of files open AM code might offer a better performance, though.

// 2015, use it at your own risk;

#target photoshop

if (app.documents.length > 0) {

var myDocument = app.activeDocument;

var theNameSegment = myDocument.name.split(".")[1];

var theDocs = new Array;

var openDocs = app.documents;

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

  var thisDoc = openDocs;

  if (thisDoc != myDocument) {

  var thisNameSegment = thisDoc.name.split(".")[1];

  if (thisNameSegment == theNameSegment) {theDocs.push(thisDoc)}

  }

  };

alert (theDocs.join("\n"));

};

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
New Here ,
Jun 07, 2015 Jun 07, 2015

Thank you for the assistance

I have got confused.

Could you please post the complete script?

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 ,
Jun 07, 2015 Jun 07, 2015

What about this is causing you confusion?

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
New Here ,
Jun 08, 2015 Jun 08, 2015

confusion is about where to wrap this code in existing script.

I'm brand new to scripting and in learning stage don't know much more about scripting.

I tried in many ways but not getting output.

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 ,
Jun 08, 2015 Jun 08, 2015

I don’t know which Script you are using, but where now it perform the task on one target document you can use a for-clause to do the same for each of the target documents.

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
New Here ,
Jun 08, 2015 Jun 08, 2015

Thank you for quick response.

I'm using the same script which you posted in this thread on Jun 15, 2014 7:33 AM.

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 ,
Jun 08, 2015 Jun 08, 2015

In this case I would recommend not going with the DOM documents but collecting their IDs.

var myDocument = app.activeDocument;

// get active document’s id;

var ref = new ActionReference();

ref.putProperty(stringIDToTypeID ("property"),stringIDToTypeID("documentID") );

ref.putEnumerated( charIDToTypeID("Dcmn"), charIDToTypeID("Ordn"), charIDToTypeID("Trgt") );

var docDesc = executeActionGet(ref);

var theID = docDesc.getInteger(stringIDToTypeID("documentID"));

// get name;

var theNameSegment = myDocument.name.split(".")[1];

var theDocs = new Array;

var openDocs = app.documents;

// work through open documents;

for (var m = 1; m <= openDocs.length; m++) {

var ref = new ActionReference();

ref.putIndex( charIDToTypeID("Dcmn"), m);

var docDesc = executeActionGet(ref);

var thisID = docDesc.getInteger(stringIDToTypeID("documentID"));

var thisName = docDesc.getString(stringIDToTypeID("title"));

var thisNameSegment = thisName.split(".")[1];

// collect names and ids;

if (thisNameSegment == theNameSegment && thisID != theID) {theDocs.push([thisName, thisID])}

and then wrapping the for-clause

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

in another for-clause that depends on that result and adapting the function "duplicateLayer" to use the identifier instead of the name to identify the target document.

Edit: To identify the document by its ID instead of its name change the line

        ref4.putName( idDcmn, theDoc );

to

        ref4.putIdentifier( idDcmn, theDoc );

and use the ID as the second argument.

The Array "theDocs" in the above code would be an Array of Arrays with the name the first and the ID the second element.

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 ,
Jun 08, 2015 Jun 08, 2015

Maybe this can help illustrate, instead of the alert you could insert the pertinent code from the other Script:

// 2015, use it at your own risk; 

#target "photoshop-70.032"

var AllDocs = app.documents;  

if (AllDocs.length > 1) {

var theIndex = getSelectedLayersIdx(); 

var myDocument = app.activeDocument;

// get active document’s id;

var ref = new ActionReference();

ref.putProperty(stringIDToTypeID ("property"),stringIDToTypeID("documentID") );

ref.putEnumerated( charIDToTypeID("Dcmn"), charIDToTypeID("Ordn"), charIDToTypeID("Trgt") );

var docDesc = executeActionGet(ref);

var theID = docDesc.getInteger(stringIDToTypeID("documentID"));

// get name;

var theNameSegment = myDocument.name.split(".")[1];

var theDocs = new Array;

var openDocs = app.documents;

// work through open documents;

for (var m = 1; m <= openDocs.length; m++) {

var ref = new ActionReference();

ref.putIndex( charIDToTypeID("Dcmn"), m);

var docDesc = executeActionGet(ref);

var thisID = docDesc.getInteger(stringIDToTypeID("documentID"));

var thisName = docDesc.getString(stringIDToTypeID("title"));

var thisNameSegment = thisName.split(".")[1];

// collect names and ids;

if (thisNameSegment == theNameSegment && thisID != theID) {theDocs.push([thisName, thisID])}

};

// the documents that meet the requirement; 

for (var x = 0; x < theDocs.length; x++) { 

alert ("the document »"+theDocs[0]+"« has the identifier "+ theDocs[1])

};

} else {alert ("No other documents open")}; 

////// duplicate layer ////// 

function duplicateLayer (theIndex, theDoc) { 

// ======================================================= 

var idDplc = charIDToTypeID( "Dplc" ); 

    var desc7 = new ActionDescriptor(); 

    var idnull = charIDToTypeID( "null" ); 

        var ref3 = new ActionReference(); 

    ref3.putIndex(charIDToTypeID("Lyr "), theIndex); 

    desc7.putReference( idnull, ref3 ); 

    var idT = charIDToTypeID( "T   " ); 

        var ref4 = new ActionReference(); 

        var idDcmn = charIDToTypeID( "Dcmn" );

// identify by id;

        ref4.putIdentifier( idDcmn, theDoc );

//        ref4.putName( idDcmn, theDoc ); 

    desc7.putReference( idT, ref4 ); 

    var idVrsn = charIDToTypeID( "Vrsn" ); 

    desc7.putInteger( idVrsn, 5 ); 

executeAction( idDplc, desc7, DialogModes.NO ); 

}; 

////// by paul mr; 

function getSelectedLayersIdx(){ 

      var selectedLayers = new Array; 

      var ref = new ActionReference(); 

      ref.putEnumerated( charIDToTypeID("Dcmn"), charIDToTypeID("Ordn"), charIDToTypeID("Trgt") ); 

      var desc = executeActionGet(ref); 

      if( desc.hasKey( stringIDToTypeID( 'targetLayers' ) ) ){ 

        desc = desc.getList( stringIDToTypeID( 'targetLayers' )); 

          var c = desc.count 

          var selectedLayers = new Array(); 

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

            try{ 

              activeDocument.backgroundLayer; 

              selectedLayers.push(  desc.getReference( i ).getIndex() ); 

            }catch(e){ 

              selectedLayers.push(  desc.getReference( i ).getIndex()+1 ); 

            } 

          } 

      }else{ 

        var ref = new ActionReference(); 

        ref.putProperty( charIDToTypeID("Prpr") , charIDToTypeID( "ItmI" )); 

        ref.putEnumerated( charIDToTypeID("Lyr "), charIDToTypeID("Ordn"), charIDToTypeID("Trgt") ); 

        try{ 

            activeDocument.backgroundLayer; 

            selectedLayers.push( executeActionGet(ref).getInteger(charIDToTypeID( "ItmI" ))-1); 

        }catch(e){ 

            selectedLayers.push( executeActionGet(ref).getInteger(charIDToTypeID( "ItmI" ))); 

        } 

      } 

      return selectedLayers; 

};

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 ,
Jun 09, 2015 Jun 09, 2015

Also it might be better to duplicate the Layers in one go instead of individually.

////// duplicate layers //////

function duplicateLayers (theDocID) {

// =======================================================

var idDplc = charIDToTypeID( "Dplc" );

    var desc8 = new ActionDescriptor();

    var idnull = charIDToTypeID( "null" );

        var ref5 = new ActionReference();

        var idLyr = charIDToTypeID( "Lyr " );

        var idOrdn = charIDToTypeID( "Ordn" );

        var idTrgt = charIDToTypeID( "Trgt" );

        ref5.putEnumerated( idLyr, idOrdn, idTrgt );

    desc8.putReference( idnull, ref5 );

    var idT = charIDToTypeID( "T  " );

        var ref6 = new ActionReference();

        var idDcmn = charIDToTypeID( "Dcmn" );

//        ref6.putName( idDcmn, "Untitled-1" );

        ref6.putIdentifier( idDcmn, theDocID );

    desc8.putReference( idT, ref6 );

    var idVrsn = charIDToTypeID( "Vrsn" );

    desc8.putInteger( idVrsn, 5 );

executeAction( idDplc, desc8, DialogModes.NO );

};

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
New Here ,
Jun 12, 2015 Jun 12, 2015

Sorry for the late reply

Script getting executed without any error identifying the open documents with name but selected layers and groups are not getting copied to those identified documents.

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 ,
Jun 14, 2015 Jun 14, 2015

Have you made the necessary amendments and what is the Script you use now?

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
New Here ,
Jun 14, 2015 Jun 14, 2015

Since not getting any error alert i have not made any amendments.

I have tried above two scripts and both are giving same result as i mentioned in previous reply.

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 ,
Jun 14, 2015 Jun 14, 2015

You seem to have misunderstood my previous posts:

None of the last two posted Scripts is a complete version but only provide elements that you have to combine to achieve the desired functionality.

I thought the statement

instead of the alert you could insert the pertinent code from the other Script:

should have made that clear.

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 ,
Jun 15, 2014 Jun 15, 2014

This might help with Groups and some other kinds of Layers.

But it takes the easy route with the target file by using the name to identify it.

// some amendments to pixxxelschubser’s code;

// 2014, use it at your own risk;

#target photoshop

var aDoc = app.activeDocument;

var AllDocs = app.documents;

var actLay = aDoc.activeLayer;

var theIndex = getSelectedLayersIdx();

if (AllDocs.length > 1) {

var itemDoc = null;

var win = new Window("dialog","Copy the active layer");

this.windowRef = win;

win.Txt1 = win.add ("statictext", undefined, "Paste in which open document?");

win.NewList=win.add ("dropdownlist", undefined)

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

win.NewList.add("item", AllDocs.name)

};

win.NewList.selection = 0;

itemDoc = win.NewList.selection.index;

win.cancelBtn = win.add("button", undefined, "Abbruch");

win.quitBtn = win.add("button", undefined, "Ok");

win.defaultElement = win.quitBtn;

win.cancelElement = win.cancelBtn;

win.quitBtn.onClick = function() {

win.close();

}

win.NewList.onChange= function () {

    itemDoc = win.NewList.selection.index;

    return itemDoc;

    }

win.show();

// duplicate layers;

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

duplicateLayer (theIndex, String(win.NewList.selection))

};

//

app.refresh();

} else {

    alert ("No other documents open")

    };

////// duplicate layer //////

function duplicateLayer (theIndex, theDoc) {

// =======================================================

var idDplc = charIDToTypeID( "Dplc" );

    var desc7 = new ActionDescriptor();

    var idnull = charIDToTypeID( "null" );

        var ref3 = new ActionReference();

    ref3.putIndex(charIDToTypeID("Lyr "), theIndex);

    desc7.putReference( idnull, ref3 );

    var idT = charIDToTypeID( "T  " );

        var ref4 = new ActionReference();

        var idDcmn = charIDToTypeID( "Dcmn" );

        ref4.putName( idDcmn, theDoc );

    desc7.putReference( idT, ref4 );

    var idVrsn = charIDToTypeID( "Vrsn" );

    desc7.putInteger( idVrsn, 5 );

executeAction( idDplc, desc7, DialogModes.NO );

};

////// by paul mr;

function getSelectedLayersIdx(){

      var selectedLayers = new Array;

      var ref = new ActionReference();

      ref.putEnumerated( charIDToTypeID("Dcmn"), charIDToTypeID("Ordn"), charIDToTypeID("Trgt") );

      var desc = executeActionGet(ref);

      if( desc.hasKey( stringIDToTypeID( 'targetLayers' ) ) ){

        desc = desc.getList( stringIDToTypeID( 'targetLayers' ));

          var c = desc.count

          var selectedLayers = new Array();

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

            try{

              activeDocument.backgroundLayer;

              selectedLayers.push(  desc.getReference( i ).getIndex() );

            }catch(e){

              selectedLayers.push(  desc.getReference( i ).getIndex()+1 );

            }

          }

      }else{

        var ref = new ActionReference();

        ref.putProperty( charIDToTypeID("Prpr") , charIDToTypeID( "ItmI" ));

        ref.putEnumerated( charIDToTypeID("Lyr "), charIDToTypeID("Ordn"), charIDToTypeID("Trgt") );

        try{

            activeDocument.backgroundLayer;

            selectedLayers.push( executeActionGet(ref).getInteger(charIDToTypeID( "ItmI" ))-1);

        }catch(e){

            selectedLayers.push( executeActionGet(ref).getInteger(charIDToTypeID( "ItmI" )));

        }

      }

      return selectedLayers;

};

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 ,
Jun 15, 2014 Jun 15, 2014

sorry guys, i wasn't even aware of the 'correct answer' feature - i'll keep my eyes on it from now on

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