SaveAs script not working after update

Anyone, especially newbies, asking for help with Photoshop Scripting and Photoshop Automation - as opposed to those contributing to discussion about an aspect of Photoshop Scripting

Moderators: Kukurykus, Tom, xbytor

jenirohwer
Posts: 2
Joined: Thu Nov 18, 2021 7:13 am

SaveAs script not working after update

Post by jenirohwer »

Hi everyone!

I use a SaveAs script (written by someone who I no longer work with), to save out images for work.

It should save a psd in a certain folder, saving over the psd of the same name that is already in that folder, then save a png in a separate folder, also saving over a png that is already in that folder.  I have been on Catalina and it has been working perfectly

Since updating to Monterey, the script is getting stuck at line 6 (doc.saveAs(saveFile, saveOptions);) and then failing. 

I think the issue lies somewhere in the SaveAs (and perhaps the changes around SaveAs and Save As Copy?), because if I delete the image in the folder it's saving to, it works, but if it needs to save over that image, it doesn't work.

I have a pretty limited knowledge of scripting so any help would be greatly appreciated!! Script below:

globals = {};
function savePSD(doc, saveFile){

var saveOptions = new PhotoshopSaveOptions();
saveOptions.embedColorProfile = true;
doc.saveAs(saveFile, saveOptions);
}

function savePNG(doc, saveFile){
app.preferences.rulerUnits = Units.PERCENT;
doc.resizeImage(40, undefined, undefined, ResampleMethod.BICUBICSHARPER);
doc.changeMode(ChangeMode.RGB)
pngSaveOptions = new PNGSaveOptions();
var newName = doc.name;
newName = newName.slice(0,-4);
newName = saveFile+"/" + newName+".png";
var newFile = File(newName);
doc.saveAs(newFile, pngSaveOptions, true, Extension.LOWERCASE);
doc.close(SaveOptions.DONOTSAVECHANGES);
// doc.exportDocument(File(newName),ExportType.SAVEFORWEB,options);

var path = newFile;
//var z = File(path);
var docRef = open(path);
doAction('BoundingCrop','PDBuilder');


}



function imageExistsValidation(saveFile) {
var imageName = File(saveFile);
if (imageName.exists) {
alert ("Save complete. Closing file");
}
else {
alert("Save validation failed. File did not save correctly, please save manually")
}
}
function checkTime(saveTime, openTime) {
if (openTime>saveTime){
alert("save is invalid. Please save manually");
}
}
function FolderSearch(fileLocation, searchKey){
var inFolder = new Folder(fileLocation)
if(inFolder != null){
var regex = searchKey;
var fileList = inFolder.getFiles();
}
for(var a = 0 ;a < fileList.length; a++)
{
if (fileList[a] instanceof File)
{
var fileName = String(fileList[a]);
if (fileName.match(/.(jpg|tif|psd|bmp|gif|png)$/))
{
var searchName = fileName.search(searchKey);
if (searchName > -1){
globals["imageExists"] = fileLocation;
return;
}
else {
globals["imageExists"] = null;
}
}
}
}

}

function filePathValidation(sFolderPath, a){
var folderValidation = Folder(sFolderPath);
if (folderValidation.exists) {
globals["folderValidationCheck"] = true;
}
else {
globals["folderValidationCheck"] = false;
}
}
function main (){
var doc = app.activeDocument;
var docName = String(doc.name);
var docPath_Working = doc.path;
var docPath_Working = String(docPath_Working);
var docPath_YearIndex = docPath_Working.search("WEEK_");
var docPathWeek = docPath_Working.slice(docPath_YearIndex+5, docPath_YearIndex+7);
var docPathYear = docPath_Working.slice(docPath_YearIndex+8, docPath_YearIndex+12);
var docPathYearShort = docPathYear.slice(2);
var openTime = app.activeDocument.path.modified;
var specialBuysSearchString = (docPathYearShort+ "_" + docPathWeek+ "_")
var docNameSpecialBuysSearch = docName.search(specialBuysSearchString);
var CMYKSBfileLocation = "/studio/CLIENTS/ALDI/ WEEKLY/WEEK_" + docPathWeek+"_20"+docPathYearShort+"/W" +docPathWeek+ "_LIVE_IMAGES/01_Special Buys Catalogue 280 Spec";
var CMYKCRfileLocation = "/studio/CLIENTS/ALDI/ WEEKLY/WEEK_" + docPathWeek+"_20"+docPathYearShort+"/W" +docPathWeek+ "_LIVE_IMAGES/04_Core Range";
var CMYKRepeatfileLocation = "/studio/CLIENTS/ALDI/ WEEKLY/WEEK_" + docPathWeek+"_20"+docPathYearShort+"/W" +docPathWeek+ "_LIVE_IMAGES/05_Repeats";
var PNGSBfileLocation = "/studio/CLIENTS/ALDI/ WEEKLY/WEEK_" + docPathWeek+"_20"+docPathYearShort+"/W" +docPathWeek+ "_LIVE_IMAGES/08_PD_1X1/SpecialBuys";
var PNGCRfileLocation = "/studio/CLIENTS/ALDI/ WEEKLY/WEEK_" + docPathWeek+"_20"+docPathYearShort+"/W" +docPathWeek+ "_LIVE_IMAGES/08_PD_1X1/CoreRange";
var PNGRepeatfileLocation = "/studio/CLIENTS/ALDI/ WEEKLY/WEEK_" + docPathWeek+"_20"+docPathYearShort+"/W" +docPathWeek+ "_LIVE_IMAGES/08_PD_1X1/Repeat";

var uploadLocation = "/~/Desktop/Upload";
var saveLocation = "~/Desktop/Upload";
filePathValidation(uploadLocation);
if (globals.folderValidationCheck == false) {
var uploadFolder = Folder(uploadLocation);
uploadFolder.create();
}


var oCurrentDate = new Date();
var CurrYear = oCurrentDate.getFullYear(); // get 4 digit year number
var CurrMonth = oCurrentDate.getMonth(); // get current month number
var CurrDay = oCurrentDate.getDate(); // get current date number

CurrMonth = CurrMonth + 1;

var uploadDate = uploadLocation +"/Upload Date "+CurrYear+"_"+CurrMonth+"_"+CurrDay;
filePathValidation(uploadDate);
if (globals.folderValidationCheck == false) {
var uploadDateFolder = Folder(uploadDate);
uploadDateFolder.create();
}



var uploadWeek = uploadDate + "/Week "+docPathWeek;
filePathValidation(uploadWeek);
if (globals.folderValidationCheck == false) {
var uploadWeekFolder = Folder(uploadWeek);
uploadWeekFolder.create();
}


var uploadCR = uploadWeek + "/Core Range";
filePathValidation(uploadCR);
if (globals.folderValidationCheck == false) {
var uploadCRFolder = Folder(uploadCR);
uploadCRFolder.create();
uploadCRFile = saveLocation +"/Upload Date "+CurrYear+"_"+CurrMonth+"_"+CurrDay+ "/Week "+docPathWeek+"/Core Range"
}

var uploadSB = uploadWeek + "/Special Buys";
filePathValidation(uploadSB);
if (globals.folderValidationCheck == false) {
var uploadSBFolder = Folder(uploadSB);
uploadSBFolder.create();
uploadSBFile = saveLocation +"/Upload Date "+CurrYear+"_"+CurrMonth+"_"+CurrDay+ "/Week "+docPathWeek+"/Special Buys"
}

var uploadRepeat = uploadWeek+ "/Repeats";
filePathValidation(uploadRepeat);
if (globals.folderValidationCheck == false) {
var uploadRepeatFolder = Folder(uploadRepeat);
uploadRepeatFolder.create();
uploadRepeatFile = saveLocation +"/Upload Date "+CurrYear+"_"+CurrMonth+"_"+CurrDay+ "/Week "+docPathWeek+"/Repeats"
}



if(docNameSpecialBuysSearch > -1){
savePSD(doc, new File(CMYKSBfileLocation))
savePSD(doc, new File(uploadSBFile))
//checkTime(app.activeDocument.path.modified, openTime);
imageExistsValidation(CMYKSBfileLocation+"/"+docName);
savePNG(doc, new File(PNGSBfileLocation));
// doc.close();
return;
}
if (docNameSpecialBuysSearch == -1) {
FolderSearch(CMYKCRfileLocation, docName);
if (globals["imageExists"] == null) {
FolderSearch(CMYKSBfileLocation, docName);
}
if (globals["imageExists"] == null) {
FolderSearch(CMYKRepeatfileLocation, docName);
}
if(globals["imageExists"] == CMYKCRfileLocation) {
savePSD(doc, new File(CMYKCRfileLocation))
savePSD(doc, new File(uploadCRFile))
//checkTime(app.activeDocument.path.modified, openTime);
imageExistsValidation(CMYKCRfileLocation+"/"+docName);
savePNG(doc, new File(PNGCRfileLocation));
// doc.close();
return;
}
else if (globals["imageExists"] == CMYKSBfileLocation) {
savePSD(doc, new File(CMYKSBfileLocation))
savePSD(doc, new File(uploadSBFile))
//checkTime(app.activeDocument.path.modified, openTime);
imageExistsValidation(CMYKSBfileLocation+"/"+docName);
savePNG(doc, new File(PNGSBfileLocation));
// doc.close();
return;
}
else if (globals["imageExists"] == CMYKRepeatfileLocation) {
savePSD(doc, new File(CMYKRepeatfileLocation))
savePSD(doc, new File(uploadRepeatFile))
//checkTime(app.activeDocument.path.modified, openTime);
imageExistsValidation(CMYKRepeatfileLocation+"/"+docName);
savePNG(doc, new File(PNGRepeatfileLocation));
// doc.close();
return;
}
else if (globals["imageExists"] == null) {
alert("Not sure what the image is")
var w = new Window ("dialog");
w.alignChildren = "left";
var buttonGroup01=w.add("group");
var SaveLocationButtonGroup = w.add ("group");
SaveLocationButtonGroup.orientation = "column";
SaveLocationButtonGroup.alignment = "left";
var SBSave = SaveLocationButtonGroup.add ("radiobutton", undefined, "Special Buys");
var CRSave = SaveLocationButtonGroup.add ("radiobutton", undefined, "Core Range");
var RepeatSave = SaveLocationButtonGroup.add ("radiobutton", undefined, "Repeat");
buttonGroup01.add ("button", undefined, "OK");
SBSave.value = true;
w.center();
w.show()
if (SBSave.value == true){
savePSD(doc, new File(CMYKSBfileLocation))
savePSD(doc, new File(uploadSBFile))
//checkTime(app.activeDocument.path.modified, openTime);
imageExistsValidation(CMYKSBfileLocation+"/"+docName);
savePNG(doc, new File(PNGSBfileLocation));
// doc.close();
}
if (CRSave.value == true){
savePSD(doc, new File(CMYKCRfileLocation))
savePSD(doc, new File(uploadCRFile))
//checkTime(app.activeDocument.path.modified, openTime);
imageExistsValidation(CMYKCRfileLocation+"/"+docName);
savePNG(doc, new File(PNGCRfileLocation));
// doc.close();
}
if (RepeatSave.value == true){
savePSD(doc, new File(CMYKRepeatfileLocation))
savePSD(doc, new File(uploadRepeatFile))
//checkTime(app.activeDocument.path.modified, openTime);
imageExistsValidation(CMYKRepeatfileLocation+"/"+docName);
savePNG(doc, new File(PNGRepeatfileLocation));
// doc.close();
}
}
return;
}
}
main();
Last edited by jenirohwer on Fri Nov 19, 2021 12:20 am, edited 1 time in total.

User avatar
Kukurykus
Posts: 432
Joined: Mon Jul 25, 2016 12:36 pm

Re: SaveAs script not working after update

Post by Kukurykus »

I tried on Windows 10:

Code: Select all

activeDocument.saveAs(File(Folder.desktop + '/fle'))
And it worked while (re)saving the file on desktop, both for ticking on/off last 2 'File Saving Options' checkboxes of File Handling in Preferences.

jenirohwer
Posts: 2
Joined: Thu Nov 18, 2021 7:13 am

Re: SaveAs script not working after update

Post by jenirohwer »

Thanks for your help! We got it working in the meantime :)

User avatar
Kukurykus
Posts: 432
Joined: Mon Jul 25, 2016 12:36 pm

Re: SaveAs script not working after update

Post by Kukurykus »

Could you share what exactly you did to make it works?