replacing spaces with %20

Ok, I’ve trying to make a link on our server that I could send via iChat and that other people on the same network already connected to our server could just click and have that file or folder selected.

Here’s what I’ve got now:
tell application “Finder”
set thePath to (POSIX path of (the selection as alias)) as text
set the clipboard to “file://” & thePath
end tell

This works part of the way since if there is a space, it will stop at that space in the path. I want to keep the international characters though since i’ve tried with the shell script sed and it transforms characters which renders the path useless.

If I run the script above and just change the space to %20 in ichat, it works, but it’s a pain and I want to be able to do a service in SL so that everyone can use it.

I’m trying to mimic the service that was in Leopard but now doesn’t work in SL “Copy selected file in Textedit” or something like that.

I could do it in Automator without using the finder too, which would even be better as some user (like me) are using PathFinder.

TIA!
Jeff

Didn’t really take a look at what you are trying to do. but don’t you just want.

tell application "Finder"
	set x to choose file
	set y to x's URL
end tell

Hi,

this should be sufficient


tell application "Finder" to set thePath to URL of item 1 of (get selection)
set the clipboard to thePath

OK, I should have mentioned this in my earlier post, the get URL thing doesn’t work. First it puts a localhost at the beginning, and converts accented letters to their ASCII counterpart, so I need something that converts only the spaces to %20 and puts a File:// before the path that starts with /Volumes/…

Thanks for you quick response thou, that’s impressive:-)

Jeff

Maybe something like this:

set myString to "/Volumes/A Hard Drive/"

set newString to {}
repeat with i in myString
	if (i as string) is " " then
		set end of newString to "%20"
	else
		set end of newString to (i as string)
	end if
end repeat

newString as string

Hope it helps;
ief2

or this


tell application "Finder" to set thePath to POSIX path of (item 1 of (get selection) as text)
set the clipboard to "file://" & (do shell script "perl -e 'use URI::Escape; print uri_escape(\"" & thePath & "\", \"\\x20\")';")

ief2,

Your’s is the winner:-)

It works great for what I need. Here’s the final version I’m going to put in a Automator Service so I can assigne a keystroke to it.

tell application “Finder”
set myString to (POSIX path of (the selection as alias)) as text

set newString to {}
repeat with i in myString
	if (i as string) is " " then
		set end of newString to "%20"
	else
		set end of newString to (i as string)
	end if
end repeat

newString as string
set the clipboard to "file://" & newString

end tell

Now if I could only be able to do the same thing from Path finder and from Launchbar. Path finder does’t have the same therminology than the finder, and Launchbar, I have to modify it to some kind of handler, which I’m not sure how to.

Thanks again!
Jeff

A string is just a default class of AppleScript, so it should work in every script. So this would be a good handler to use:

replaceChars("/Volumes/A Hard Drive", space, "%20")

on replaceChars(aString, charToReplace, newChar)
	set newString to {}
	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 (newString as string)
end replaceChars

Hope it helps,
ief2

Here is an example.

This approach is proportionally slow for long strings.
The “classical” way with text item delimiters is faster


replaceChars("/Volumes/A Hard Drive", space, "%20")

on replaceChars(aString, charToReplace, newChar)
	set {TID, text item delimiters} to {text item delimiters, charToReplace}
	set aString to text items of aString
	set text item delimiters to newChar
	set aString to aString as text
	set text item delimiters to TID
	return aString
end replaceChars

thanks guys, I have now a bigger problem.

The Path I am getting with the script works fine with SL clients, but doesn’t work for Leopard Clients.

If I use the Service from TextEdit, New window containing selection, in Leopard. I get a link in TextEdit that looks like this: /Volumes/Serveur/Dossiers Partagés. If I look at the link, it looks like file://localhost/Volumes/Serveur/Dossiers Partagés/

If I try to use this form in SL and paste it in iChat to send to a Leopard client, it shows a link that’s underlined blue from file: and stops at the first space in the name. If I replace the space with %20, as I do in my previous script and add the localhost part, it doesn’t work either. So, what changed in SL, compared to Leopard when it comes to how it handles path?

If I do a Reveal on a path that looks like /Volumes/Serveur/Dossiers Partagés, it works. I’d like to have a clickable link that works on both Leopard and SL, like it did before.

I Know it’s not really the place to ask this, but since you guys are pretty much mac geeks;-) it’s worth a shot.

Jeff