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!');
}
Sheaffer
SheafferAuthor
Participating Frequently
October 10, 2021

Typing the date detail into a prompt would be perfect! Just the word date so I can distinguish between ones that have dates and don't as they will be uploaded in different locations. If adding the date is easier that would be acceptable as well. Thank you for your help

 

 

Thanks appreciate this and can't wait to try it tommorrow. You said below you decided not to do somethings until you got feedback. I'm fine with stripping meta data and I do want to add a dropshadow on the text or something that will look good no matter what the background looks like. 

 

 

Also if it helps. The font I'm using is Graphik - Bold at 2pt. And all the images are 1in x 0.563 and the resolution 1920. 

Sheaffer
SheafferAuthor
Participating Frequently
October 12, 2021

Here is a function to add a layer style in your style's palette to a layer use it after the text layer is added in his script to add the style you want.

Add the function to the end of the script and add a javascript statement  

addStyle("MyLayerStyle")

after the text layet is added in his script replace MyLayerStyle with the style name you want added the " are required.

 

 

 

function addStyle(Style){
	var idASty = charIDToTypeID( "ASty" );
	    var desc20 = new ActionDescriptor();
	    var idnull = charIDToTypeID( "null" );
	        var ref3 = new ActionReference();
	        var idStyl = charIDToTypeID( "Styl" );
	        ref3.putName( idStyl, Style );
	    desc20.putReference( idnull, ref3 );
	    var idT = charIDToTypeID( "T   " );
	        var ref4 = new ActionReference();
	        var idLyr = charIDToTypeID( "Lyr " );
	        var idOrdn = charIDToTypeID( "Ordn" );
	        var idTrgt = charIDToTypeID( "Trgt" );
	        ref4.putEnumerated( idLyr, idOrdn, idTrgt );
	    desc20.putReference( idT, ref4 );
	try{
		executeAction( idASty, desc20, DialogModes.NO);
	}catch(e){}
}

 

 

 

.


Thanks You!

 

I realized it wasn't loading my font at all when I messed with this. Does the font have to be ttf? and not otf to recognize the name? The name in font book is "Graphik" 

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