Skip to main content
Participant
January 12, 2006
Answered

Insert auto date

  • January 12, 2006
  • 23 replies
  • 6599 views
Hi

First, sorry if you've also read the InDesign Forum, I've cross posted this enquiry there as well.

I need to insert a date/time when the IDCS2 document is printed, i've looked on the web and various other resources like this Forum.

Peter

Set to not expire because of active cross references elsewhere
This topic has been closed for replies.
Correct answer _Dave_Saunders_
Inserting and later updating a date is easy.

Triggering it to a specific event is less easy. RogueSheep has a product that allows you to tie a script to an event. So, if you wrote a script to replace all instances of a character style named (say) CurrentDate with the current date and tied this to happen before a Print command was actually executed. That would do it.

A JavaScript to do this would look something like this:
//DESCRIPTION: Update Date in Active Document


/*
Updates all instances of current date in a document to reflect the
actual date when the script is run. Depends on character style named
"CurrentDate" -- does nothing if document lacks said style.
*/

if (app.documents.length == 0) { exit() }
myDoc = app.activeDocument; //Global
var myStyle = myDoc.characterStyles.item("CurrentDate");
if (myStyle == null) { exit () }

// If we get here, we have work to do
var myDate = getDateString();

// Use Find/Change across document to update date:
app.findPreferences = app.changePreferences = null;
myDoc.search("", false, false, myDate, {appliedCharacterStyle:myStyle});

function getDateString() {
var today = new Date();
var myDateString = today.toLocaleDateString();
myParts = myDateString.split(" 0");
if (myParts.length != 1) {
myDateString = myParts[0] + " " + myParts[1];
}
return myDateString.slice(0,-5) + "," + myDateString.slice(-5);
}
To use the script, copy and paste to a text editor and save as a plain text file with an appropriate name in the form UpdateDate.jsx in the Scripts folder of the Presets folder of your InDesign CS2 folder (you could put it in a subfolder if you wish). Then to run the script, double-click its name in the Scripts palette.

If you want to work it with RogueSheep's InEventScript, follow their directions on how to connect this to the Print command. If you want the date in a different format, change the function to provide the format -- this might require some research.

Dave

23 replies

Participating Frequently
September 28, 2006
Well Dave, as usual, you come through with flying colors. I did manage to create something that would work for the time being, but your scripts are far more efficient than what I came up with.

Thank you so much. I certainly do appreciate all you and the other people on this forum do to help each other.

Thanks!

Theresa
Inspiring
September 17, 2006
Theresa,

Sorry that I missed your message when you posted it. I had come back here to make an announcement about a change in the scripts posted at:

http://pdsassoc.com/downloads/DateScripts.zip

and so I belatedly found your message. The changes I've made were done because David Blatner and Anne-Marie Concepcion spoke at some length about the date insertion scripts covered in this topic.

I'd almost forgotten about them, to tell the truth, but the podcast caused me to revisit them. They come much closer to doing what you asked for, and I've included instructions on making the kind of change you requested.

There are still two scripts, but they work quite differently from before:

DateTimeSetup.jsx allows you to create two DateTimeFormat/Character style pairs (by means of a dialog) that allows you to have up to two date/time stamping formats in any of your documents.

Ive built-in 14 different formats and provided instructions on how to add more.

DateTimeInsert.jsx uses the settings for your document to insert the current date/time in place of any text that uses the character styles you assigned.

So, this second script is exactly as David and Anne-Marie described in the podcast, with the addition that it can now work with any of the 14 built-in formats and you can have two active at once in your document.

Ive written an eight-page user guide which explains how to install the scripts, how to operate them, and how to customize them.

To read more about the podcast or even to listen to it, follow this link:

http://indesignsecrets.com/indesignsecrets-podcast-031.php

Dave
Participating Frequently
August 22, 2006
Thanks for the great advice everyone.

I'm hoping that one of you may know the answer to this question.
I've been following the instructions for adding the date, but how do I format the text into a specific format like "08/06" for August 2006?

I keep getting error messages when I try to run the script. It's usually something along the lines of: "It was looking for a string and found a date instead..."

Thanks for your time!

TJV
Participating Frequently
June 20, 2006
Chad,

Here is some AppleScript examples:

Short date aka: 20.6.2006

set theDateString to (short date string of (current date))

tell application "Adobe InDesign CS2"
tell document 1
set contents of selection to theDateString
end tell
end tell


Regular date aka: tiistai 20. kesäkuu 2006 (propably in some other form in US computers)

set theDateString to (date string of (current date))

tell application "Adobe InDesign CS2"
tell document 1
set contents of selection to theDateString
end tell
end tell


You can also insert some other day than today. Here is example of seventh day from today aka: 27.6.2006

property dueDate : 7 * days


set theDateString to (short date string of ((current date) + dueDate))
tell application "Adobe InDesign CS2"
tell document 1
set contents of selection to theDateString
end tell
end tell


Shane probably can tell you even more fancier examples.

Kari
Participating Frequently
June 13, 2006
Yes, that did it. I deleted the crud, resaved it, and now it works. I don't know why it was adding those extra characters. I didn't add them. I opened your script directly in MS WORD and then resaved it as a text only file.

I should have used Apple TextEdit, but didn't know until know that you can change the default format to be plain text and not RTF. The crud doesn't show up in TextEdit. I should have known I would be in trouble if I used a microsoft product (no offense if you use them)

Thanks again,
Chad
Inspiring
June 13, 2006
What is the crud immediately before the first characters of the script? That would certainly cause an error. Is this saved as Plain Text? I tried to get around that problem by distributing it already in the same form but then you edited it.

Dave
Participating Frequently
June 13, 2006
Ôªø//DESCRIPTION: Update Date in Active Document

/*
¬(c)Copyright 2006 PDS Associates

This script must not be distributed without first obtaining written permission of:

Dave Saunders, proprietor
PDS Associates
P.O. Box 127
Allenhurst, NJ 07711-0127

e-mail: davesaunders@pdsassoc.com

donations gratefully received: http://order.kagi.com/?6CYQW&lang=en
*/

if (app.documents.length == 0) { exit() }
myDoc = app.activeDocument; //Global
var myStyle = myDoc.characterStyles.item("CurrentDate");
if (myStyle == null) { exit () }

// If we get here, we have work to do
var myDate = getDateString();

// Use Find/Change across document to update date:
app.findPreferences = app.changePreferences = null;
myDoc.search("", false, false, myDate, {appliedCharacterStyle:myStyle});

function getDateString() {
var today = new Date();
var myDateString = today.toLocaleDateString();
myParts = myDateString.split(" ");
return myParts[1] + " " + myParts[3];
}
Inspiring
June 13, 2006
Please post the text of the script.

Dave
Participating Frequently
June 13, 2006
Dave,

Actually when I run the script the first time with newly character styled text it runs fine but still adds the comma, then when I click on the script again, I get the javascript error.

?

Thanks
chad
Participating Frequently
June 13, 2006
Dave,

When I made the change I get a javascript error from InDesign,

Error number: 5
Error String: "Unterminated comment"
Line: 4

I am using InDesign CS and not CS2, I hope that doesn't make a differnence.

Thanks again for all your work. I'll definetely keep you in mind for future enterprise wide scripting.

Chad