Copy link to clipboard
Copied
Hi,
I found this script in this forum i almost fits for my need but only thing i need is to add save jpeg option to this script so that i can save the selected area as an new jpeg file in the same path location in document name in a separate folder.
Can anyone help me on this
Here is the code
#target photoshop
// bring application forward for double-click events
app.bringToFront();
// ensure at least one document open
if (!documents.length) alert('There are no documents open.', 'No Document');
else main(); // at least one document exists proceed
///////////////////////////////////////////////////////////////////////////////
// main function //
///////////////////////////////////////////////////////////////////////////////
function main() {
try {
// Set the ruler units to PIXELS
var orig_ruler_units = app.preferences.rulerUnits;
app.preferences.rulerUnits = Units.PIXELS;
if(hasSelection()) newDocFromSelection();
// Reset units to original settings
app.preferences.rulerUnits = orig_ruler_units;
}
// display error message if something goes wrong
catch(e) { alert(e + ': on line ' + e.line, 'Script Error', true); }
}
///////////////////////////////////////////////////////////////////////////////
// main function end //
///////////////////////////////////////////////////////////////////////////////
function hasSelection(doc) {
if (doc == undefined) doc = activeDocument;
var res = false;
var activeLayer = activeDocument.activeLayer; // remember active layer
var as = doc.activeHistoryState; // get current history state #
doc.selection.deselect();
if (as != doc.activeHistoryState) { // did the deselect get recorded
res = true;
doc.activeHistoryState = as; // backup and reselect
}
activeDocument.activeLayer = activeLayer; // insure active layer didn't change
return res; // return true or false
}
function newDocFromSelection() {
var AD = app.activeDocument.name; // remember current document name
var docPath = AD.path // I declared a variable to get source path
/* the following code seems to be from the script listener what it
does is to add a layer via copy selection ( Ctrl+J) which will fail
if the select area of the active layer is empty then code then creates
a new documents with a normal layer cotaining the contents of that layer */
executeAction( charIDToTypeID('CpTL'), undefined, DialogModes.NO );//
var desc5 = new ActionDescriptor();
var ref2 = new ActionReference();
ref2.putClass( charIDToTypeID('Dcmn') );
desc5.putReference( charIDToTypeID('null'), ref2 );
desc5.putString( charIDToTypeID('Nm '), "Swatch "+documents.length.toString());
var ref3 = new ActionReference();
ref3.putEnumerated( charIDToTypeID('Lyr '), charIDToTypeID('Ordn'), charIDToTypeID('Trgt') );
desc5.putReference( charIDToTypeID('Usng'), ref3 );
desc5.putString( charIDToTypeID('LyrN'), "From Selection" );
executeAction( charIDToTypeID('Mk '), desc5, DialogModes.NO );
app.activeDocument.trim(TrimType.TRANSPARENT); // trim new document transparency
activeDocument = documents.getByName(AD); // retun to the old document
activeDocument.activeLayer.remove(); // delete layer added via Ctrl+J
Copy link to clipboard
Copied
var docPath = AD.path // I declared a variable to get source path
It look like you try to get the current document Path. The statment should be:
var docPath = app.activeDocument.path;
That should be in a try catch for it will faill if the document has not been saved.
You also have not added a save of the new document after the trim. Use the docPath to save the created document into the original document path.
Also generate a unique new file name you do not want to overlay old existing files.
Also the file type should be png for the layer may have a shape that has some transparency,
The script you posted is also missing the final }