Skip to main content
Known Participant
May 23, 2020
Answered

Script that exports a JPG with unique extension?

  • May 23, 2020
  • 1 reply
  • 3411 views

I am not very good with code, so I am asking here. I have a bunch of scripts. Most I made by pulling and altering code. Basically, I want a script so I can quickly export a JPG in one click, but it to be a unique name so no other JPGs are overwritten.

This can be done in two ways: (1) a script that checks a folder for the current name, if it exists it adds "-001" for example, if that exists "-002" and so on. The other idea (2) is just to generate a unique long number as the filename. So it would export a JPG with a 10 digit number file name "1950037482" and then the next file might be "1777490021". This random number method could overright, but the chances would be very slim.

 

Any ideas how to get this working? I have a similar script for PSD files, so if I save on in my EXPORT folder, then run the script it checks the folder and adds a 3 digit extension, 001, 002, 003 and so on so nothing is overwritten. Can't seem to understand the code enough to translate it to JPGs... Any ideas? Thanks!

This topic has been closed for replies.
Correct answer Charu Rajput

Sure, so I want the code to simply export a high quality JPG of the active document to "C:/exports/" upon running the script, and (like your first script) it would be ideal to create an extersion with date/time format.

This script would then work on anything in one click. I could create a new document, open a previous document, PSD, TIF - it shouldn't matter because it will simply export the active document to the same folder with the unique date/time filename.

(I understand this is a werid workflow, and it's not what I generally use for my photography and design, but I want this script for quick saving of random scraps and ideas, so I can waste little time and get the imagery out)


Hii,

Its not wierd work flow, it just we have such requirements. I would request to try another version of the script that have few changes as you required. And you can change the folder location where you want to export as required. I have added the comment where it needs to be get changed. I am exporting at location Desktop/Exported JPEG. Also this script uses name as [date_timestamp]. jpeg. For instance, 200523_1590251957007. jpeg. I added date in the file name just to have more clearity in the name of the files. 

 

#target photoshop
function exportToJPEG(path) {

    var dateObj = new Date();
    var year = dateObj.getFullYear().toString().substr(-2);
    var month = ("0" + (dateObj.getMonth() + 1)).slice(-2);
    var date = ("0" + dateObj.getDate()).slice(-2);
    var timeStamp = dateObj.getTime();
    var completeDate = year.toString() + month.toString() + date.toString();

    var fileName = path + "/" + completeDate + "_" + timeStamp + ".jpeg";

    var idsave = stringIDToTypeID("save");
    var desc5 = new ActionDescriptor();
    var idas = stringIDToTypeID("as");
    var desc6 = new ActionDescriptor();
    var idextendedQuality = stringIDToTypeID("extendedQuality");
    desc6.putInteger(idextendedQuality, 12);
    var idscans = stringIDToTypeID("scans");
    desc6.putInteger(idscans, 5);
    var idmatteColor = stringIDToTypeID("matteColor");
    var idmatteColor = stringIDToTypeID("matteColor");
    var idnone = stringIDToTypeID("none");
    desc6.putEnumerated(idmatteColor, idmatteColor, idnone);
    var idJPEG = stringIDToTypeID("JPEG");
    desc5.putObject(idas, idJPEG, desc6);
    var idin = stringIDToTypeID("in");
    desc5.putPath(idin, new File(fileName));
    var iddocumentID = stringIDToTypeID("documentID");
    desc5.putInteger(iddocumentID, 219);
    var idlowerCase = stringIDToTypeID("lowerCase");
    desc5.putBoolean(idlowerCase, true);
    var idsaveStage = stringIDToTypeID("saveStage");
    var idsaveStageType = stringIDToTypeID("saveStageType");
    var idsaveSucceeded = stringIDToTypeID("saveSucceeded");
    desc5.putEnumerated(idsaveStage, idsaveStageType, idsaveSucceeded);
    executeAction(idsave, desc5, DialogModes.NO);
}

function main() {
    var desktopFolder = Folder.desktop;
    var exportedFolder = Folder(desktopFolder + "/Exported JPEG"); // Change folder name and location here as you required
    if(!exportedFolder.exists){
        exportedFolder.create();
    }
   exportToJPEG(exportedFolder.fsName);
}

main();

 

I hope this version works for you as expected.

 

1 reply

Charu Rajput
Community Expert
Community Expert
May 23, 2020

Hi,

There is a small version of the script that export PSD to JPEG, and  you can altered easily. I would like you to try this once.

 

 

#target photoshop
function exportToJPEG() {
    var docName = app.activeDocument.name.split('.')[0];

    var dateObj = new Date();
    var year = dateObj.getFullYear().toString().substr(-2);
    var month = ("0" + (dateObj.getMonth() + 1)).slice(-2);
    var date = ("0" + dateObj.getDate()).slice(-2);
    var time = dateObj.getTime();
    var hours = dateObj.getHours()
    var minutes = dateObj.getMinutes();
    var seconds = dateObj.getSeconds();
    var completeTime = hours.toString() + minutes.toString() + seconds.toString();
    var completeDate = year.toString() + month.toString() + date.toString();

    var fileName = app.activeDocument.path + "/" + "MN-" + completeDate + "-" + docName + "-" + completeTime + ".jpeg";
    var idsave = stringIDToTypeID("save");
    var desc5 = new ActionDescriptor();
    var idas = stringIDToTypeID("as");
    var desc6 = new ActionDescriptor();
    var idextendedQuality = stringIDToTypeID("extendedQuality");
    desc6.putInteger(idextendedQuality, 12);
    var idscans = stringIDToTypeID("scans");
    desc6.putInteger(idscans, 5);
    var idmatteColor = stringIDToTypeID("matteColor");
    var idmatteColor = stringIDToTypeID("matteColor");
    var idnone = stringIDToTypeID("none");
    desc6.putEnumerated(idmatteColor, idmatteColor, idnone);
    var idJPEG = stringIDToTypeID("JPEG");
    desc5.putObject(idas, idJPEG, desc6);
    var idin = stringIDToTypeID("in");
    desc5.putPath(idin, new File(fileName));
    var iddocumentID = stringIDToTypeID("documentID");
    desc5.putInteger(iddocumentID, 219);
    var idlowerCase = stringIDToTypeID("lowerCase");
    desc5.putBoolean(idlowerCase, true);
    var idsaveStage = stringIDToTypeID("saveStage");
    var idsaveStageType = stringIDToTypeID("saveStageType");
    var idsaveSucceeded = stringIDToTypeID("saveSucceeded");
    desc5.putEnumerated(idsaveStage, idsaveStageType, idsaveSucceeded);
    executeAction(idsave, desc5, DialogModes.NO);
}

 

 

 

This script uses a timestamp, so everytime you try to export, it will have different timestamp, so different file name.

Let us know if this helps you.

 

Best regards
Charu Rajput
Community Expert
Community Expert
May 23, 2020

Another version of the same script. Following script will export all open PSD documents to JPEG in the same folder where your PSD file exists with the name of the PSD document adding 0001, 0002 and so on. SO for an example there are two PSD files with name TEST.psd and TEST1.psd. This script will export to jpeg files with name TEST-0001.jpeg and TEST1-0002.jpeg.

 

 

 

#target photoshop
function exportToJPEG(doc,i) {
    var docName = doc.name.split('.')[0];
    var fileName = doc.path + "/" + docName + "-000" + (i + 1) + ".jpeg";

    var idsave = stringIDToTypeID("save");
    var desc5 = new ActionDescriptor();
    var idas = stringIDToTypeID("as");
    var desc6 = new ActionDescriptor();
    var idextendedQuality = stringIDToTypeID("extendedQuality");
    desc6.putInteger(idextendedQuality, 12);
    var idscans = stringIDToTypeID("scans");
    desc6.putInteger(idscans, 5);
    var idmatteColor = stringIDToTypeID("matteColor");
    var idmatteColor = stringIDToTypeID("matteColor");
    var idnone = stringIDToTypeID("none");
    desc6.putEnumerated(idmatteColor, idmatteColor, idnone);
    var idJPEG = stringIDToTypeID("JPEG");
    desc5.putObject(idas, idJPEG, desc6);
    var idin = stringIDToTypeID("in");
    desc5.putPath(idin, new File(fileName));
    var iddocumentID = stringIDToTypeID("documentID");
    desc5.putInteger(iddocumentID, 219);
    var idlowerCase = stringIDToTypeID("lowerCase");
    desc5.putBoolean(idlowerCase, true);
    var idsaveStage = stringIDToTypeID("saveStage");
    var idsaveStageType = stringIDToTypeID("saveStageType");
    var idsaveSucceeded = stringIDToTypeID("saveSucceeded");
    desc5.putEnumerated(idsaveStage, idsaveStageType, idsaveSucceeded);
    executeAction(idsave, desc5, DialogModes.NO);
}

function main() {
    for (var i = 0; i < app.documents.length; i++) {
        exportToJPEG(app.documents[i], i);
    }
}

main();

 

 

 

I hope this helps you

 

Best regards