I have a string of the form “Mon 22nd April”. I’m trying to extract the day (i.e. Mon), the date (i.e. 22nd) and the month (i.e. April) using this Applescript:
set originalDateString to “Mon 22nd April”
-- Extract the components by splitting the string
set AppleScript's text item delimiters to " "
set dayOfWeekAbbrev to text item 1 of originalDateString
set dayOfMonth to text item 2 of originalDateString
set monthName to text item 3 of originalDateString
When I run this on its own it works as expected:
dayOfWeekAbbrev is set to “Mon”
dayOfMonth is set to “22nd”
monthName is set to “April”
When I run this inside a bigger script involving Numbers, the text item delimiters fails to work, no compile or run time errors occur and I end up with:
dayOfWeekAbbrev is set to “M”
dayOfMonth is set to “o”
monthName is set to “n”
I.e the first three characters of the string.
If I replace originalDateString with the literal string “Mon 22nd April” I get the same result. In other words, text items and being recognized as individual characters, no delimiter (or delimiter is null). Totally confused.
The usual approach would be to save whatever the existing delimiters are, set the delimiters to what you want to use, split the string into a list of its text items, restore the previous delimiters, and then just use the list. You are continually splitting the string to get each piece, so all it would take is for the delimiters to get changed/reset somewhere else in the bigger script for the text items to be different the next time you split the string. Another option would be to try using the words
of the string.