I am working on a script to have Finder get a file's location locally, replace spaces with %20, then append localhost:// so the transformed file name can be inserted into email.

I've have used the replacing spaces code structure suggested elsewhere on stackoverflow but the code is coming up with error 1721. I am not sure what I've done wrong.

The code for text substitution is:

```
on run {input, parameters}
    set newString to {"localhost://"}
    set aString to "/file name input/"
    set aString to aString as text
    set charToReplace to " "
    set newChar to "%20"
    repeat with i in aString
        if (i as string) is charToReplace then
            set end of newString to newChar
        else
            set end of newString to (i as string)
        end if
    end repeat
    return input
end run

```

The output should be /file%20name%20input/

Thanks for any help you can offer.

Michael