user2804628
user2804628

Reputation: 143

Check if file exists with different extension?

I am starting out in photoshop with a .tif file. I run a script which adds some layers etc and then i save the file as a .psd in a new folder.

The problem i am having is checking to see if a .psd file already exists with the same name. My goal is to simply close down the .tif file without saving if a .psd with the same name appears in the folder.

Here is my save code:

//Save document
var savePath = Folder(doc.path.parent) + "/new_folder/";
saveFile = new File(savePath);
saveOptions = new PhotoshopSaveOptions;
saveOptions.embedColorProfile = true;
if ( WHAT SHOULD I BE ASKING HERE? ) {
    doc.saveAs(saveFile, saveOptions, false, Extension.LOWERCASE);
} else {
    doc.close(SaveOptions.DONOTSAVECHANGES);    
}

I'm stuck with what add to the if function? I've tried .exists but it's not working because the current file is still in .tif mode and hasn't saved to .psd yet. So it just keeps on saving and overwriting the previous saved .psd

Any help would be most welcome. :)

EDIT:

Thought i had it working with this but still no luck:

//Strip .tif and add .psd to file name
var docName = doc.name;
PSDName = docName.substr(0,docName.length-3);    
PSDName = PSDName + "psd";  


//Save document
var savePath = Folder(doc.path.parent) + "/new_folder/";
saveFile = new File(savePath);
saveOptions = new PhotoshopSaveOptions;
saveOptions.embedColorProfile = true;
var savedFile = savePath + "/" + PSDName
if (! savedFile.exists ) {
    doc.saveAs(saveFile, saveOptions, false, Extension.LOWERCASE);
} else {
    doc.close(SaveOptions.DONOTSAVECHANGES);

}

the if statement is returning false every time and the doc is not saving. If i take away the ! it saves every time.

Upvotes: 1

Views: 2554

Answers (1)

Mark Setchell
Mark Setchell

Reputation: 207758

Make a new variable with the filename that you want to test - i.e. the name of the .PSD file and use that. For example, strip off the TIF and replace it with PSD then use .exists.

var ImageName = activeDocument.name;
PSDName = ImageName.substr(0,ImageName.length-3);    // Strip "TIF" from end
PSDName = PSDName + "psd";                           // Add on "PSD" instead

If you need to debug your script, you can do something like this:

// Change Debug=1 for extra debugging messages, Debug=0 for no messages
var Debug=1;
...
if(Debug)alert(PSDName);
...
if(Debug)alert("File exists");

Upvotes: 1

Related Questions