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
ShaneMcGAuthor
Known Participant
May 23, 2020

Interesting. If I understand correctly, this script would export a JPG using an exact timestamp. That is really smart, did not even think of that solution.

 

I tossed this code in a JS and ran it in PS but I don't see that anything happened. I opened a PSD and ran it, also tried on a JPG and a completely new document. Is there anything I should be adding to the code? And can I make the export file path the same no matter what?

Charu Rajput
Community Expert
Community Expert
May 23, 2020

I tested the first and second script. The second one does work, as long as I save a PSD first, it exports the same filename with "-001" extension JPG. The first script I cannot seem to get to do anything. That would be the best code, a filename that is the date/time so it is unique.

This code is on the right track. I just need to find ways to modify it to do what I need. What would be ideal, what I am mostly looking for, is a similar script that would export a JPG without needing a PSD to be saved. So I can just open a file or paste a new document, then run a script that would export to c:/export (for example) as date/time.JPG. One click, no need to save or anything.


Ok, Let first clear the work flow. Here is you want.

 

1. Open a file.

2. You do some stuff inside that

3. Now, you want to export into JPEG

 

My questions:

  • Will you be exporting one document at atime or all document at one time. 
  • At what location you want to save the JPEG ? May be your Desktop/Exported JPEG

 

Let me know is this workflow is correct. So that I can help you.

 

Best regards