General Question

makemo's avatar

AppleScript: get and reformat current date, then insert it in any text field?

Asked by makemo (531points) September 5th, 2008


I’m about to create an AppleScript that can reformat the current date into a YY-MM-DD string and then insert the result in any active text field.

Here’s exactly what I want:

1. Get the current date.
2. Reformat and convert that date to a YY-MM-DD string.
3. Insert that string into a text field via System Events keystrokes.

So far, I’ve only figured out step 3:

tell application “System Events” to keystroke current_date

Step 1 is also faitly obvious, but I’m stuck in step 2 – on how to reformat the date into something less verbose.

Observing members: 0 Composing members: 0

3 Answers

aidje's avatar

You’re my hero but I don’t know how to help you.

jrpowell's avatar

This is the best it could do. I haven’t played with Applescript much.

Not sure how to get around the parenthesis.

makemo's avatar

johnpowell: your contribution led to astonishing results. Thanks a bundle.

Here’s what I finally ended up using:

set {year:y, month:m, day:d, time string:t} to (current date)

set date_format to (y * 10000 + m * 100 + d) as string
set time_format to (t) as string

set reformated_date to (text items 3 thru 4 of date_format as string) & (text items 5 thru 6 of date_format as string) & (text items 7 thru 8 of date_format as string) & ” at ” & (text items 1 thru 2 of time_format as string) & (text items 4 thru 5 of time_format as string)

tell application “System Events” to keystroke (reformated_date)

Answer this question




to answer.

This question is in the General Section. Responses must be helpful and on-topic.

Your answer will be saved while you login or join.

Have a question? Ask Fluther!

What do you know more about?
Knowledge Networking @ Fluther