How to make log.csv file to log 400 .jpeg names in one row (Photoshop/JavaScript)

Multi tool use


How to make log.csv file to log 400 .jpeg names in one row (Photoshop/JavaScript)
I am still new to JavaScript. But somehow managed to create following script. Usually I run this script, to batch process 400-500 images. It creates and saves 2 copies of an image in 2 different locations, while doing some specific Image Compression.
//Script should be used only in Adobe Photoshop
//This script will: 1. Check if canvas is 1:1, if not then scale to the correct ratio (3000px x 3000px)
// 2. Check whether it has ColorProfile embedded. If not then change collor profile to sRGB.
// 3. Save the resized TIFF version (100% Quality, 3000px x 3000px, sRGB, LZW compressed, Without layers) into "/Volumes/Hams Hall Workspace/Ecom_Staging/Images_Today"
// 4. Save another copy as JPEG (80% Quality, 2000px x 2000px, sRGB) "into /Volumes/Hams Hall Workspace/Ecom_Staging/Jpegs_for_Hybris"
if( BridgeTalk.appName == "photoshop" ) {
//continue executing script
}
// create the output file
// first figure out which kind of line feeds we need
if ($.os.search(/windows/i) != -1) {
fileLineFeed = "Windows"
}
else {
fileLineFeed = "Macintosh"
}
//output location
folderHybrisUpload = "/Jpegs_for_Hybris";
folderTiffSave = "/Images_Today";
folderDumpRoot = "/Volumes/Hams Hall Workspace/Ecom_Staging";
folderName = "~/Desktop/"
// Sizes
var hybrisSize=2000;
var hybrisQuality=80;
var docRef = activeDocument;
//History States
app.purge(PurgeTarget.HISTORYCACHES);
var history = docRef.historyStates.length - 1;
//Units Pixels
app.preferences.rulerUnits = Units.PIXELS;
//Make it 1:1 Ratio (Square)
if (docRef.height != docRef.width) {
docRef.changeMode(ChangeMode.RGB);
// these are our values for the END RESULT width and height (in pixels) of our image
var fWidth = 3000;
var fHeight = 3000;
// do the resizing. if height > width (portrait-mode) resize based on height. otherwise, resize based on width
if (docRef.height > docRef.width) {
docRef.resizeImage(null, UnitValue(fHeight, "px"), null, ResampleMethod.BICUBIC);
} else {
docRef.resizeImage(UnitValue(fWidth, "px"), null, null, ResampleMethod.BICUBIC);
}
// Makes background white
var white = new SolidColor();
white.rgb.hexValue = "FFFFFF";
app.backgroundColor = white;
// Resize Canvas
app.activeDocument.resizeCanvas(UnitValue(fWidth, "px"), UnitValue(fHeight, "px"));
var history = docRef.historyStates.length - 1;
}
//Correct .jpeg extension when saving for Hybris
var fileNameNoExtension = docRef.name;
fileNameNoExtension = fileNameNoExtension.split(".");
var fileExtension= fileNameNoExtension[1];
if (fileNameNoExtension.length > 1) {
fileNameNoExtension.length--;
}
fileNameNoExtension = fileNameNoExtension.join(".");
fileNameType = fileNameNoExtension.split("_");
var finalUnderScorePosition = fileNameNoExtension.lastIndexOf("_");
varFileName = "";
for (var a = 0; a < finalUnderScorePosition; a++) {
varFileName += fileNameNoExtension.charAt(a)
}
varFileType = "";
for (var a = finalUnderScorePosition + 1; a < fileNameNoExtension.length; a++) {
varFileType += fileNameNoExtension.charAt(a)
}
//Save File into Hybris Folder
app.activeDocument.resizeImage(hybrisSize, undefined, undefined, ResampleMethod.BICUBICSHARPER);
saveFile = File(folderDumpRoot + folderHybrisUpload + "/" + varFileName + "_"+varFileType+".jpg")
SaveJPEG(hybrisQuality,saveFile);
docRef.activeHistoryState = docRef.historyStates[history];
//Save copy of an "Original" into Tiff Folder (Images_Today)
app.activeDocument.save();
var saveTIFF = new TiffSaveOptions();
saveTIFF.layers = false;
saveTIFF.imageCompression = TIFFEncoding.TIFFLZW;
app.activeDocument.saveAs(new File(folderDumpRoot + folderTiffSave + "/" + docRef.name), saveTIFF);
//JPEG Compression Settings
function SaveJPEG(quality,saveFile) {
var exportOptionsSaveForWeb = new ExportOptionsSaveForWeb();
exportOptionsSaveForWeb.format = SaveDocumentType.JPEG;
exportOptionsSaveForWeb.includeProfile = false;
exportOptionsSaveForWeb.interlaced = true;
exportOptionsSaveForWeb.optimized = true;
exportOptionsSaveForWeb.includeProfile = false;
exportOptionsSaveForWeb.quality = quality;
activeDocument.exportDocument(saveFile, ExportType.SAVEFORWEB, exportOptionsSaveForWeb);
}
// Create the log file
var fileOut = new File(folderName+"time_test.txt");
// Use OS specific linefeeds
fileOut.lineFeed = fileLineFeed;
// open the file for writing
fileOut.open("w", "TEXT", "????");
// Write a line to .log file
// use commas as delimiters if you want to open the file in Excel
// use "/r" to add carriage returns
fileOut.writeln("SKU, Date");
// Write another line to .log file
// var TodaysDate = new Date();
fileOut.write(varFileName + "_"+varFileType+".jpgr");
// stop writing to the file
fileOut.close();
// Open the file in it's associated application
// .log files are associated with the "Console" on OS X
fileOut.execute();
//Close
app.activeDocument.close();
What I want it to be able to do next, is to log any image name that has been successfully saved in "/Volumes/Hams Hall Workspace/Ecom_Staging/Jpegs_for_Hybris" appear in one row and saved in .csv file on the Desktop.
I managed to create .csv log file, which would log an image name ("SKU"), into that .csv file on the desktop.
// Create the log file
var fileOut = new File(folderName+"time_test.txt");
// open the file for writing
fileOut.open("w", "TEXT", "????");
// Write a line to .log file
// use commas as delimiters if you want to open the file in Excel
// use "/r" to add carriage returns
fileOut.writeln("SKU, Date");
// Write another line to .log file
// var TodaysDate = new Date();
fileOut.write(varFileName + "_"+varFileType+".jpgr");
// stop writing to the file
fileOut.close();
// Open the file in it's associated application
// .log files are associated with the "Console" on OS X
fileOut.execute();
But the problem is that with every new image that the script processes, it simply recreates the same .csv file. And all what appears in the .csv file is one line of image name.
I've tried searching for some answers online, but the only answers were given, is how to create the basic log file. Or log file from multiple differently named Layers.
I have also tried to create some conditions, that if the Log is already created, don't create new one, but rather use the existing and simply writeln(filename) or "r"+filenamer . But it simply ignores it, and keeps creating the new file.
Any advice would be much appreciated.
Problem with the current log.csv that it shows only 1 line of image name
1 Answer
1
Replace the last part of your script (i.e. the part where you write to the text file), with the following instead:
// Create the log file
var fileOut = new File(folderName+"time_test.txt");
// Check if the file already exists.
if (!fileOut.exists) {
fileOut.open("w");
fileOut.writeln("SKU, Date");
fileOut.writeln(varFileName + "_"+varFileType+".jpg");
} else {
fileOut.open("a");
fileOut.writeln(varFileName + "_"+varFileType+".jpg");
}
// stop writing to the file
fileOut.close();
// Open the file in it's associated application
// .log files are associated with the "Console" on OS X
fileOut.execute();
Explanation
You need to firstly check whether the .txt
file already exists via the line reading:
.txt
if (!fileOut.exists) { ... }
This is simply checking whether the .txt
file does not exist.
.txt
Note: the !
at the beginning - which means if the check for the files existence returns false
.
!
false
The first time the script is run the check will return false
and the .txt
file is opened using the "w"
argument - which means write.
false
.txt
"w"
This time we write the header SKU
and Date
on the first line, and the name of the first file processed on the second line.
SKU
Date
When the script is run again the
if (!fileOut.exists) { ... }
check returns true
because the file already exists. This time the .txt
file is opened using the "a"
argument, (which means append), and the name of the file processed is written on a new line as the end of the file.
true
.txt
"a"
Notes
.txt
.txt
writeln
write
r
By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.