Re: Fun with coercing to string
Re: Fun with coercing to string
- Subject: Re: Fun with coercing to string
- From: Chris Nebel <email@hidden>
- Date: Tue, 20 Feb 2001 15:27:35 -0800
- Organization: Apple Computer, Inc.
"Stephen Swift (aka Burnum)" wrote:
>
Mac OS 8.1
>
AppleScript 1.1.2
>
>
WHAT WORKS:
>
month of (current date) --> February
>
>
set ThisMonth to month of (current date)
>
display dialog ThisMonth
>
--displays a dialog box with the text 'feb'
>
>
set ThisDate to day of (current date)
>
display dialog "It is the " & ThisDate & "th of February."
>
--displays a dialog box with the text 'It is the 20th of February."
>
>
WHAT DOESN'T WORK:
>
set ThisMonth to month of (current date)
>
display dialog "We are in the month of " & ThisMonth
>
--Can't make February into a string
>
>
According to my AppleScript reference tables current date command is
>
coercible to a list of its properties. How come I can use the month in a
>
dialog box by itself but when I try to put it with another string
>
AppleScript tells me 'February can't be a string'? And How come it will
>
take the day of the current date and make it into a string? Thanks.
"Current date" returns a date object. Date objects have properties for all
the obvious sub-bits -- day, month, year, etc. Most of the properties are
integers (e.g. day), but month and weekday are enumerations. AppleScript
1.1.2 can coerce an integer to a string, so the second "display dialog"
works, but can't do anything with an enumeration, so the third one doesn't.
AppleScript 1.4 (in Mac OS 9.0) added the ability to coerce from an
enumeration to a string (though not the other way around), so the third case
would work then.
Now the tricky bit: why does the first "display dialog" work? Well, it's
cheating. What you're actually seeing is the raw code for February, which
is "feb ". This is technically a bug, but it's a sort of useful one, so
it's probably not going to get fixed.
If you're sticking with Mac OS 8.1 and you still want to get the month as a
string, either write a handler to do it yourself, or use one of the many
date-formatting scripting additions.
--Chris Nebel
AppleScript Engineering