Using AppleScript's Text Item Delimiters

Hi.

The first item in the list isn’t actually the text “a” ” although it’s logged as that ” but the reference item 1 of “aabbccdd”, since that’s the value of x in the kind of repeat: repeat with x in .. Until recently, coercing a list containing a reference to text only included the items before the reference (if any). That seems to be what’s happening in your case. On my Mountain Lion system (AppleScript 2.2.4), the coercion attempt causes an error: “Can’t make {item 1 of "aabbccdd", "", "", "bbccdd"} into type text.”

Instead of the reference in the list, you should use its contents or it as text:

set text item y in getUniqueCharacters to contents of x

-- Or:
set text item y in getUniqueCharacters to x as text

However, this doesn’t actually make the script work. :frowning: The output’s exactly the same as the input. I’ll look at it again shortly.

Nigel, are you sure the error isn’t happening during pass 2 in the y-loop, as a reaction to the unexpected result during pass 1? (I get that same error during pass 2 as well… Well, I ~think~ it’s the same error!)

Here’s a working version, if I’ve correctly understood the aim:


stripRedundantCharacters("aabbccdd")

to stripRedundantCharacters(inputText)
	set getUniqueCharacters to inputText
	
	set astid to AppleScript's text item delimiters
	repeat with i from 1 to (count inputText)
		set x to character i of inputText
		set AppleScript's text item delimiters to x
		set getUniqueCharacters to getUniqueCharacters's text items
		set AppleScript's text item delimiters to ""
		-- Concatenating the 'rest' list to text automatically coerces it to text using the current "" delimiter.
		set getUniqueCharacters to beginning of getUniqueCharacters & x & rest of getUniqueCharacters
	end repeat
	set AppleScript's text item delimiters to astid
	
	return getUniqueCharacters
end stripRedundantCharacters

Edit: And here’s a modification which cycles through the output string instead of the input. Duplicate characters are always to the right of the index, so their removal further reduces the number of iterations still to be done.


stripRedundantCharacters("aabbccdd")

to stripRedundantCharacters(inputText)
	set getUniqueCharacters to inputText
	
	set astid to AppleScript's text item delimiters
	set i to 1
	repeat until (i > (count getUniqueCharacters))
		set x to character i of getUniqueCharacters
		set AppleScript's text item delimiters to x
		set getUniqueCharacters to getUniqueCharacters's text items
		set AppleScript's text item delimiters to ""
		-- Concatenating the 'rest' list to text automatically coerces it to text using the current "" delimiter.
		set getUniqueCharacters to beginning of getUniqueCharacters & x & rest of getUniqueCharacters
		set i to i + 1
	end repeat
	set AppleScript's text item delimiters to astid
	
	return getUniqueCharacters
end stripRedundantCharacters

Bingo!
And it now has sunk-in that your diagnosis in Post #21 is the crux, i.e.,…


set z to "aabbccdd"

repeat with x in z
	set text item delimiters to x
	set z to text items of z
	set text item 1 of z to x --> {[item 1 of "aabbccdd"], "", "bbccdd"}
	log z --> Booby trap!: {"a", "", "bbccdd"}
	set text item delimiters to ""
	set z to z as text
	log z --> ""
	exit repeat
end repeat

(Whereas this is a text item delimiters booby trap awaiting to snare the unwary, I guess these posts indeed belong in this tutorial thread.)

A big thanks and tip o’ the hat, Nigel!

AppleScript: 2.0.1
Browser: Firefox 4.0.1
Operating System: Mac OS X (10.5)

Well. The problem turned out be not directly connected with text item delimiters; but if the poster (yourself) thought it might be happening because of something he hadn’t understood in Adam’s article, it would be reasonable to ask here. Otherwise “AppleScript | Mac OS X” is the place. :slight_smile:

In Applescript 2.4, some of the examples in the first script return something very different.
It´s seems that, even though I have the same TID “”, AS recognizes “-” and “*” as a TID; and don’t consider “_” as a word.

(If I ask for “length of Applescript’s text item delimiters” the answer is: 1)


words of “Hi, I’m Peggy-Sue” → {“Hi”, “I’m”, “Peggy-Sue”}
– {“Hi”, “I’m”, “Peggy”, “Sue”}

words of “Nowis the.time&for all_good (folks) to learn-AppleScript"
→ {“Now”, "
”, “is”, “the.time”, “&”, “for”, “all”, “_”, “good”, “folks”, “to”, “learn-AppleScript”}
– {“Now”, “is”, “the.time”, “for”, “all_good”, “folks”, “to”, “learn”, “AppleScript”}

words of “set-piece is hyphenated” → {“set-piece”, “is”, “hyphenated”} - the hyphen isn’t separated
– {“set”, “piece”, “is”, “hyphenated”} (now it’s not)

words of “funnyword with asterisk" → {“funny”, "”, “word”, “with”, “asterisk”} - the asterisk is separate
– {“funny”, “word”, “with”, “asterisk”}

Hello, first of all, that tutorial is awesome, it gave me great new ideas for my application (btw I’m creating a calculator).
I am referring to the first part of the tutorial, the “word of” part. For the “minus”, “times” and “divided by” section of the calculator I can’t get the second word, which would be “-”, “*” or “/”. Here’s my code for the 4 basics.


set response to text returned of (display dialog "Enter one of the 4 basic arithmetic operations 
				(+, -, *, /)" default answer "" buttons {"Continue"} default button 1 with title "Basic arithmetic operations")
			set a1 to the result
			try
				repeat with aCharacter in response
					if (aCharacter as text) is not in allowedAll then
						display dialog "Please enter numbers only" buttons {"OK"} cancel button 1 default button 1
					end if
					if (word 2) of a1 is "+" then
						set a2 to (word 1) of a1
						set a3 to (word 3) of a1
						set a4 to a2 + a3
						display dialog a2 & " + " & a3 & " = " & a4 with title "Result" buttons {"Copy result to clipboard", "Okay"} default button 2
						if the button returned of the result is "Okay" then
							exit repeat
						else
							set the clipboard to a3 as text
							exit repeat
						end if
					else if (word 2) of a1 is "-" then
						set a2 to (word 1) of a1
						set a3 to (word 3) of a1
						set a4 to a2 - a3
						display dialog a2 & " - " & a3 & " = " & a4 with title "Result" buttons {"Copy result to clipboard", "Okay"} default button 2
						if the button returned of the result is "Okay" then
							exit repeat
						else
							set the clipboard to a3 as text
							exit repeat
						end if
					else if (word 2) of a1 is "*" then
						set a2 to (word 1) of a1
						set a3 to (word 3) of a1
						set a4 to a2 * a3
						display dialog a2 & " ・ " & a3 & " = " & a4 with title "Result" buttons {"Copy result to clipboard", "Okay"} default button 2
						if the button returned of the result is "Okay" then
							exit repeat
						else
							set the clipboard to a3 as text
							exit repeat
						end if
					else if (word 2) of a1 is "/" then
						set a2 to (word 1) of a1
						set a3 to (word 3) of a1
						set a4 to a2 / a3
						display dialog a2 & " : " & a3 & " = " & a4 with title "Result" buttons {"Copy result to clipboard", "Okay"} default button 2
						if the button returned of the result is "Okay" then
							exit repeat
						else
							set the clipboard to a3 as text
							exit repeat
						end if
					end if
				end repeat
				exit repeat
			end try

But as I said, that doesn’t work with the “minus”, “times” and “divided by” section. So I tried this workaround, as described in the tutorial


set a1 to "4/2"
set x to AppleScript's text item delimiters
set AppleScript's text item delimiters to ASCII character 47 -- (that's the slash)
return word 2 of a1
set AppleScript's text item delimiters to x

but that still returnes 2, which is supposed to be the third word :frowning:
Your help is greatly apprechiated :smiley:

Glad you liked the tutorial, Colin. Your problem is that “/”, “*”, etc. are not words in AppleScript, they are characters.

Try this:

set theString to "a / c . e"
set W to words of theString --> "a", "c", "e"

as you see, only the letters (separated by spaces) show up. You’ll have to hunt for those characters specifically.