Is it not possible to alter a single word in a variable's text string?

I already know how to work around the following failed script via concatenation (or simply resesetting the variable), and I also don’t need this script for anything in particular; I’m still curious as to why it isn’t working - it seems pretty straightforward.

set testText to "This isn't a test."
set text item 2 of the text of testText to "is"

Look at the result of this:

set testText to "This isn't a test."
set textCount to count of text items of testText

Just because I wanted to learn how to do this:

set testText to "This isn't a test."
set beginNew to items 1 through -12 of testText
set endNew to items 11 through -1 of testText
set newText to beginNew & endNew as string

or another way:

set testText to "This isn't a test."
display dialog (((items 1 through -12 of testText) & (items 11 through -1 of testText)) as string)

and this:

set testText to "This isn't a test."
set textCount to count of words of testText

Alright, well that answers that, as I was addressing one character out of 18, not a word, however, when I change it so that the object of the change is the 2nd word, it still fails:

set testText to "This isn't a test."
set word 2 of the text of testText to "is"

capitalj, thanks for the replies, but both of your 2nd examples use concatenation, which I’m already familiar with. I guess what I’d like to know is if concatenating the pieces is a necessary workaround, or if pieces of the variable can be changed directly.

hi marc,

i don’t think the ‘words’ in a string are seen as variables. capitalJ’s solution is one way, here is another:


set testText to "This isn't a test."
set newWord to "is"
set newTestText to ""

set howMany to number of words in testText
set i to 1

repeat while i ≤ howMany
	if i is 2 then
		set newTestText to newTestText & newWord & space
	else
		set newTestText to newTestText & word i of testText & space
	end if
	--log newTestText
	set i to (i + 1)
	--log i
end repeat

set testText to newTestText

display dialog testText

you should probably do something like this in a ‘handler’ or ‘function’. also, there is probably a shorter way to do this, but it’s beyond me.

EDITED: because my cut & paste didn’t get everything. & removed unnecessary code.

Hi Marc,

This is from the AppleScriptLanguageGuide.pdf:

You cannot set the value of an element of a string. For example, if you attempt
to change the value of the first character of the string “boris” as shown in the
following example, you’ll get an error.
set myName to “boris”
set character 1 of myName to “D”
–results in an error, because you cannot set the values of
–elements of strings

This came from the section on strings.

Edited: and BTW, this only applies to vanilla AppleScripts. You can do this in many text editors.

gl,

also, this may be the most useful way, if you know the word you are looking for in the string:


set testText to "This isn't a test."
set newWord to "is"
set newTestText to ""

set howMany to number of words in testText
set i to 1

repeat while i ≤ howMany
	if word i of testText is "isn't" then
		set newTestText to newTestText & newWord & space
	else
		set newTestText to newTestText & word i of testText & space
	end if
	--log newTestText
	set i to (i + 1)
	--log i
end repeat

set testText to newTestText

display dialog testText

EDITED: removed some unnecessary code

I use this handler (a product of Nigel Garvey and Kai):

set tt to "This isn't a test."
findAndReplace("isn't", "is", tt) -- > "This is a test"

on findAndReplace(toFind, toReplace, theText)
	set astid to AppleScript's text item delimiters
	set AppleScript's text item delimiters to toFind
	set textItems to theText's text items
	set AppleScript's text item delimiters to toReplace
	tell textItems to set editedText to beginning & toReplace & rest
	set AppleScript's text item delimiters to astid
	return editedText
end findAndReplace

EDIT: where, btw, the clever bit is: “tell textItems to set editedText to beginning & toReplace & rest”

I appreciate the help of all who contributed, but Kel had the info that I was looking for. I was unsure if this was a syntax error on my part or an actual limitation. This was just a hypothetical, but it’s good info to know. Thanks again.

I’m grateful for all the examples. Now if I could just type faster…

Know just what you mean, j. I was typing the following (which I’ll post for good measure, anyway) - only to find the topic had already moved on several messages… :stuck_out_tongue:

In the case of a simple value (such as text), not directly, Marc Anthony - although you can change an element of composite AppleScript value (such as a list or record):

set example_list to words of "This isn't a test."
set example_list's item 2 to "is"
example_list --> {"This", "is", "a", "test"}

Of course, you then need to put the result back together again as text (or use one of the other methods suggested).

It can also be possible to modify the text elements of certain application objects:

tell application "TextEdit"
	activate
	tell (make new document with properties {text:"This isn't a test."})
		delay 2 (* for demonstration purposes *)
		set word 2 of its text to "is"
	end tell
end tell

As with most of these things, the most appropriate technique depends on what you’re ultimately trying to do… :slight_smile: