Skip to main content
Sheaffer
Participating Frequently
October 8, 2021
Question

Help with Photoshop action, script, or variable not sure

  • October 8, 2021
  • 4 replies
  • 6654 views

I have a lot of images that I need to add dates too. 4 images get the same date and are in the same folder. There are videos in that folder too.  Attatched images is an example of what I am trying to achieve. 

 

I'd like to be able to select every jpg in a folder add the date to it and save it back to the folder with " date" appended to the end. 

 

I tried this with actions and batch to select the folder but I'm unsure how to quickly change the date for each folder so I don't have to record the text date each time. 

 

I tried this with photoshop variables but I get stuck because I have to use the title of each file and that's just as time consuming as manually adding the date. This would be a great solution if I could tell it to grab every jpg in the folder when using it instead of naming each individaul file. 

I've not familiar enough with scripts to have tried them. 

This topic has been closed for replies.

4 replies

Legend
October 26, 2021

Could you use Batch Rename in Bridge?

Bojan Živković11378569
Community Expert
Community Expert
October 9, 2021

When using variables and data sets you can not rename files. Also when using data sets you can export or save files only in PSD file format. There are other ways to append to file name but the question is whether you want word date, actual date or both as already asked by @Stephen Marsh 

You can also count on variables to add text and some other way to append whatever you want to file name while converting PSD to desired output format.

Stephen Marsh
Community Expert
Community Expert
October 9, 2021

@Sheaffer 

 

If it was just renaming files, then there would be no need to open them into Photoshop. As you wish to add text to the image, you will indeed need to open the files and resave them in Photoshop.

 

Looking at your two sample images, this should be easy enough to script. Would it be acceptable to type the date details into a prompt once per batch, then those details would be applied to the entire batch of input JPEG images in that top-level directory/folder?

 

And just to be crystal clear, do you simply wish to only include " Date" at the end of the current filename, or the actual date string, such as " AUGUST, 2 2021" or whatever the date you enter is?

 

 

Please try the following script, saving as JPEG quality 12. As the example files appear to be from a template, I have also added the feature to remove photoshop:DocumentAncestors metadata from the processed files (which is not a bad thing in itself, however, it can bloat file sizes if there is excessive entries).

 

NOV 28 Downloading and Installing Adobe Scripts

 

/* 
https://community.adobe.com/t5/photoshop-ecosystem-discussions/help-with-photoshop-action-script-or-variable-not-sure/td-p/12439599
Help with Photoshop action, script, or variable not sure

Batch Date Stamp.jsx
v1.0
Stephen Marsh, 9th October 2021
*/

#target photoshop

if (app.documents.length === 0) {

    // The "Test if Cancel..." needs to be within a function
    (function () {

        var savedDisplayDialogs = app.displayDialogs;
        var inputFolder = Folder.selectDialog("Select the input folder containing JPEG files", "");
        // Test if Cancel button returns null, then do nothing
        if (inputFolder === null) {
            app.beep();
            return;
        }

        var inputFiles = inputFolder.getFiles(/\.(jpg|jpeg)$/i);

        // var outputFolder = inputFolder;
        var outputFolder = Folder.selectDialog("Select the output folder", inputFolder);
        // Test if Cancel button returns null, then do nothing
        if (outputFolder === null) {
            app.beep();
            return;
        }

        var dateString = prompt("Enter the date (Month, Day Year)", "MONTH, ## ####");
        // Test if Cancel button returns null, then do nothing
        if (dateString === null) {
            app.beep();
            return;
        }

        app.displayDialogs = DialogModes.NO;

        for (var i = 0; i < inputFiles.length; i++) {
            open(inputFiles[i]);
            var docName = app.activeDocument.name.replace(/\.[^\.]+$/, '');
            var saveFileJPG = new File(new File(outputFolder + '/' + docName + ' date' + '.jpg'));
            dateStamp();
            // As this appears to be created from a template
            deleteDocumentAncestorsMetadata();
            saveJPG(saveFileJPG);
            app.activeDocument.close(SaveOptions.DONOTSAVECHANGES);
        }

        app.displayDialogs = savedDisplayDialogs;

        app.beep();
        alert("Script Completed! Files saved to: " + "\r" + outputFolder.fsName);


        /************ Functions ************/

        function dateStamp() {
            var doc = app.activeDocument;
            // Ruler units
            var origRuler = app.preferences.rulerUnits;
            app.preferences.rulerUnits = Units.POINTS;

            // Doc resolution
            var origRes = app.activeDocument.resolution;
            app.activeDocument.resizeImage(null, null, 72, ResampleMethod.NONE);

            // Text layer, based on a 72ppi resolution
            var layerRef = doc.artLayers.add();
            layerRef.name = "Date Info";
            layerRef.kind = LayerKind.TEXT;
            var textRef = layerRef.textItem;
            var textColor = new SolidColor();
            textColor.rgb.red = 255;
            textColor.rgb.green = 255;
            textColor.rgb.blue = 255;
            textRef.color = textColor;
            textRef.contents = dateString;
            textRef.position = new Array(0, 0);
            textRef.font = 'Arial-Black';
            textRef.size = 50;
            textRef.useAutoLeading = true;
            textRef.justification = Justification.CENTER;

            // Ruler units for positioning on canvas
            app.preferences.rulerUnits = Units.PIXELS;
            // Align text to horizontal centre
            align2SelectAll('AdCH');
            // Align text to bottom
            align2SelectAll('AdTp');
            // move text layer down
            doc.activeLayer.translate(0, 42);

            // Restore original doc resolution
            app.activeDocument.resizeImage(null, null, origRes, ResampleMethod.NONE);
            // Restore original ruler units
            app.preferences.rulerUnits = origRuler;

            function align2SelectAll(method) {
                /*
                AdLf = Align Left
                AdRg = Align Right
                AdCH = Align Centre Horizontal
                AdTp = Align Top
                AdBt = Align Bottom
                AdCV = Align Centre Vertical
                */
                doc.selection.selectAll();
                var desc = new ActionDescriptor();
                var ref = new ActionReference();
                ref.putEnumerated(charIDToTypeID("Lyr "), charIDToTypeID("Ordn"), charIDToTypeID("Trgt"));
                desc.putReference(charIDToTypeID("null"), ref);
                desc.putEnumerated(charIDToTypeID("Usng"), charIDToTypeID("ADSt"), charIDToTypeID(method));
                try {
                    executeAction(charIDToTypeID("Algn"), desc, DialogModes.NO);
                } catch (e) { }
                doc.selection.deselect();
            }
        }

        function saveJPG(saveFileJPG) {
            var jpgOptions = new JPEGSaveOptions();
            jpgOptions.formatOptions = FormatOptions.STANDARDBASELINE;
            jpgOptions.embedColorProfile = true;
            jpgOptions.matte = MatteType.NONE;
            jpgOptions.quality = 12;
            app.activeDocument.saveAs(saveFileJPG, jpgOptions, true, Extension.LOWERCASE);
        }

        function deleteDocumentAncestorsMetadata() {
            // https://prepression.blogspot.com/2017/06/metadata-bloat-photoshopdocumentancestors.html
            // https://forums.adobe.com/message/8456985#8456985
            if (ExternalObject.AdobeXMPScript === undefined) ExternalObject.AdobeXMPScript = new ExternalObject("lib:AdobeXMPScript");
            var xmp = new XMPMeta(activeDocument.xmpMetadata.rawData);
            xmp.deleteProperty(XMPConst.NS_PHOTOSHOP, "DocumentAncestors");
            app.activeDocument.xmpMetadata.rawData = xmp.serialize();
        }

    })();

} else {
    alert('No documents should be open when running this script!');
}
JJMack
Community Expert
Community Expert
October 9, 2021

The script look good. It could be improved some by adding a drop  shadow on the text layer for white on white does not show the text. Also the text size  should probably be set relative to the width of the document so the text will fit in narrow documents. Also  the save file name  you added a suffix  ' date' not the entered date. I'm not sure which the OP wants.

JJMack
Stephen Marsh
Community Expert
Community Expert
October 9, 2021

@JJMack – thank you!

 

I personally prefer using the following for white on white;

 

layerRef.blendMode = BlendMode.DIFFERENCE;

 

I decided not to do that and some of the other things that I did in Print metadata on bottom of photo that you mention which I have done in the past, until there is some feedback.

 

I asked @Sheaffer about the ' date' suffix, that is easily changed,.. However, if you re-read the post and look at both of the attached filenames ' date' seems appropriate for now.

 

There is always room for adjustments, this is only a v1.0 script after all.

JJMack
Community Expert
Community Expert
October 8, 2021

A script can Process all jpeg files in a folder.  A script can rename files.  What date do want to add to add to the  Jpeg files image name? Today's Date. Your birth date? If its a constant date like today's date the files can just be renamed they do not even need to be opened. If you need to have duplicate file in the folder with the two file names the script can copy the jpeg file?  There is no need to encode a new jpeg generation and lose some image quality. You should learn about scripting photoshop.   You can use Photoshop Batch to add a date to an image file name also Image Processor Pro can add a Date however they would open the original  jpeg and saves as additional copy with the dated names. You would beed toe select all the jpeg files ins the bridge and uset the  bridge menu Tools>Photoshop>Batch... or Image Pocessor Pro...

JJMack
Sheaffer
SheafferAuthor
Participating Frequently
October 8, 2021

Thanks for the response I was really hoping you would help with this. I've read several of your feeback in the past!

 

I think maybe I didn't explain it well. I'm looking for a date that might not be associated with the file so i would specify what I want put on it. This is why I thought variables might work well but I got stuck with having to name every file instead of just selecting each file in the folder.

 

It almost worked with variables but i'd like to grab every jpg in a folder add a specfic text to that image, rename it with the word "date" appended to each name so I know that is the one that has been dated. So it would looks something like this. 

 

[Folder for show]  Folder contains 4 images that need the text put on it and then saved next to that one with the word "date" on the file.  So my file would be "Feature Story" and "Feature Story Date" 

JJMack
Community Expert
Community Expert
October 8, 2021

A Script can rename a file anyway you want as well as it can save a file with any file name you want. You would need to pass the parameters you want the script to use.  This is normally done using a ScriptUI Dialog.  You would be able to set the folder to process in the dialog or have a select folder button  in its the dialog you can use select the folder to process. You cans also set the Image file type or types you want to process in the folder.  You can  set Date suffix you you  added to the file names then click the dialog's run button.  The thing is you most likely do not know how to script Photoshop. That is why I suggested the image processor pro plug-in script you can download and install it from the web.   However,  its dialog does not have a way to filter the image files types to process. I will process all Image files in  a selected folder.  However, it can be used from the bridge where you would select just the Jpeg files you want to process in the Bridge's UI and then you would use Bridge's menu Tools >Photoshop>Image Processor Pro...  Photoshop will open and Photoshop will open Image Processor Pro's Dialog where you will set Image Processor Pro dialog to process the files selected in the Bridge  and have it save  Jpeg files in the same folder and use your custom Date file name suffix you set in the Image Processor dialog.  The thing is the jpeg file will be a copy there will two jpeg file with identical content in the folder with different names.  It not a rename.  If you want to add things to the jpeg You can record an action to  add what you want to add to the jpegs and have the Image processor pro include using your action when processing your selected jpeg images.

 

Tp add data set up an action toe add the dats to the jpeg. ANy the set to Image Processor Proo.. dialoy toe user it.

JJMack