Simple script which gives current date to a specific text layer.

Participant ,
May 28, 2021 May 28, 2021

Copy link to clipboard

Copied

Is there any simple script which gives current date to a specific text layer every time I open this project?
Thank you for your time!!!

TOPICS
Actions and scripting, How to, Windows

Views

1.6K

Likes

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines

correct answers 1 Correct Answer

Adobe Community Professional , May 28, 2021 May 28, 2021
evnt = 'layersFiltered'; if (name.split('Photoshop').length - 1 && !$.getenv('not')) $.setenv('not', 1), notifiers.add(evnt, File($.fileName)) else if(typeIDToStringID(arguments[1]) == evnt && (aD = activeDocument).name == 'Project.psd' && (tI = aD.artLayers.getByName('Date').textItem) .contents != $.getenv('Date')) $.setenv('Date', tI.contents = new Date().toLocaleString()) Save above script to: C:\Program Files (x86)\Common Files\Adobe\Startup Scripts CC\Adobe Photoshop Then relaunch Ph...

Likes

Translate

Translate
Adobe Community Professional ,
May 28, 2021 May 28, 2021

Copy link to clipboard

Copied

»a specific text layer«

How is the Layer identified? 

Likes

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Participant ,
May 28, 2021 May 28, 2021

Copy link to clipboard

Copied

By it's name, "Date"!!! If you mean that.

Likes

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Adobe Community Professional ,
May 28, 2021 May 28, 2021

Copy link to clipboard

Copied

Beg your pardon, let me see if I understand: you are looking for a script that automatically updates the name of a text layer, showing the current date or a specific date every time you open the file in photoshop? This script would allow you to automate name to date only for text layers?

 

May i ask what are the benefits of this solution and why text layers only?

 

Given that I really understood your request.

Likes

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Participant ,
May 28, 2021 May 28, 2021

Copy link to clipboard

Copied

I am looking for a script which will update the text of a text layer, not the name of it. For example, let's say I have a text layer into my project with the name "Date" and it's text is "28/05/2021". What I want to do with this script is to update this text to current date automatically or by click on a button, no problem. Just to avoid looking for this text layer and doing it by the hand.

Likes

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Adobe Community Professional ,
May 28, 2021 May 28, 2021

Copy link to clipboard

Copied

 

evnt = 'layersFiltered'; if (name.split('Photoshop').length - 1 && !$.getenv('not'))
	$.setenv('not', 1), notifiers.add(evnt, File($.fileName)) else if(typeIDToStringID(arguments[1]) == evnt
&& (aD = activeDocument).name == 'Project.psd' && (tI = aD.artLayers.getByName('Date').textItem)
.contents != $.getenv('Date')) $.setenv('Date', tI.contents = new Date().toLocaleString())

 

Save above script to: C:\Program Files (x86)\Common Files\Adobe\Startup Scripts CC\Adobe Photoshop

 

Then relaunch Photoshop, and remember it works only for document named 'Project.psd' until you change its name in the code. The text layer named 'Date' should not be in any layer set, but directly in document at any position. btw I used date with time to let script detect your project not once a day, but any time of any day.

Likes

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Adobe Community Professional ,
May 28, 2021 May 28, 2021

Copy link to clipboard

Copied

This script will be called every time when calling any script, including internal scripts such as ruler -> "straighten button". Why is this necessary?

Likes

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Adobe Community Professional ,
May 29, 2021 May 29, 2021

Copy link to clipboard

Copied

Clicking 'straighten button' does not produce 'layersFiltered' event for me, however that event may occur by doing other stuff of course, but it must fulfil given condition, and once changed date does not change until you relunch Photoshop.

Likes

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Adobe Community Professional ,
May 29, 2021 May 29, 2021

Copy link to clipboard

Copied

I was talking about the script above and not the event.

Likes

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Adobe Community Professional ,
May 29, 2021 May 29, 2021

Copy link to clipboard

Copied

I'm sorry, now I'm confused. I completely don't know what you ask for then.

 

Maybe you mean script will be triggered any time you do something in photoshop (or anything that produces whatever event?). I checked that and it does not work like it. The StartupScripts are loaded only once, at time of launching Photoshop. Later they are inactive until you make they can be called. Or there is something you discovered you'd like to share?

Likes

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Adobe Community Professional ,
May 29, 2021 May 29, 2021

Copy link to clipboard

Copied

quote The StartupScripts are loaded only once, at time of launching Photoshop.

 

All StartupScripts are executed each time before any script is called. You can check by inserting an alert there.

Likes

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Adobe Community Professional ,
May 29, 2021 May 29, 2021

Copy link to clipboard

Copied

Ah right, that's true, I noticed it by opening Scripts Event Manager. But to avoid consequences of unedeed actions there're events that let you filter how exactly you want to use startupscript.

Likes

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Adobe Community Professional ,
May 29, 2021 May 29, 2021

Copy link to clipboard

Copied

I didn't think about it.

It seems to me that the author just wanted a simple script (not necessarily automatic) that did the job for him.

Likes

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Adobe Community Professional ,
May 29, 2021 May 29, 2021

Copy link to clipboard

Copied

LATEST

It seems he wants a script that let him to forget of doing the same thing manually each time he starts project. Later he precised he wants it happens automatically or if not possible, by triggering it, so to be half-automatic. He marked my code as correct. Who unmarked it? 😕

Likes

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Adobe Community Professional ,
May 28, 2021 May 28, 2021

Copy link to clipboard

Copied

 

This is what I came up with from hacking away at some other code:

 

/*

https://community.adobe.com/t5/photoshop/simple-script-which-gives-current-date-to-a-specific-text-layer/td-p/12073216
Simple script which gives current date to a specific text layer.

Update Date Layer with Todays Date Time.jsx
Stephen Marsh, 30th May 2021 - v1.3

Script should be run on the open document event via Script Events Manager and relies on the following two conditions:
   * Document name to update (case sensitive): Project
   * Text layer name to update should be: Date

*/

#target photoshop

/* if (app.activeDocument.name.match(/^Project\.[psd|psb|tif|tiff|pdf]/) !== null) { */
if (app.activeDocument.name.match(/^Project\b/) !== null) {

    /* Time stuff */
    var myDate = new Date();
    myDate.toLocaleString('en-GB');
    var myDateString = myDate.toString();
    var time = myDateString.replace(/(.+\d{4} )(.+)( .+)/, '$2');

    /* Date stuff based on:
    https://www.w3resource.com/javascript-exercises/javascript-basic-exercise-3.php
    */
    var todaysDateTime = new Date();
    var dd = todaysDateTime.getDate();
    var mm = todaysDateTime.getMonth() + 1;
    var yyyy = todaysDateTime.getFullYear();
    if (dd < 10) {
        dd = '0' + dd;
    }
    if (mm < 10) {
        mm = '0' + mm;
    }
    var todaysDateTime = dd + '/' + mm + '/' + yyyy + ' - ' + time;

    /* Main code based on:
    https://community.adobe.com/t5/Photoshop/Script-to-Copy-Active-Layer-name-to-a-quot-Label-quot-text-layer/m-p/10659934#M268362
    Script to Copy Active Layer name to a "Label" text layer and filter out numbers, hyphens and convert word space to a carriage return
    */
    var myDoc = app.activeDocument;
    var myRulers = app.preferences.rulerUnits;
    app.preferences.rulerUnits = Units.PIXELS;

    try {
        var myLayerText = myDoc.layers.getByName('Date');
        var myText = myLayerText.textItem;

    } catch (e) {
        var myLayerText = myDoc.artLayers.add();
        myLayerText.kind = LayerKind.TEXT;
        var myText = myLayerText.textItem;
        myColor = new SolidColor;
        myColor.rgb.red = 255;
        myColor.rgb.green = 0;
        myColor.rgb.blue = 0;
        myLayerText.textItem.color = myColor;
        myText.position = [10, 30]; // Upper Left
        myText.justification = Justification.LEFT;
        myText.font = 'Courier';
        myText.size = 30;
        myLayerText.name = 'Date';

    } finally {
        myText.contents = todaysDateTime;
    }

    app.preferences.rulerUnits = myRulers;
}

else {
    // Do nothing if active document is not named "Project" (case sensitive)
}

 

 

https://prepression.blogspot.com/2017/11/downloading-and-installing-adobe-scripts.html

 

EDIT 29th May 2021: v1.2 – I like the suggestion of adding a timestamp from Kukurykus, so I have edited/updated the original code.

 

EDIT 30th May 2021: v1.3 – I have now added a conditional check that the filename is "Project" (without the quotes, case sensitive) in addition to the previous requirement that the layer name is "Date" (again, borrowing the idea from Kukurykus).

 

Likes

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Adobe Community Professional ,
May 28, 2021 May 28, 2021

Copy link to clipboard

Copied

Sorry, I forgot to mention that I would use File > Scripts > Scripts Events Manager... to automatically trigger this script to run on the open document event. I would probably use a "safer" unique layer name than the generic "Date" though, just in case this was accidentally triggered by a file that contained the layer name "Date" that was not meant to be updated.

 

EDIT 30th May 2021: I have added a second conditional safety check so that both the filename and layer name have to be matched before the script executes.

Likes

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines