skip to Main Content

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);

}

2

Answers


  1.  srcDoc.activeLayer.remove();
    

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

    Login or Signup to reply.
  2. A little late to the thread, but hope this helps someone else.

    The remove method expects a file location. You can accomplish removing the original file like this:

    var activeDoc = app.activeDocument;
    var docPath = new Folder(activeDoc.path);
    var psd = new File(docPath + "/" + activeDoc.name);
    ...
    psd.remove();
    

    Edit: Meant to also include a link to this handy ESTK reference doc where I learned about working with the File object: http://estk.aenhancers.com/3%20-%20File%20System%20Access/file-object.html?highlight=delete

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search