As a somewhat novice Applescripter, I’m confused by why coercion of text to date works in some contexts and not in others. For example, this script generates an error:
tell application "AppleScript Editor"
set theText to "04/15/2012"
set theDate to date theText
end tell
But if I leave off the tell block, the script works:
set theText to "04/15/2012"
set theDate to date theText
If I put the coercion line from the first script in handler, that makes it work:
tell application "AppleScript Editor"
set theText to "04/15/2012"
set theDate to my DateFromText(theText)
return theDate
end tell
on DateFromText(theText)
return date theText
end DateFromText
This variation on the first script also generates and error:
tell application "AppleScript Editor"
set theText to "04/15/2012"
set theDate to theText as date
end tell
However, this variation with “double coercion” works:
tell application "AppleScript Editor"
set theText to "04/15/2012"
set theDate to date theText as date
end tell
If anyone can explain to me why some of these work and others don’t, it would be greatly appreciated.
Thanks!