iCal Date/Time Question

I’m creating a script as an alarm in iCal… what I need to know is if there is a way to set theDate equal to the date of the event and theTime equal to the time of the event.

Then, I also want to insert theDate and theTime into a text paragraph:
set theBody to “…theDate at theTime…” but when I enter this command I get theDate and theTime written out.

Thanks for any help.

When your script runs, set theDate and theTime using (current date).
To get the date is easy, just get the parts and concantenate
set theDate to month of (current date) & " " & day of (current date) & ", " year of (current date)

The time is more difficult but doable. Fortunately, wiser heads than mine have found the answer.

Time formatting courtesy of Nigel Garvey

As for using variables like theDate and theTime in a string, again, concantenate. Anything placed between quotes is a literal string.

“…” & theDate & " at " & theTime & “the rest of the paragraph…”

Watch the spaces for proper formatting.

Well, these minds are FAR smarter than I, so much so that I’m confused. I just want to get rid of the seconds in the time. I need the time to be something like 7:30AM or 11:50PM or something like that.

Ah, yes, Nigel returns the time without the separator and in 24 hour notation with a leading zero. It’s simple, really. Time is returned in seconds.

set t to time of (current date) --this is the number of seconds since midnight
--hours is a special constant in Applescript equal to the number of seconds in an hour
-- I'll leave that as a lesson for the student :-)
-- div is an Applescript operator, it returns only whole factors and throws out the remainder
set hr to t div hours -- total seconds so far in day divided by 3600, throw out remainder
-- mod is the complement of div; it returns only the remainder
-- minute is a AS constant equalling the number of seconds in a minute.
-- t mod hours gives us the number of seconds since the last whole hour
-- divide by 60 and throw away any remaining seconds
set min to t mod hours div minutes 
-- this next section is for formatting.
-- going from in to out, hr * 100 gives us (if it's six thirty in the evening) 1800
--10000 + 1800 equals 11800; plus min (30) is 11830; coerced to string
-- text 2 thru 5 (this is why we coerced, wouldn't make sense as a number) is 1830
set merged_time to text 2 thru 5 of ((10000 + hr * 100 + min) as string)

The 10000 is added to get a leading zero for times before ten in the morning. Simple, no?

If we want time with separators we would format it this way:

set separated_time to hr & “:” & min

If we care about AM and PM we test:

if hr is greater than 12 then
	set hr to hr - 12
	set time_suffix to " PM"
else
	set time_suffix to " AM"
end if
if hr is equal to 0 then
	set hr to 12
end if
set twelve_hour_time to hr & ":" & min & time_suffix

Thanks again.

Just one more problem, when I use your code and the separated time I get something like 17:0 rather than 5:00.

EDIT
I got the hours figured out with:
set hr to t div hours - 12

Now I just need that extra 0.

EDIT 2
The 0 is actually the second number in the time. For example 8:02 is give as
{8,“:”,2}

EDIT 3

Forget everything above.

Kel on the Apple Forums answered my question:

set cur_date to (current date)
set time_string to (time string of cur_date)
tell time_string
set time_without_sec to (word 1 & “:” & word 2 & space & word 4)
end tell

And all the time I was writing that I was thinking that there must be a better way.

Good on ya, Kel!