Re: birth date from Address Book
Re: birth date from Address Book
- Subject: Re: birth date from Address Book
- From: Paul Berkowitz <email@hidden>
- Date: Tue, 11 Mar 2003 22:50:23 -0800
On 3/11/03 6:42 PM, "Darwin Zins" <email@hidden> wrote:
>
Can anyone tell me how to take the birth date from the Address Book and
>
change it to "DD.MM.YYYY" so 1/30/1970 would be 30.1.1970. I can't
>
seem to even set a variable to the birth date. I am trying to do
>
something like this:
>
>
try
>
set a_birth_date to birth date of aCard
>
display dialog a_birth_date
>
if a_birth_date is not "" then
>
set tmp_birth_date to a_birth_date
>
display dialog my_birth_date
>
set tmp_birth_date to day of my_birth_date & "." & month of
>
my_birth_date & "." & year of my_birth_date
>
display dialog tmp_birth_date
>
set |Birthday| of zCard to tmp_birth_date as string
>
end if
>
on error
>
set nothing to ""
>
end try
>
>
The only way I can even get it to set the value of a_birth_date to the
>
birth date is by changing it to:
>
set a_birth_date to birth date of aCard as string
>
No, that's not true. To display it in display dialog you need to turn it
(not necessarily the same variable) into a string, but , well, 'display
dialog' requires strings. It doesn't display dates.
One of your problems is that you haven't figured out what to do an extra
problem if a contact doesn't have a birth date. These Cocoa apps can be
rather ridiculous about such things. If aCard doesn't have a birth date then
it won't error on the line:
set a_birth_date to birth date of aCard
which is a serious bug as far as I'm concerned. Of course when you next try
to use the a_birth_date variable, _then_ it errors. You've discovered
that ' a_birth_date as string' gives you "", which is something to work
with. Better I think is to get the 'properties' property. If there's no
birth date you get 'missing value' without needing to convert it to string:
tell application "Address Book"
set aCard to person 1
set theProps to properties of aCard
set a_birth_date to birth date of theprops
--> date "Saturday, October 16, 1948 12:00:00 AM" --[US style]
set aCard to person 2
set theProps to properties of aCard
set a_birth_date to birth date of theprops
--> missing value
end tell
You can 'tell theProps' everything to get all the properties you need (but
not elements; for that you can 'tell aCard').
Then, you need some way to convert 'month' to an integer. there are much
classier ways, including some Unix methods and Emmanuel's method, but this
will do so you get the general idea. Then 'day' has to be coerced 'as
string' since it's left-most in the concatenation. Any integers that come
after a string will be coerced naturally, so another way to do it would be:
set tmp_birth_date to "" & (day of a_birth_date) & "." & mm & "." & year
of a_birth_date
and mm could be done the same way without bothering to coerce it explicitly
as string. I do so below to draw your attention to the fact that it's not a
string.
Finally, you switched varuables midstream, from a_birth_date to
my_birth_date, so every line in the script was erroring. So that's only
about 9 little errors - welcome to AppleScript!
tell application "Address Book"
set aCard to person 1
set theprops to properties of aCard
set a_birth_date to birth date of theprops
display dialog a_birth_date as string -- why?
if a_birth_date is not missing value then
set mm to month of a_birth_date
repeat with i from 1 to 12
if mm = item i of {January, February, March, April, May, June,
July, August, September, October, November, December} then
set mm to (i as string)
exit repeat
end if
end repeat
set tmp_birth_date to ((day of a_birth_date) as string) & "." & mm &
"." & year of a_birth_date
display dialog tmp_birth_date
--set |Birthday| of zCard to tmp_birth_date -- what's this?
--undefined
else
display dialog "No birthday for this contact."
end if
end tell
--
Paul Berkowitz
_______________________________________________
applescript-users mailing list | email@hidden
Help/Unsubscribe/Archives:
http://www.lists.apple.com/mailman/listinfo/applescript-users
Do not post admin requests to the list. They will be ignored.