Skip to content

Instantly share code, notes, and snippets.

@joonaspaakko
Last active August 29, 2015 14:02
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save joonaspaakko/09e8e2a218038e15344d to your computer and use it in GitHub Desktop.
Save joonaspaakko/09e8e2a218038e15344d to your computer and use it in GitHub Desktop.
Helps you save selected layer comps as a single psd file.
/*
Author: https://github.com/joonaspaakko
Gist: https://gist.github.com/joonaspaakko/09e8e2a218038e15344d
<javascriptresource>
<name>Menu=Selected Layer Comps to PSD...</name>
<category>layercomps</category>
</javascriptresource>
*/
// enable double clicking from the Macintosh Finder or the Windows Explorer
#target photoshop
try {
var save, cancel, dlg, tempkeep;
// If documents exist, do stuff...
if ( app.documents.length > 0 ) main();
function main() {
// Currently active document...
var doc = app.activeDocument;
// Document has been saved...
try {
var docPath = Folder( doc.fullName.parent ).fsName;
var docName = decodeURI( doc.fullName.name.substring( 0, app.activeDocument.fullName.name.indexOf(".") ) );
}
// Document has not been saved...
catch( e ) {
var docPath = "";
var docName = doc.name;
}
// THIS IS WHERE DECISIONS ARE MADE!?!?!?!?!?
dialog( doc, docName, docPath );
if ( !cancel ) {
// Duplicate current document
var tempDoc = doc.duplicate();
// Get layer comps
var layerComps = tempDoc.layerComps;
var shredder = [];
// Loop through each comp...
for ( i = 0; i < layerComps.length; i++ ) {
// Each looped comp
var comp = layerComps[ i ];
// If comp is not selected...
if ( !comp.selected ) {
// ...push it to shredder
shredder.push( comp );
}
}
// Loop through each shreddable comp...
for ( i = 0; i < shredder.length; i++ ) {
// Each looped comp
var comp = shredder[ i ];
// Shred it
comp.remove();
}
// Save temporary document as a psd file.
if ( save ) savePSD( doc, docName, docPath, tempDoc);
// Close temporary document.
tempDoc.close( SaveOptions.DONOTSAVECHANGES );
// If user checks the checkbox "Keep new file open",
// this part of the code actually opens up the file again...
if ( dlg.export.tempkeep.value ) {
// Find the file we just saved...
var savedFile = new File( dlg.destination.input.text + '/' + dlg.filename.input.text + '.psd' );
// ...open it
app.open( savedFile );
}
}
}
} catch ( e ) {}
function dialog( doc, docName, docPath, tempDoc ) {
// Prepare dialog...
var dialog = "dialog { \
text: 'Export selected layer comps as PSD', \
destination: Panel { \
alignment: 'left', \
alignChildren:['left','top'], \
orientation: 'row', \
text: 'File destination', \
input: EditText { text: '"+ docPath +"/', preferredSize: [230,20] }, \
browse: Button { text: 'Browse...' }, \
}, \
filename: Panel { \
alignment: 'left', \
alignChildren:['left','top'], \
orientation: 'row', \
text: 'Filename', \
input: EditText { text: '"+ docName +"', active: true, preferredSize: [320,20] }, \
}, \
export: Group { \
alignment: 'right', \
orientation: 'row', \
tempkeep: Checkbox { text: 'Keep new file open', value: false }, \
save: Button { text: 'Save', properties:{ name: 'ok' } }, \
cancel: Button { text: 'Cancel' }, \
} \
}";
// Make dialog
dlg = new Window( dialog );
// Browse click event...
dlg.destination.browse.onClick = function() {
// Browse for destination
destination = Folder.selectDialog("Select destination", docPath );
// If destination was selected above...
if ( destination != null ) {
// Update input path
dlg.destination.input.text = destination.fsName;
}
}
dlg.export.save.onClick = function() {
save = true;
dlg.close();
}
dlg.export.cancel.onClick = function() {
cancel = true;
dlg.close();
}
// Show dialog
dlg.show();
}
function savePSD( doc, docName, docPath, tempDoc ) {
// Options for the psd file
var psd_Opt = new PhotoshopSaveOptions();
psd_Opt.layers = true; // Keep layers
// Save active document to the selected destination
tempDoc.saveAs( File( dlg.destination.input.text + '/' + dlg.filename.input.text + '.psd' ), psd_Opt, true );
app.beep();
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment