Replace : with / in script

I’m trying to set the path of a folder, and then return the result of that path in a shell script. Problem is that the shell script doesn’t like the colon “:”.

I want my path to be HD/Users/myname/path/to/folder
instead of HD:Users:myname:path:to:folder

How would I go about replacing the “:” in the path with a “/”.


property ChooseFolderPrompt : "Please choose a folder"

choose folder with prompt ChooseFolderPrompt
set the message_text to the result
set the message_text to replace_chars(message_text, ":", "/")
on replace_chars(this_text, search_string, replacement_string)
	set AppleScript's text item delimiters to the search_string
	set the item_list to every text item of this_text
	set AppleScript's text item delimiters to the replacement_string
	set this_text to the item_list as string
	set AppleScript's text item delimiters to ""
	return this_text
	set this_text to TargetFolder
end replace_chars
-- ETC.
-- ETC.
set this_data to do shell script "shell command goes here...  " & " " & TargetFolder

I keep getting the error "Can’t get every text item of “HD:Users:myname:path:to:folder:”
TIA

This is because message_text is not a text, but an alias:

choose folder
--> alias "path:to:folder:"

You should set up this line:

set the message_text to the result

To:

set the message_text to the result as text

Anyway, standard additions provides a quickest way to do this task:

POSIX path of alias "path:to:folder"
--> "/path/to/folder/"

Also, to make a path more shell-suitable, you can also:

quoted form of POSIX path of alias "path:to:folder:"
--> "'/path/to/folder with spaces in its name/'"

Thanks for the reply jj.

I’m very new to AppleScript (piecing this one together to learn), so it’s easy for me to admit that you tatally lost me regarding standard additions.

How would I write that into my script?

Thanks again

Try this:

property ChooseFolderPrompt : "Please choose a folder"
set TargetFolder to quoted form of POSIX path of (choose folder with prompt ChooseFolderPrompt)
set this_data to do shell script "shell command goes here...  " & " " & TargetFolder

– Rob

Thanks Rob! That works.
You couldn’t make it any easier, could you? :slight_smile:

What if somebody want’s to use a folder with an " ’ " or a space, such as “Martin’s Folder”?

Much appreciated!

Glad it helped. JJ provided the pertinent info - I just spliced it to form a single command. :slight_smile:

When you use ‘quoted form’, it escapes spaces in paths.

– Rob

[quote=“Rob”]

It did :slight_smile:

I edited my post because of an error using apostrophes in the file name. I guess you were too quick for me.

Thanks!

I’m not getting a proper result because of an apostrophe in the folder name. How would I correct that (without replacing the apostrophe)?

I’m not sure how to escape an apostrophe for the shell.

– Rob

To escape an apostrophe, use two backslashes in your applescript…

do shell script "ls /path/to/Martin\'s\ Folder"

Note that there is a space after the last backslash before Folder and that there is no space before the apostrophe.
Or you can use the “quoted form” as JJ and Rob mentioned above.

My apologies. When I was testing it with Script Editor open it looked like I wasn’t getting proper results. But it’s working.

… probably just need sleep … :).

Thanks!