where do I start?

I’m totally new to AppleScript. The tutorials I’ve looked at online don’t directly help me with what I want to do. Buying a book to explaine it will take awile since I’m living in China.

I want to make a script that will do the following:

When I highlight a file in the Finder, I want a script that will create the path to that file and put it into the clipboard. I also need “%20” to replace any blank spaces that are in the path.

Model: PowerMac G5 Dual 2GHz
AppleScript: 2
Browser: Safari 312
Operating System: Mac OS X (10.3.9)

See if something like this works for you:

tell application "Finder"
	set thisItem to the selection as alias -->path to the selection in the frontmost Finder window
end tell
set AppleScript's text item delimiters to space
set pathParts to text items of (thisItem as text)
set AppleScript's text item delimiters to "%20"
set thePath to pathParts as text

Edit: oops! You wanted it on the clipboard:

tell application "Finder"
	set thisItem to the selection as alias -->path to the selection in the frontmost Finder window
	set AppleScript's text item delimiters to space
	set pathParts to text items of (thisItem as text)
	set AppleScript's text item delimiters to "%20"
	set thePath to pathParts as text
	set the clipboard to thePath
end tell

If you use System Events, you can get the path as a file URL which automatically converts spaces and other entities to their proper encoded values:

tell application "Finder" to set the_file to item 1 of (get selection)
tell application "System Events" to set the_URL to URL of (the_file as alias)
set the clipboard to the_URL

Jon

dkmarsh, i he wants spaces replaced with “20%”, i’m pretty sure he needs a POSIX path, so i edited your script to make it a POSIX path

tell application "Finder"
	set thisItem to POSIX path of (the selection as alias) -->path to the selection in the frontmost Finder window
	set AppleScript's text item delimiters to space
	set pathParts to text items of (thisItem as text)
	set AppleScript's text item delimiters to "%20"
	set thePath to pathParts as text
	set the clipboard to thePath
end tell

end tell

Thanks for pointing that out.

D’oh! Yes, he does. :rolleyes:

POSIX paths do not contain URL escape codes. Perhaps the OP could explain what he wants these paths for.

web browsers use POSIX paths and need the %20 escape

Thanks guys! It works great. However, I forgot to mention that at the beginning of the path, “file://” needs to be added.

For example when I ran LobsterMan’s script, here’s what it puts in the clipboard:

/Users/bob/Desktop/test%20file.rtf

I need it to be:

file:///Users/bob/Desktop/test%20file.rtf

In case you are wondering, I’m using this in Hog Bay Notebook. It allows you to put links to files, and I’m constantly having to type links. It takes for ever.

Thanks guys! It works great. However, I forgot to mention that at the beginning of the path, “file://” needs to be added.

For example when I ran LobsterMan’s script, here’s what it puts in the clipboard:

/Users/bob/Desktop/test%20file.rtf

I need it to be:

file:///Users/bob/Desktop/test%20file.rtf

In case you are wondering, I’m using this in Hog Bay Notebook. It allows you to put links to files, and I’m constantly having to type links. It takes for ever.

Hi baturjan,

Jon already posted how to get the local file url:

set f to choose file
tell application “System Events”
URL of f
end tell

gl,

thanks gl. I missed that.

Web browsers use URLs. You can get well-formed file URLs directly from the Finder or System Events, as other folks have shown.

HTH

What’s wrong with my simple script to emulate this advice? It returns this error:
The file /file/::localhost:Users:MyUsrName:Sites:WebFiles:ACB:index.html cannot be found.
I can see that the URL is mal-formed, but don’t know how to ask for what I want. [OS X 10.3.9]
Doesn’t work in Safari either, btw, so it’s not Camino.

set f to choose file
tell application "System Events" to set u to URL of f
tell application "Camino" to open u

The ‘open’ command requires a file object. Use ‘open location’ for URL strings.

Subtle. I was unaware of the differences, and vaguely surprised that it didn’t coerce.

‘open’ and ‘open location’ are two separate commands. The ‘open’ command is designed to open files. The ‘open location’ command is designed to open URLs. Coercion doesn’t come into it; it’s a matter of using the correct command for the job. (You can argue whether this particular arrangement is good design or bad, but either way it’s what we’re stuck with.)

HTH

Thanks. Understood. What I meant by coercion was that I could see no ambiguity in telling a browser to “open” a file given a URL, because most can’t open a file any other way. I’m not qualified to comment on good or bad design in AS. I just started writing scripts in February and its nuances often escape me.