Applescript text item delimiters not working

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.

Answered by red_menace in 807839022

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.

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.

I'm guessing that AppleScript's text item delimiters are somehow getting messed up in your larger script and that's causing some funny behavior. Not sure if it'll help, but I'm always carefully saving and restoring that value whenever I use it like so ~


 -- SAVE delimiters
set tid to AppleScript's text item delimiters

-- do stuff using delimiters
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

 -- RESTORE delimiters
set AppleScript's text item delimiters to tid

return {dayOfWeekAbbrev, dayOfMonth, monthName}

This keeps any behaviors resulting to my modifications of the delimiters local to this block of code and I don't need to worry about side effects elsewhere. Maybe check through your script and see what's happening the AppleScript's text item delimiters?

Good suggestion, and good programming practice. I moved the code to a user-defined handler, with no changes, and it worked as expected. But it would still have been useful to understand what went wrong.

Here's how I look at it ~ I suspect that AppleScript's text item delimiters were somehow getting set to an empty string (or possibly {}, but that's another story). And, that matches each of the empty strings between all of the characters in the text.

For example, try this script:

set tid to AppleScript's text item delimiters

set AppleScript's text item delimiters to ""
set stringparts to text items of "This is a test."

set AppleScript's text item delimiters to tid

return stringparts

-- returns a list: 
-- {"t", "h", "i", "s", " ", "i", "s", " ", "a", " ", "t", "e", "s", "t"}

Your variable originalDateString got coerced to a list in your bigger script. from "Mon 22nd April" to {"M", "o", "n", " ", "2", "2", "n", "d", " ", "A", "p", "r", "i", "l"}

so, text item 3 in the list is "n".

place in your code, before setting the text item delimiter

set originalDateString to originalDateString as text

.

Text item delimiter.

I don't bother to save it to a variable "tid". I just set it back to it's default "", an empty string. Text item delimiters only changes when we temporarily need it. By saving it to a variable you might backup an uncorrect tid and restore this mistake. So, set it to it's default "".

handler based on your example

set originalDateString to items of "Mon 22nd April" -- 'items of' to test with a list
set dateItems to my getDateItems(originalDateString) -- 'dateItems', list of 3 items
-- or declare 3 variables at once
set {dayOfWeekAbbrev, dayOfMonth, monthName} to my getDateItems(originalDateString)


on getDateItems(someText as text)
	try
		set AppleScript's text item delimiters to " "
		set {ti1, ti2, ti3} to {text item 1, text item 2, text item 3} of someText
		set AppleScript's text item delimiters to ""
		return {ti1, ti2, ti3}
	on error errorMessage number errorNumber
		set AppleScript's text item delimiters to ""
		error ((name of me) & linefeed & "....> getDateItems()" & linefeed & linefeed & errorMessage) number errorNumber
	end try
end getDateItems
Applescript text item delimiters not working
 
 
Q