Replace spaces in text with number

Hello,

I need to replace spaces in some text with %20.

Something like:

set theText to “everybody is happy today”

with the result being “everybody%20is%20happy%20today”

Thanks!

As often happens, searching for the answer is not enough. I had to actually ask for help before I could find the answer on my own.

set theText to "everybody is happy today"
set AppleScript's text item delimiters to " "
set theTextItems to text items of theText
set AppleScript's text item delimiters to "%20"
set theText to theTextItems as string
set AppleScript's text item delimiters to {""}
theText

Cheers.

.or


set theText to "everybody is happy today"
set theText to do shell script "perl -e 'use URI::Escape; print uri_escape(\"" & theText & "\")';"

…or

set theText to quoted form of "everybody is happy today"
set theText to do shell script "echo " & theText & " | perl -pe 's/ /%20/g'"

This is really fine as it works not only with spaces but also with all other characters as @, &, %, to get URL safe enconding.
Thanks.