Total newbie help

I’m new to AppleScript and I’ve been fooling around with it a little, but I’m having this stupid little issue that I can’t seem to solve.

I’m trying to write a script that takes a string and returns it backwards. Easy enough, right?

I’m getting this error when I try to run it.

“Can’t set character 1 of “” to “j”.”

Anyone have any idea what I’m doing wrong?

on run
	display dialog "Reverse:  " default answer "abcdefghij"
	set theString to text returned of result
	set newString to ""
	set number_of_characters to length of theString
	set counter to 1 as integer
	repeat until counter is greater than number_of_characters
		set character counter of newString to character (number_of_characters - counter + 1) of theString
		set counter to counter + 1
	end repeat
	return newString
end run

You’re making it harder than it needs to be. Applescript has a built-in ‘reverse’ command… :wink:

set theInput to text returned of (display dialog "Reverse:" default answer "abcdefghij")
set theOutput to (reverse of (characters of theInput)) as string


This works:

display dialog "Reverse the String?" buttons {"Yes Please"} default button 1 default answer "enter the string to reverse here"
set theString to text returned of result
set RevString to ""
set strLength to count of characters of theString
repeat with k from strLength to 1 by -1
	set thisChar to character k of theString
	set RevString to RevString & thisChar
end repeat

Careful, she’s a cheatin’ woman and she’ll break your heart drive

Hi, john. I hope it’s clear from jobu’s and NovaScotian’s replies that, in AppleScript, you can’t set a character in a string to anything. You have to make a new string containing the change(s) you want.

NovaScotian’s method uses concatenation, and (assuming you go with his default answer) his RevString variable gets set successively to the strings “”, “e”, “er”, “ere”, “ereh”, "ereh ", etc.

jobu’s method creates a list of the input strings’s individual characters - {“a”, “b”, “c”, “d”, “e”, “f”, “g”, “h”, “i”, “j”} - then creates a reversed version of that list and coerces the result back to string. This is simpler and faster. But coercing lists to string brings ‘AppleScript’s text item delimiters’ into play. If these are set to anything other than their default value - {“”} or “” - you’ll find other characters inserted into the final string:

set AppleScript's text item delimiters to "x" -- suppose they've been left as this, for some reason

set theInput to "abcdefghij"
set theOutput to (reverse of (characters of theInput)) as string
--> "jxixhxgxfxexdxcxbxa"

In practice, it’s usualler safest to set the delimiter explicitly before coercing a list to string:

set theInput to "abcdefghij"

set astid to AppleScript's text item delimiters -- store the current delimiter value
set AppleScript's text item delimiters to "" -- explicity set the delimiter for this coercion
set theOutput to (reverse of (characters of theInput)) as string
set AppleScript's text item delimiters to astid -- restore the previous delimiter

--> "jihgfedcba"

I agree that jobu’s method:

is better and faster, but for a noobie I was looking for the most obvious “grab one character at a time and assemble in reverse” method.

Right. I wasn’t trying to belittle either method. (Sorry if I gave that impression.) It seemed to me that the original request was for enlightenment rather than for a string reverser per se. I was trying to give a reasonably full explanation of why the original code wouldn’t work and why yours and jobu’s did.

Not to worry - I didn’t feel sneered at; my skin is not that thin and yours was a good explanation.

Feelin the love in BBS! :oops: