Encoding a URL?

I feel like this should have been easy to find, but I have searched and searched macscripter.net and these forums without luck. Is there an easy way in AppleScript, perhaps with a scripting addition (Mac OS X addition) to encode a URL? For example to turn the following:

http://some.web.site/my page.html

into:

http://some.web.site/my%20page.html

Steve

Hi,

Here’s a basic replace text.

set the_text to "http://some.web.site/my page.htm"
set def_tid to AppleScript's text item delimiters
set AppleScript's text item delimiters to {space}
set text_items to (text items of the_text)
set AppleScript's text item delimiters to {"%20"}
set new_text to text_items as string
set AppleScript's text item delimiters to def_tid
return new_text

You can search for replacing text and there should be ready made, more complicated subroutines at this site.

gl,

I’ve yet to see anyone produce a vanilla AS solution that encodes URLs correctly, and frankly it’s not worth the effort when a simple ‘do shell script’ will do the job:

set txt to "my page.html"
do shell script "python -c 'import sys, urllib; print urllib.quote(sys.argv[1])' " & quoted form of txt
--> "my%20page.html"

Alternatively, see the ‘encode URL’ command in TextCommands.

HTH