Universal date stamp

Everybody has to write down the current date, in documents, letters etc… so I thought that there would be plenty of choice finding a freeware to do this at the press of one key.
I could find some but only for Entourage or for BBEdit. Surprising.:confused:

Perhaps an applescript could do the job? and be useable with TextEdit and Thunderbird (and in the Finder)?

Thank you for a suggestion.

maurice
Mac mini running Tiger

Did you ever get an answer to this? You might get better response from the OS X forum. But in case you didn’t, here is a script to copy the short date to the clipboard (so you can paste it in any document):

set the clipboard to (shortDate(current date))

to shortDate(theDate)
	set monthList to {"January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December"}
	
	set mm to 1
	repeat while (month of theDate) as string ≠ (item mm of monthList) as string
		copy mm + 1 to mm
	end repeat
	if mm < 10 then copy "0" & mm to mm
	
	set dd to day of theDate
	if dd < 10 then copy "0" & dd to dd
	
	set yy to text 3 thru 4 of ((year of (theDate)) as string)
	
	if second word of (date ("1/2/3" as string) as string) is "January" then
		return mm & "/" & dd & "/" & yy
	else
		return dd & "/" & mm & "/" & yy
	end if
end shortDate

and here’s one that will paste directly into TextEdit’s front window (at the start of text)

copy shortDate(current date) to newDate
tell application "TextEdit"
	make new text at front of paragraph 1 in text of front document with data newDate
end tell

to shortDate(theDate)
	set monthList to {"January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December"}
	
	set mm to 1
	repeat while (month of theDate) as string ≠ (item mm of monthList) as string
		copy mm + 1 to mm
	end repeat
	if mm < 10 then copy "0" & mm to mm
	
	set dd to day of theDate
	if dd < 10 then copy "0" & dd to dd
	
	set yy to text 3 thru 4 of ((year of (theDate)) as string)
	
	if second word of (date ("1/2/3" as string) as string) is "January" then
		return mm & "/" & dd & "/" & yy
	else
		return dd & "/" & mm & "/" & yy
	end if
end shortDate

Many thanks to you, Kevin, for the second script which is almost exactly what I was looking for.

It would be absolutely perfect if it could write the date at the insertion point (instead of the beginning of the page) and if it could function as well in Thunderbird and in the Finder.

Is that possible?

Then I would save it as an app, assign a function key to it and the trick woul be done.

Maurice,

Apparently, TextEdit’s dictionary doesn’t support inserting things are the insertion point (which is technically “the selection” but with no text selected). Appleworks will do it. Not sure about Thunderbird, I’m using Mail so I don’t have Thunderbird installed. But try taking the script below and working with it in Thunderbird and see if you can get it to do what you want.

copy shortDate(current date) to newDate
tell application "AppleWorks 6"
	copy newDate to the selection of the front document
end tell

to shortDate(theDate)
	set monthList to {"January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December"}
	
	set mm to 1
	repeat while (month of theDate) as string ≠ (item mm of monthList) as string
		copy mm + 1 to mm
	end repeat
	if mm < 10 then copy "0" & mm to mm
	
	set dd to day of theDate
	if dd < 10 then copy "0" & dd to dd
	
	set yy to text 3 thru 4 of ((year of (theDate)) as string)
	
	if second word of (date ("1/2/3" as string) as string) is "January" then
		return mm & "/" & dd & "/" & yy
	else
		return dd & "/" & mm & "/" & yy
	end if
end shortDate

Hi, Maurice.

Although you can use the core AppleScript language to get and format the current date for you, inserting the text at a particular point in an application document is a matter of addressing commands to the application concerned. From that point of view, a universal script isn’t possible.

But I see that, in fact, that you’re running Tiger. (See the OS X forum.) So you could use this to enter the current date at the insertion point of the frontmost document:

tell application "System Events"
	keystroke (date string of (current date))
end tell

The problem with this is the the document has to be frontmost while the script’s running. That means you can’t use the script from Script Menu, which brings System Events to the front. I’ve never looked into assigning scripts to keys, but if you have some software that allows that, and which doesn’t change the focus of the text input, the code above may be of some use. Otherwise, it’s a simple matter to produce a few copies of it, each of which has a line to activate a specific application and which lives with your other scripts for that application. For instance:

tell application "TextEdit" to activate
tell application "System Events"
	keystroke (date string of (current date))
end tell

Kevin,

It is very kind of you to try and help me with this seemingly easy task, which turns out to be harder than I thought:

after:
copy shortDate(current date) to newDate
tell application “TextEdit”
instead of:
make new text at front of paragraph 1 in text of front document with data newDate

I tried:
make new text at end of front document with data newDate
and I tried:
make new text at end of paragraph 1 in text of front document with data newDate

but the date keeps popping on the top left corner of the page.

If there are no way of writing the shortdate at the insertion point, I have a script which puts the shortdate in the Clipboard, then typing Command V will bring it at the insertion point. It would be handy in this case to append to the script a “type Command V” function.
Before OS X AutoType used to do this very simply, but it is no longer supported. Do you know another way of scripting Command V?

Nigel,

Thanks for the suggestions. I will try them and report the result. Assigning a Function key to the date should not be a problem. I have done it before. It is surprising that Tiger does not come with a dedicated key for entering the date on a document or on files in the Finder!

Nigel,

The TextEdit scriptlet does a good job of placing the date at the insertion point Thanks!.. but it is not the short date (I am trying for dd/mm/yy) Can this be configured?

To the other scriptlet I added a top line to obtain this:
tell application “Finder” to activate
tell application “System Events”
keystroke (date string of (current date))
end tell

However I must have done it wrong since it does not add the date to a filename in the Finder. Any clue?

I surely must have System Events installed since the TextEdit scriptlet works, but I haven’t seen any app of that name.
I searched with Spotlight and all it found was one script (called “Tell System Events”

Hi, Maurice.

Yes indeed. We’ve just been discussing something similar in a nearby thread. For your purposes, the TextEdit script would be:

tell application "TextEdit" to activate
tell application "System Events"
	keystroke (short date string of (current date)) 
end tell

The ‘short date string’ property of a date (Panther or later) is formatted according to your preferences in the “International” panel of System Preferences. If your normal preferences are something other than what you want from the script, you can use a handler, as follows. This produces a dd/mm/yy short date:

on ddmmyy(theDate)
	-- Get the day and year numbers.
	set {day:d, month:m year:y} to theDate
	
	-- Get the month number using a "French Vanilla" variant.
	copy theDate to b
	set b's month to January
	set m to (b - 2500000 - theDate) div -2500000
	
	-- Combine the results into a yyyymmdd value, coerce
	-- that to string, extract and recombine the relevant bits.  
	tell (y * 10000 + m * 100 + d) as string -- (or as unicode text)
		return text 7 thru 8 & "/" & text 5 thru 6 & "/" & text 3 thru 4
	end tell
end ddmmyy

tell application "TextEdit" to activate
tell application "System Events"
	keystroke my ddmmyy(current date)
end tell

I think that when the Finder stops being the frontmost application (when you activate the script), the selected icon’s text field ceases to be selected. However, the icon itself remains selected ” unless whatever you do to run the script changes that ” so you could use this Finder-specific script:

on ddmmyy(theDate)
	-- Get the day and year numbers.
	set {day:d, year:y} to theDate
	
	-- Get the month number using a "French Vanilla" variant.
	copy theDate to b
	set b's month to January
	set m to (b - 2500000 - theDate) div -2500000
	
	-- Combine the results into a yyyymmdd value, coerce
	-- that to string, extract and recombine the relevant bits.  
	tell (y * 10000 + m * 100 + d) as string -- (or as unicode text)
		return text 7 thru 8 & "-" & text 5 thru 6 & "-" & text 3 thru 4
	end tell
end ddmmyy

tell application "Finder"
	activate
	set f to item 1 of (get selection)
	set f's name to my ddmmyy(current date)
end tell

Note that although it’s possible, it’s not generally considered a good idea to use slashes in OS X file and folder names, as slashes are used as separators in Unix paths. I’ve changed the slashes to dashes in the handler above, but of course it’s up to you if you want to change them back.

System Events is a “faceless” application that handles much of the background work in Mac OS X. It took over many of the tasks that were handled by the Finder in OS 9 and earlier. Even some of the jobs that the Finder has retained are partially subcontracted to System Events. This can occasionally cause problems from a scripting point of view, but, happily, those problems aren’t encountered here. :slight_smile:

Hi, I’ve being playing around, and this seems to work for me for getting to the frontmost app, with or without using the script menu:

tell application "System Events"
	-- 'active application' must be done outside of System Events, hence 'my'. 
	(* Using 'name' usually gets "AppName.app" for modern apps,
	    'displayed name' just gets 'AppName' without extension.
	    Doesn't seem to complain about it, hence 'name' *)
	my (activate application (name of (path to frontmost application)))
	keystroke (date string of (current date))
end tell

In terms of freeware, I can always recommend Peter Maurer’s Butler (have an keystroke action in the menubar) or Textpander (automatic text expansion in active app, for example, ‘ASS’ becomes ‘AppleScript Studio’, but can also use dates, clipboard contents, formatted text, etc.).

Silly me, should also be able to use:

tell application "System Events"
	my (activate application (path to frontmost application as Unicode text))
	keystroke (date string of (current date))
end tell

Wow! It does work in Script Menu ” in Tiger at least. I’ve not seen that before. Well spotted, Qwerty! :slight_smile:

A more conventional layout also works:

tell application (path to frontmost application as Unicode text) to activate
tell application "System Events"
	keystroke (date string of (current date)) -- or: keystroke (short date string of (current date))
end tell

Nigel,

You have completely answered my question and taught me a few things (but applescripting seems more like an art rather than a science and the bulk of it is way over my head!)

Many thanks for your help.

PS: the script which starts with:
on ddmmyy(theDate)
– Get the day and year numbers.
set {day:d, month:m year:y} to theDate

gets an alert from the Compiler:
Syntax Error
Expected “,” or "}"but found property

Qwerty,

Thank you for the scriptlets, and for reminding me about Textpander, which is a very neat way of wrtiting a date and spares the time of the patient helpers. I am very grateful to all of you for your goodwill.

Yeah, thanks, that’s the way! Using the ‘name’ required System Events, but I forgot to take it out in the next one. Silly me again, I guess.

Hi, Maurice. Try putting a comma after the ‘month:m’, and then it should compile. Glad to help. :slight_smile:

Hello, Maurice.

Yes. It does. Sorry. As Qwerty pointed out, there should be a comma after ‘month:m’.

While I was posting, I decided to simplify the handler to take advantage of the month-to-integer coercions that are available in Panther and Tiger. Then I changed my mind again and obviously made a mess of the edit! If you or anyone else is interested, here are the two versions of the handler, hopefully correct this time! :slight_smile:

Only for versions of AppleScript with Panther or later:

on ddmmyy(theDate)
	-- Get the day and year numbers and the month.
	set {day:d, month:m, year:y} to theDate
	
	-- Combine the results into a yyyymmdd value, coerce
	-- that to string, extract and recombine the relevant bits. 
	-- The month is automatically coerced to integer during the calculation.
	tell (y * 10000 + m * 100 + d) as string -- (or as unicode text)
		return text 7 thru 8 & "/" & text 5 thru 6 & "/" & text 3 thru 4
	end tell
end ddmmyy

-- ddmmyy(current date)

For any version of AppleScript:

on ddmmyy(theDate)
	-- Get the day and year numbers.
	set {day:d, year:y} to theDate
	
	-- Get the month number using a "French Vanilla" variant.
	copy theDate to b
	set b's month to January
	set m to (b - 2500000 - theDate) div -2500000
	
	-- Combine the results into a yyyymmdd value, coerce
	-- that to string, extract and recombine the relevant bits.  
	tell (y * 10000 + m * 100 + d) as string -- (or as unicode text)
		return text 7 thru 8 & "/" & text 5 thru 6 & "/" & text 3 thru 4
	end tell
end ddmmyy

-- ddmmyy(current date)