The date is an important part of that. It needs to appear in the body and subject of the email to my Editor, in the heading of the daily segment of the blog post that collates each week’s headlines, and attached to each item in my record of ‘sent’ items.
And it appears in a couple of formats. Sometimes it’s like this:
Tuesday, 17 August 2010 and at other times it’s like this:
Another quirk is that sometimes it needs to be today’s date, and sometimes tomorrow’s, depending on when I run the scripts: the morning the column’s due, or the evening before.
The Current Date
This Applescript gets the current date:
Just type those 2 words into a blank Script Editor document and click the Run button. In the Result pane at the bottom of the window you’ll see a line like
date "Monday 16 August 2010 20:47:32 ".
The result includes the word ‘date’ and the speechmarks.
If all you wanted was the first part,
Monday 16 August 2010, then you need to work with it a bit. You need to get the
To get the time portion,
20:47:32 you’d get the
Note: the symbol
¬ shows where you must press the Return key to make a new line.
set myDate to date string of (current date) ¬
set myTime to time string of (current date) ¬
set myWords to myTime & " " & myDate ¬
The result of that sequence of commands is the line
"20:47:32 Monday 16 August 2010".
The date itself is stored as a bunch of numbers. Because they’re numbers you can do maths operations like add and subtract. Once you get the
string the date becomes a bunch of words.
So, to get tomorrow’s date (I don’t need to care about the time), I need to find the
Current Date and add 60 seconds * 60 minutes * 24 hours, or as I write it:
set Tomorrow to (current date) + (24 * 60 * 60) ¬
set myTomorrow to (date string of Tomorrow)
The result of that sequence of commands when I run it on Tuesday, 17 August 2010 is the line
"Wednesday 18 August 2010".
The Short Date
In the file where I keep a record of each item I use I want the date like this:
20100817. That format would allow me to sort easily.
To get that format I need to get, and then work with, the
Short Date in Applescript.
The Short Date string alone gets something like this:
"18/08/2010", but that’s not quite what I want.
To turn the date around I have to pick out each bit, using code like
((items 4 through 5 of ShortDate) as string). Items 4 through 5 net me the
Here’s the code from my script:
set myTomorrow to ((current date) + (24 * 60 * 60)) ¬
set ShortDate to short date string of myTomorrow ¬
set stampText to "20" & ((items 9 through 10 of ShortDate) as string) & ((items 4 through 5 of ShortDate) as string) & ((items 1 through 2 of ShortDate) as string)
Out of all that I get
stampText, which is the string I need in my preferred format:
Finding these bits and pieces of date and time for my Applescripts was a challenge for me. I’m no great scripter. Then once I had them running for today, it was another struggle to work out how to get tomorrow. I hope my script snippets here will help you in your Applescript adventures.
How do you use dates in your Applescripts? Leave a comment below.