Does AppleScript Studio deal with dates differently than AppleScript?
I’m pulling my hair out trying to figure out why I can’t make a date work in a Studio app.
In AppleScript, this works fine:
set dateEntered to "06/11/1967" as string
set theDate to (date dateEntered)
But in AppleScript Studio, it produces nothing.
Any ideas?
Where exactly did you place this chunk of code? And to what element of your interface did you connect it? Reading only the two lines of your example it’s a bit hard to guess where’s the problem…
Ciao
Farid
I have trimmed this down for readability, but the basic context is a field where a birthdate is entered…
on clicked theObject
tell window of theObject
set theBirthDate to "" as string
set BirthDateAsEntered to contents of text field "ChooseFieldTextField" as string
try
set theBirthDate to (date BirthDateAsEntered)
on error
display dialog "BirthDateAsEntered is " & BirthDateAsEntered & ". theBirthDate is " & theBirthDate
end try
end tell
end clicked
This always results in theBirthDate throwing an error. (I created the dialog to display what the variables are at the time of the error.)
If you format your text field as date (you do so by dragging the NSdateFormatter on it, it’s the one with the calendar icon…) its content is a valid date information without having to do any coercions from text to date and back to text. For instance this works for me:
on clicked theObject
tell window "main"
set BirthDateAsEntered to contents of text field "bdate" as date
set BirthDateAsEntered to BirthDateAsEntered as string
display dialog BirthDateAsEntered
end tell
end clicked
I did the coercion into string just for the purpose of the dialog to control the result, in any case, BirthDateAsEntered is a valid date right from the beginning.
Hope it helps
Farid
Unfortunately, I’ve tried that already too…
Your code gives me the AppleScript error
"Can’t make “06/12/1967” into a date. (-1700)
I just don’t get why it works in AppleScript but not in AppleScript Studio.
I was able to get the script to work only after designating the data entry field as being for a date specifically. I was really trying to be able to enter text into it as well.
Thanks for the help.