Stripping unwanted chars from URL

I have an appscript that takes user input, puts it together with a saved user preference and other coded bits to get a proper URL, for example

ftp://server.com/path/toDir/toFile

but because of what some users might enter (even when instructed not to :slight_smile: the result can end up as

ftp://server.com/path//toDir/toFile

–>How do I strip out unnecessary “//” without killing the necessary one?

set badURL to "ftp://server.com/path//toDir/toFile"

set AppleScript's text item delimiters to "//"
set badURL to badURL's text items
set AppleScript's text item delimiters to "/"
set goodURL to badURL's item 1 & "//" & rest of badURL as string
set AppleScript's text item delimiters to {""}

goodURL

TIDs are our friends! :lol:

Works perfect. I’ve put off learning this kind of code as I’ve always found text and string handling overly cryptic (don’t even talk about regexp). This, however, is clever enough it makes sense to me. Thanks again.

g.

If there’s one thing to learn about handling text that you shouldn’t put off, it’s this (using text item delimiters). It’s one of the most powerful and overlooked tools available to scripters. :slight_smile:

Allow me to interject: here here!

Give it up for text item delimiters! wOOt!