How do I replace all backslashes with colons in a string?

Hi,

I’m new to Applescript (but not programming - most recently PHP a couple of years ago). I’m reading furiously, but can’t see how to do the following (I tried searching the forums but didn’t come up with anything):

I’m trying to convert a whole lot of directory paths formatted for Windows into a Mac equivalent. I have a script that takes care of replacing the first part of the path string with the Mac equivalent, but I am trying to replace all occurrences of a Windows backslash with a Mac colon in the remainder of the path string. This is part of a migration of digital photographs from a PC to my new Mac.

In my script so far, the string “thePath” contains a file path with the first part replaced with the top level of the Mac path. It still has backslashes separating the subsequent folders down to the file name itself. The number of backslashes varies.

I’m sure this is easy, and I’ll slap my forehead when I see the answer, but while I come up to speed any help will be much appreciated. When my migration is complete I plan to take an axe to my PC :slight_smile: .

Sam McMillan.

Model: iMac
AppleScript: 2.2
Browser: Safari 523.10
Operating System: Mac OS X (10.5)

To be answered by someone who knows what a PC path looks like. Give us a sample, if no one answers soon.

In general terms, I use this (due to Nigel Garvey):

set str to "this has \\ in it" -- note the excaped back slash

set newStr to findandreplace("\\", ":", str) -- again, the backslash is escaped

on findandreplace(toFind, replaceWith, theText)
	set astid to AppleScript's text item delimiters
	set AppleScript's text item delimiters to toFind
	set textItems to theText's text items
	set AppleScript's text item delimiters to replaceWith
	tell textItems to set editedText to beginning & replaceWith & rest
	set AppleScript's text item delimiters to astid
	return editedText
end findandreplace

newStr --> "this has : in it"

Welcome. :slight_smile:

Try something like this:

set example to "path\\path"
set example to replaceText("\\", ":", example)

-- find    : Text to be found
-- replace : Text to replace with
-- subject : Text to be searched
on replaceText(find, replace, subject)
	set prevTIDs to AppleScript's text item delimiters
	set AppleScript's text item delimiters to find
	set subject to text items of subject
	
	set AppleScript's text item delimiters to replace
	set subject to "" & subject
	set AppleScript's text item delimiters to prevTIDs
	
	return subject
end replaceText

Adam - typically this is the string I’m trying to convert:

folder1\folder2\folder3\folder4\file

which needs to end up as:

folder1:folder2:folder3:folder4:file

Thanks to you and Bruce for such speedy responses.

Sam.

I’m not sure we’ve helped – AppleScript won’t buy the string as it is, it has to have each of the backslashes (an escape character in AppleScript) itself escaped – you won’t be able to read them in as they are. If you’ve got to escape every one, you might just as well replace it with a colon.