Skip to content

Instantly share code, notes, and snippets.

@cameronmcefee
Created September 7, 2012 16:01
Show Gist options
  • Save cameronmcefee/3667414 to your computer and use it in GitHub Desktop.
Save cameronmcefee/3667414 to your computer and use it in GitHub Desktop.
/*
o----------------------o----------------------o
CAMERON McEFEE | 530.339.8372
New Media Designer | [email protected]
o----------------------o----------------------o
Thanks for looking!
Duplicate-To-All is a Photoshop script by Cameron McEfee.
www.cameronmcefee.com
*/
if( app.documents.length > 0 ){
// Adds limited support for specific layer position
//
// insertAt - if set to 0, the duplicated layers will appear before
// the currently selected layer. If greater than to 0,
// the duplicated layer will appear at the specified index.
// 1 is the top-most layer of the photoshop document.
// saveAndClose - if true, all documents will be saved and closed. Use carefully.
//
var dupeOpts = {
insertAt: 3,
saveAndClose: true
}
duplicateToAll(dupeOpts);
}
function duplicateToAll(opts){
var errors = '',
docs = app.documents,
curDoc = app.activeDocument
groupLayerset();
// duplicate layers
for(var i = 0; i < docs.length; i++){
if(curDoc != docs[i]){
try {
var curLayer = (opts.insertAt && opts.insertAt > 0) ? docs[i].artLayers[dupeOpts.insertAt-1] : docs[i].activeLayer;
curDoc.activeLayer.duplicate(docs[i],ElementPlacement.PLACEATBEGINNING);
app.activeDocument = docs[i];
app.activeDocument.activeLayer.name = 'Duplicate To All Group';
docs[i].activeLayer.move(curLayer, ElementPlacement.PLACEBEFORE);
ungroupLayerset();
} catch(e) {
errors += '"Duplicate to All" has encountered an error. Please email the text below to [email protected] \n' + 'Error with PS Script: Duplicate to All at [duplicateToAll()] \n\n' + e + '\n\n'
}
}
app.activeDocument = curDoc;
}
// save and close
if(opts.saveAndClose && errors == ''){
try {
for (var i = docs.length-1; i >= 0; i--) {
if(curDoc != docs[i]){
docs[i].close(SaveOptions.SAVECHANGES)
}
}
} catch(e){
errors += "Couldn't save your files\n\n"
}
}
// ungroup
ungroupLayerset();
// report
if (errors == ''){
alert('"Duplicate to All" complete');
} else {
alert(errors)
}
}
function ungroupLayerset(){
var m_Dsc01 = new ActionDescriptor();
var m_Ref01 = new ActionReference();
m_Ref01.putEnumerated( cTID( "Lyr " ), cTID( "Ordn" ), cTID( "Trgt" ) );
m_Dsc01.putReference( cTID( "null" ), m_Ref01 );
try {
executeAction( sTID( "ungroupLayersEvent" ), m_Dsc01, DialogModes.NO );
} catch(e) {
alert('"Duplicate to All" has encountered an error. Please email the text below to [email protected] \n' + 'Error with PS Script: Duplicate to All at [ungroupLayerset()] \n\n' + e);
}
}
function cTID(s){return charIDToTypeID(s)}
function sTID(s){return stringIDToTypeID(s)}
function groupLayerset(){
var desc = new ActionDescriptor();
var ref = new ActionReference();
ref.putClass( stringIDToTypeID('layerSection') );
desc.putReference( charIDToTypeID('null'), ref );
var ref = new ActionReference();
ref.putEnumerated( charIDToTypeID('Lyr '), charIDToTypeID('Ordn'), charIDToTypeID('Trgt') );
desc.putReference( charIDToTypeID('From'), ref );
try {
executeAction( charIDToTypeID('Mk '), desc, DialogModes.NO );
} catch(e) {
alert('"Duplicate to All" has encountered an error. Please email the text below to [email protected] \n' + 'Error with PS Script: Duplicate to All at [groupLayerset()] \n\n' + e);
}
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment