Script that outputs file path like Terminal would

Hi there.

I can’t seem to get the output right no matter what I try so I though I’d ask here.
I’m trying to make applescript output the file path exactly as it would appear in Terminal because I actually need to pass it on to Terminal.

Most scripts I have come across do not include the necessary backslash to escape spaces. Like so:

What I really want is for the selected file in Finder to have its path printed like this:

so that Terminal can use it without fuss.

Any help would be appreciated.

Hi,

just use


quoted form of "/Volumes/Macbook/Public/Drop Box/some file.txt"

AppleScript will add the correct quotation

Thanks Stefan but I’m not in need of quotation. I actually need the proper backslashes that Terminal needs to interpret spaces in filenames.

So a script like

tell application "Finder"
	set currentPath to (POSIX path of (target of front window as alias))
	set the clipboard to currentPath
end tell

Gives me:

But what I really want is:

(Note the backslash)

Hi,

If you’re sending the posix path from an AppleScript, then it has double quotes. So to use a literal backslash in your AppleScirpt, you need to quote the quote:

"/Volumes/Daniel/Public/Drop\\ Box/"

This gives you a literal backslash I think.
Edited: meant to say escape. :slight_smile:

gl,
kel

Like this:

tell application "Finder"
	set currentPath to (POSIX path of (target of front window as alias))
	--set the clipboard to currentPath
end tell
set tids to AppleScript's text item delimiters
set AppleScript's text item delimiters to space
set temp_list to text items of currentPath
set AppleScript's text item delimiters to "\\ "
set currentPath to temp_list as string
set AppleScript's text item delimiters to tids
set the clipboard to currentPath

That works. Much obliged.

escaping space characters with backslashes and quotation are two different ways for exactly the same purpose

The clipboard is probably not needed.

do shell script

can execute shell commands and Terminal.app is scriptable

A bit ham-fisted, but effective. And easily modified to include any additional characters that may have been overlooked but need to be “escaped”.



on tEsc(POSIXpath)
	try
		--the list of characters that will be escaped; the backslash character must be checked first
		--this list was compiled via LIMITED trial and error; may need to be appended.
		set charsToEscape to "\\ ~`!#$%&*()=<>,;'{}[]|" & quote
		
		set {TID, AppleScript's text item delimiters} to {AppleScript's text item delimiters, ""}
		set theString to POSIXpath as string
		
		--iterate through the list of characters to be escaped
		repeat with theChar in charsToEscape
			--if the input string contains this character
			if theString contains theChar then
				--replace every occurence of the character with the escaped version
				set AppleScript's text item delimiters to theChar
				set theList to every text item of theString
				set AppleScript's text item delimiters to "\\" & theChar
				set theString to theList as text
			end if
		end repeat
	on error errMsg number errNum
		set AppleScript's text item delimiters to TID
		return ""
	end try
	set AppleScript's text item delimiters to TID
	return theString
end tEsc

Stefan is right. You do need quotation. Single quotes turn substitution off (read manual of bash for more information) meaning: there is not a character with a special meaning, including the single quote.

Here an example:

do shell script "echo \"current user's $HOME\"" --substitution off (except for bash variable); wrong
do shell script "echo 'current user'\\''s $HOME'" --same as quoted form; the correct way

Trying to escape characters yourself is a no-go. Terminal works with quoted form as well, so it’s still “script that outputs file path like Terminal would”.

Hello.

I have a faint memory of once constructing something, so that the quoted path didn’t work.

I can’t remember for the life of me how I did it, but I’m stating still that it is possible.

escapeSpacesInPxPath()
on escapeSpacesInPxPath()
	try
		tell application id ¬
			"MACS" to set pxPath to ¬
			POSIX path of ¬
			(target of its front Finder window as alias)
	on error
		display alert "No valid path in Front Finder window"
		return
	end try
	tell (a reference to AppleScript's text item delimiters)
		local tids
		set {tids, contents of it} to {contents of it, space}
		set {pxPath, contents of it} to {text items of pxPath, "\\ "}
		set {pxPath, contents of it} to {pxPath as text, tids}
	end tell
	set the clipboard to pxPath
end escapeSpacesInPxPath


Here is a handler that escapes spaces and spaces only in a posix path. I though’t I’d come up with something much cleverer than this, but it appears that the on error trick only works with stuff that came onto the clipboard by cmd - c.
That is, that is the only time the string with the error is escaped. :confused:

Nope, we have had this discussion before and quoted form of always works. It was an discussion with you, me and Stefan before. I remember too and you couldn’t remember how you did it then as well :smiley: .

For one thing, the quoted form is hard to understand when there are single quotes in the string. e.g.

set f to choose folder
set pp to quoted form of (POSIX path of f)
--> "'/Users/kelhome/Desktop/Bob'\\''s Folder/'"

Maybe I screwed up something else. :wink: It was actually when I tried to cd to a variable with a do shell script. I fumbled with it then, some years ago…