Search code examples
javascriptfilenamesphotoshop-script

Filename issue in Photoshop script that put multiple PNGs on mockups


I am using JS with Photoshop to put multiple PNGs on the mockup and export them as JPGs. Everything is working fine except for the exported file name. It names the file with the current date and time.

var filename = docPath + '/' + basename + "#" + getTime() + '.jpg';

I want it to name the files in a sequence like, Mockup 1, Mockup 2, Mockup 3 ... Is there any way to do it? Any help will be highly appreciated.


Solution

  • Example to gain correct Filename:

        docPath = "/myProjectFolder/img";  //set the path / maindirectory you need
        basename = "myFileprimaryName"; // set the primary Name you want to use 
    
    
        //getDate ist a function of the Date object - you need to instance it to call 
    
        var myDate = new Date(); 
        var filename = docPath + '/' + basename + "#" + myDate.getTime() + '.jpg';
    
        //Following Lines will just give you an output, to check if variables 
        //are set correct - you will not need them in final code
        const data = {
           message: filename,
         }
    
        $('#msg').html(data.message)
    

    Note: Not sure if '#' is really the best sign to use to set in Filenames, I would rather preferre to use "_" instead, due to interoperability on different filesystems.

    To save the file you will need to create a new File-Object, assign the Filename to it and save the file through the .saveAs() function, like in the following.

    Example to save File:

            var newFile = new File(filename);   
    
            var saveOptions = new JPEGSaveOptions();
            saveOptions.embedColorProfile = true;
            saveOptions.formatOptions = FormatOptions.STANDARDBASELINE;
            saveOptions.matte = MatteType.NONE;
            saveOptions.quality = quality;
    
            document.saveAs(newFile, saveOptions, true, Extension.LOWERCASE);
    

    Now if you want to generate and save multiple files, it's pretty basic for you just jave to combine the two snipplets in a for-Loop.

    Example to generate multiple files / filenames - out of one source file:

        docPath = "/myProjectFolder/img"; 
        basename = "myFileprimaryName";
         var filename;
    
        var saveOptions = new JPEGSaveOptions();
        saveOptions.embedColorProfile = true;
        saveOptions.formatOptions = FormatOptions.STANDARDBASELINE;
        saveOptions.matte = MatteType.NONE;
        saveOptions.quality = quality;
    
        var myDate = new Date(); 
    
        var i;
        for (i = 0; i < cars.length; i++) {
    
        filename = docPath + '/' + basename + +"#" + i + myDate.getTime() +'.jpg';
    
        var newFile = new File(filename);   
        document.saveAs(newFile, saveOptions, true, Extension.LOWERCASE);
        } 
    

    Note: this will create the same file with the same content multiple times (e.g. if you are a teacher who wants to handle working copies of the same file to multiple students. If you instead want to save different files, I would rather do the following: Check the Maximum Count of Basename#Count of existing files in directory, increment it and save lateron new file with incremented Count.