Last active
April 9, 2020 15:25
-
-
Save joonaspaakko/898394cf2b46ae7b6ad7 to your computer and use it in GitHub Desktop.
Script for photoshop that duplicates layers or groups and unlinks any possible smart objects.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// https://gist.github.com/joonaspaakko/898394cf2b46ae7b6ad7 | |
#target photoshop | |
// If there are any documents open, run the code... | |
// ================================================= | |
if ( app.documents.length > 0 ) { | |
// Used later on when the layers are duplicated back to the original document. | |
// =========================================================================== | |
var docName = app.activeDocument.name; | |
// Script listener code - Duplicate active group to a new document. | |
// ================================================================= | |
var idMk = charIDToTypeID( "Mk " ); | |
var desc14 = new ActionDescriptor(); | |
var idnull = charIDToTypeID( "null" ); | |
var ref10 = new ActionReference(); | |
var idDcmn = charIDToTypeID( "Dcmn" ); | |
ref10.putClass( idDcmn ); | |
desc14.putReference( idnull, ref10 ); | |
var idUsng = charIDToTypeID( "Usng" ); | |
var ref11 = new ActionReference(); | |
var idLyr = charIDToTypeID( "Lyr " ); | |
var idOrdn = charIDToTypeID( "Ordn" ); | |
var idTrgt = charIDToTypeID( "Trgt" ); | |
ref11.putEnumerated( idLyr, idOrdn, idTrgt ); | |
desc14.putReference( idUsng, ref11 ); | |
var idVrsn = charIDToTypeID( "Vrsn" ); | |
desc14.putInteger( idVrsn, 5 ); | |
executeAction( idMk, desc14, DialogModes.NO ); | |
// Script listener code - Duplicate active group to the prior document. | |
// ===================================================================== | |
var idDplc = charIDToTypeID( "Dplc" ); | |
var desc16 = new ActionDescriptor(); | |
var idnull = charIDToTypeID( "null" ); | |
var ref13 = new ActionReference(); | |
var idLyr = charIDToTypeID( "Lyr " ); | |
var idOrdn = charIDToTypeID( "Ordn" ); | |
var idTrgt = charIDToTypeID( "Trgt" ); | |
ref13.putEnumerated( idLyr, idOrdn, idTrgt ); | |
desc16.putReference( idnull, ref13 ); | |
var idT = charIDToTypeID( "T " ); | |
var ref14 = new ActionReference(); | |
var idDcmn = charIDToTypeID( "Dcmn" ); | |
ref14.putName( idDcmn, docName ); | |
desc16.putReference( idT, ref14 ); | |
var idVrsn = charIDToTypeID( "Vrsn" ); | |
desc16.putInteger( idVrsn, 5 ); | |
executeAction( idDplc, desc16, DialogModes.NO ); | |
// Closes the new document. | |
// ======================================================= | |
app.activeDocument.close( SaveOptions.DONOTSAVECHANGES ); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
@anoopnr, there shouldn't be anything there that wouldn't work in just about any version of Photoshop upwards from CS3. I tried it just in case and it worked PS CC 2019-2020.
The error points to
line 15
and the whole chunk of code that starts from there. This error tends to happen when the "action" can't be executed for various reasons. This script is pretty much just a glorified Action so I'm going to lay down all the steps this script automates below and I want you to try doing either the whole process or just step 2, as that should be the problem area. See what happens.If you're able to do it manually, try the script again. If doing it manually fails, see if there are any clues as to why it won't work. Off the top of my head, I can't think of any scenarios why it wouldn't work, but actions like layer duplication could fail if some other progress is not completed. Say free transform, cropping, quick mask, perhaps a dialog is open or something like that. But basically, if you can perform the steps below manually, then there shouldn't be a reason for the script to throw an error either.
Lines 15-30
Layers panel → Right-click one of the active layersDuplicate layer(s)...
and thenDestination > Document: New
.Lines 36-52
Layers panel (in the new document) → Right-click one of the active layersDuplicate layer(s)...
and thenDestination > Document: whatever-the-previous-document-is-called
.