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.
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:-)
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
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\")';")
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.
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
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
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?