Scripting Photoshop - Microsoft Excel Macro

#1
Hey Guys,

I am new here so I apologise if I am posting this in the wrong place.

Here is what we are looking to do:

- Open a file
- Duplicate the layer
- Resize to place the layers next to each other, the same size
- Save the file as a new name
- Repeat the process

We have already set up the "Duplicate and Resize" part, but we do not know how to do the other parts.

What we ideally want is an Excel file with the file name is Column A, and the new name which we would like to save the new image as in Column B.

Is something like this possible?

Regards,

Dan
 

Paul MR

The Script Master
#3
You can use a script, here is an example that would need amending to suit your needs.

Example CSV file:-
Code:
filename,newfilename
P1000670.JPG,O1020270
P1000672.JPG,O1020267
P1000678.JPG,O1020278
P1000680.JPG,O1020234
P1000681.JPG,O1020212
N.B. no extension on new file name as this is added in the script.

Example javaScript
Code:
#target photoshop;
app.bringToFront();
main();
function main(){
var inputFolder = Folder.selectDialog("Please select the folder with Files to process");
if(inputFolder == null) return;
var outputFolder = Folder.selectDialog("Please select output folder");
if(outputFolder == null) return;
var csv = File.openDialog("Please select CSV file.","CSV File:*.csv"); 
if(csv == null) return;
csv.open("r");
csv.encoding = "UTF-8";
var fileList = csv.read().split("\n");
csv.close();
//start at 1 to exclude header line. 0 if no header line
for(var a = 1;a<fileList.length;a++){
    if(fileList[a].length < 3) continue;
    var line = fileList[a].split(",");
    var file = File(inputFolder + "/" + line[0].toString());
    if(!file.exists) continue;
    app.open(file);
    //amend to suit.
    doAction("ActionName","ActionSet");
    var outFile = File(outputFolder + "/" + line[1].toString() + ".jpg");
    SaveJPEG(outFile, 8);
    app.activeDocument.close(SaveOptions.DONOTSAVECHANGES);
    }
};
function SaveJPEG(saveFile, jpegQuality){
jpgSaveOptions = new JPEGSaveOptions();
jpgSaveOptions.embedColorProfile = true;
jpgSaveOptions.formatOptions = FormatOptions.STANDARDBASELINE;
jpgSaveOptions.matte = MatteType.NONE;
jpgSaveOptions.quality = jpegQuality;
activeDocument.saveAs(saveFile, jpgSaveOptions, true,Extension.LOWERCASE);
};
N.B. the line doAction("ActionName","ActionSet"); needs to be amended to suit your action name and actionset name!
In this example the files are saved as jpegs
 
#4
Thank you for your reply Paul, that very nearly works for me, but I have two problems.

1) Even though I have entered the folder locations, it still asks me in Photoshop to select the folder. I am entering the folder location as such:

var inputFolder = Folder.selectDialog("R:\Marketing\Images\Batch Done")

2) The final line of the script is not working - activeDocument.saveAs(saveFile, jpgSaveOptions, true,Extension.LOWERCASE); I am getting an Error 1302 No Such Element. Do you have any idea why this is?

Regards,

Dan.
 
#6
Sure....

filename,newfilename
BBA1860.jpg,TEST1

That is how I think it was supposed to be based on what you said at the beginning of your message.

The file is opening, the batch is running, it just does not want to save it.
 

Paul MR

The Script Master
#7
Have you amended the output folder as well?
The input folder should be:
var inputFolder = Folder("/R/Marketing/Images/Batch Done");
It would not have found the files to open.
 
#8
var inputFolder = Folder.selectDialog("R:\Marketing\Images\Batch Done");
if(inputFolder == null) return;
var outputFolder = Folder.selectDialog("R:\Inventory\Test");
if(outputFolder == null) return;
var csv = File.openDialog("R:\Inventory\Test\KitRename","CSV File:*.csv");

That is what I have at the top of the script.

When I run it in Photoshop (File > Scripts > Browse), it opens a "Choose a Folder" box to select the folder for Batch Done, then a "Choose a Folder" box to select the folder for Test, then a "Select a File" box to choose the csv. It then runs the Photoshop action that I have entered (as I can see it resizing and stuff), but then comes up with the error as it goes to save it.
 
#9
Paul MR

Just to confirm your script...

Your script runs fine for me when run with the OP's csv data, although I had to take an educated guess at what the action does.

That shouldn't make any difference to saving the file anyway but just to be clear.

I also tried setting the output folder to 'Read Only' but it still worked flawlessly.

I'm not sure why the OP is changing the folder dialog Titles as there is no need but it too shouldn't make any difference....maybe they should put it back as you wrote it?

Not trying to muscle in on your excellent script....just trying to help in letting you know that for me...it works 100%.

Regards.
MrToM.
 

Paul MR

The Script Master
#10
Please change those lines to:
Code:
var inputFolder = Folder("/R/Marketing/Images/Batch Done");
if(!inputFolder.exists) {
    alert("Input folder does not exist");
    return;
    }
var outputFolder = Folder("/R/Inventory/Test");
if(!outputFolder.exists) outputFolder.create();
var csv = File.openDialog("Please select CSV file.","CSV File:*.csv");
 

Paul MR

The Script Master
#12
It doesn't like the format of the output file path/name.
Please add the alert line after this line, to see what file it is trying to save.
var outFile = File(outputFolder + "/" + line[1].toString() + ".jpg");
alert(decodeURI(outFile));
 
#14
Right, ignore that second image there, the Error 1302. We had not deleted the part of the action that saved and closed.

The final thing that we need to fix is that Script Alert box that appears. We could do without having to click on "Ok" after each image has been processed. Could be up to 500 times or something!
 
#18
Ok just one more quick question seeing as you have been so awesome so far......

Is there anything that we can add to the end of this script so that once every image in the csv file has been processed, to run another Action that we have in PS.

We have another Action that will resize everything in the location folder into 8 different sizes and save those into 8 different folders.

If there is no way of doing this then we will just have to wait until the script has finished and then run the action after manually. Would just be handier if it all ran at once!
 

Paul MR

The Script Master
#19
You can add another doAction after the curly bracket as all files will have been processed.

Code:
    app.activeDocument.close(SaveOptions.DONOTSAVECHANGES);
    }
    doAction("ActionName","ActionSet");
};
 
#20
#target photoshop;
app.bringToFront();
main();
function main(){
var inputFolder = Folder("/R/Marketing/Images/Batch Done");
if(!inputFolder.exists) {
alert("Input folder does not exist");
return;
}
var outputFolder = Folder("/R/Marketing/Images/Batch To Go");
if(!outputFolder.exists) outputFolder.create();
var csv = File.openDialog("/R/Inventory/Test","CSV File:*.csv");
if(csv == null) return;
csv.open("r");
csv.encoding = "UTF-8";
var fileList = csv.read().split("\n");
csv.close();
//start at 1 to exclude header line. 0 if no header line
for(var a = 1;a<fileList.length;a++){
if(fileList[a].length < 3) continue;
var line = fileList[a].split(",");
var file = File(inputFolder + "/" + line[0].toString());
if(!file.exists) continue;
app.open(file);
//amend to suit.
doAction("Whole","Pair_Kit_Batch");
var outFile = File(outputFolder + "/" + line[1].toString() + ".jpg");
SaveJPEG(outFile, 8);
app.activeDocument.close(SaveOptions.DONOTSAVECHANGES);
}
doAction("batch_to_go","DestinyMacrosRDrive201703161145");
};
function SaveJPEG(saveFile, jpegQuality){
jpgSaveOptions = new JPEGSaveOptions();
jpgSaveOptions.embedColorProfile = true;
jpgSaveOptions.formatOptions = FormatOptions.STANDARDBASELINE;
jpgSaveOptions.matte = MatteType.NONE;
jpgSaveOptions.quality = jpegQuality;
activeDocument.saveAs(saveFile, jpgSaveOptions, true, Extension.LOWERCASE);
};



So it should be like that?