Replacing a portion of a string on the fly

I am trying to put together a script to open a folder based on a path from the clipboard. Thanks to poking around in the MacScripter forums, I found a script by Sprale that I was able to adapt to my purposes for mac paths.

try
	set userInput to the clipboard
	set userInputGood to POSIX file userInput as alias
on error
	display dialog "D'oh!" buttons {"Cancel"} default button 1
end try

tell application "Finder"
	activate
	set newWindow to make new Finder window
	set target of newWindow to userInputGood
end tell

It works great for paths formatted by mac’s like this:

/Volumes/THEVAULT/Projects/Open/AZSQC01390_XXXXXX_C_Managed Mkts_Employer Calculator/

but no so fantastic with paths the pc people send us:

V:/Projects/Open/AZSQC01390_XXXXXX_C_Managed Mkts_Employer Calculator/

in this case “V:” from a PC is always referring to what the mac reads as “/Volumes/THEVAULT”

I would ideally like to just do a conditional statement where if the string begins with “V:” delete “V:” and concatonate the path to “/Volumes/THEVAULT” but I think I am in over my head. I could not seem to get the conditional statement or the concatonation to work.

Is it delimiters I should be using?

Thanks for your advice!

Model: Dual 2 Ghz PowerPC g5
AppleScript: 1.10.7
Browser: Firefox 2.0.0.8
Operating System: Mac OS X (10.4)

Hi,

try this:

try
	set userInput to the clipboard
	if userInput starts with "V:" then set userInput to text 3 thru -1 of userInput
	tell application "Finder" to open (POSIX file userInput as alias)
on error
	display dialog "D'oh!" buttons {"Cancel"} default button 1
end try

Note: Just opening the folder does the same as creating a Finder window

Thanks so much! That was the tip I needed!!!

I still needed to add on “/Volumed/THEVAULT” to the begining of the string, but I got it to work:

try
	set userInput to the clipboard
	if userInput starts with "V:" then
		set userInput to text 3 thru -1 of userInput
		set userInput to "/Volumes/THEVAULT" & userInput
	end if
	tell application "Finder" to open (POSIX file userInput as alias)
on error
	display dialog "D'oh!" buttons {"Cancel"} default button 1
end try

Thanks so much StefanK!

I seem to have come upon some bug in my code, sometimes a path like this:

V:\Projects\Open\AZSQC01357_2007 consumer acq\

is being recognized as this by the applescript:

“V:\Projects\Open\AZSQC01357_2007 consumer acq\”

or at least it’s not working, and this is how it shows up in the event log… Anyone have a suggestion of how I can correct and issue like this?
could I make a conditional statement


if userInput contains "//" then
magically transform "//" to "/"

Thanks again for taking the time to read this.

magically works with text item delimiters


try
	set userInput to the clipboard
	if userInput starts with "V:" then
		set {TID, text item delimiters} to {text item delimiters, "\\"}
		set userInput to text items of userInput
		set text item delimiters to "/"
		set userInput to userInput as text
		set text item delimiters to TID
		set userInput to "/Volumes/THEVAULT" & text 3 thru -1 of userInput
	end if
	tell application "Finder" to open (POSIX file userInput as alias)
on error
	display dialog "D'oh!" buttons {"Cancel"} default button 1
end try

Thanks Stefan!

So I can use delimiters to replace any part of a string with something else, so long as I am aware that every instance will be swapped?

exactly.
But consider to escape some special characters (with a leading backslash) for example backslash and quote

Thank you very much Stefan! I have learned a lot today!

~John