What's new
Photoshop Gurus Forum

Welcome to Photoshop Gurus forum. Register a free account today to become a member! It's completely free. Once signed in, you'll enjoy an ad-free experience and be able to participate on this site by adding your own topics and posts, as well as connect with other members through your own private inbox!

Scripting Photoshop - Microsoft Excel Macro


DanEchelon

Member
Messages
12
Likes
0
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
 
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
 
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.
 
Dan, Can you give me the first two lnes of your csv file so I can see what output filename it is trying to create?
 
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.
 
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.
 
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.
 
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.
 
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");
 
Capture.PNG

On the plus side, the folders are working now, and I have to select the csv file. However I am still getting an error as seen in the image here.
 
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));
 
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!
 
Mmmm
It looks as if the output folder does not exist, please can you check.
The file name/path looks okay.
 
And you can ignore that bit now too as I just had to remove the alert(decodeURI(outFile));

Thank you very much for your help Paul.
 
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!
 
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");
};
 
#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?
 

Back
Top