Search code examples
javascriptphotoshopphotoshop-script

.remove() is not a function error in Photoshop CC 2017 ExtendScript Tool Kit


I am trying to modify original PSD and then delete the original one and only want to save as a new jpg. My code is working fine with this line:

activeDocument.close(SaveOptions.DONOTSAVECHANGES);	// Close Original Image

But when I replace above line with this line:

psd.remove();	// I want to delete Original file

It giving me remove() is not a function error.

Here is the complete script. I have tired to read Photoshop JS Guide 2015 and also google this issue but I didn't find any answer.

var defaultRulerUnits = preferences.rulerUnits; 
preferences.rulerUnits = Units.PIXELS;

if (documents.length >= 1){

var hres = 0;
var vres = 0;
var OldName = activeDocument.name.substring(0, activeDocument.name.indexOf('.'));
var CurrentFolder = activeDocument.path;
var psd = app.activeDocument;
hres = activeDocument.width;
vres = activeDocument.height;

activeDocument.selection.selectAll();

if (activeDocument.layers.length >1) {
	activeDocument.selection.copy(true);
}

else{
	if (activeDocument.layers.length =1) {
	activeDocument.selection.copy(false);
	}
}

psd.remove();	// I want to delete Original file
       
var newDoc = documents.add(hres, vres, 72, OldName, NewDocumentMode.RGB, DocumentFill.WHITE);

newDoc.paste();

jpgFile = new File(CurrentFolder + "/" + OldName+ ".jpg" );
jpgSaveOptions = new JPEGSaveOptions();
jpgSaveOptions.formatOptions = FormatOptions.STANDARDBASELINE;
jpgSaveOptions.matte = MatteType.NONE;
jpgSaveOptions.quality = 12;
newDoc.saveAs(jpgFile, jpgSaveOptions, true,   Extension.LOWERCASE);

}


Solution

  •  srcDoc.activeLayer.remove();
    

    Will delete the active layer. There's no .remove() method to delete a file.