Capture Server Path for Emailing link...help with Find and Replace

Dear All,

I am totally new to Applescript, but not to google and commonsense. So I have searched and scoured and managed to put together the following script that I am perversely proud of.



on run
	tell application "Finder" to set sel to selection
	open sel
end run
on open theseItems
	
	## This Section Creates the Mac file path
	
	set macFilePath to {}
	repeat with oneFile in theseItems
		set text item delimiters to ":"
		set filePath to text items of (oneFile as text)
		set text item delimiters to "/"
		set end of macFilePath to filePath as text
	end repeat
	set macFilePath1 to macFilePath
	set macFilePath to macFilePath1 as text
	
	## This Section Creates the windows file path
	
	set winFilePath to {}
	repeat with oneFile in theseItems
		set text item delimiters to ":"
		set filePath to text items of (oneFile as text)
		set text item delimiters to "\\"
		set end of winFilePath to filePath as text
	end repeat
	set winFilePath1 to winFilePath
	set winFilePath to winFilePath1 as text
		
	set the clipboard to "******************************************" & return & "Mac Path: " & "afp://fileserver/" & macFilePath1 & return & return & "If the link is broken, copy (apple+c) and paste (apple+v) whole link into Connect to Server (apple + k) " & return & return & "******************************************" & return & "Windows Path: " & "\\\\fileserver\\" & winFilePath1 & return & return & "If the link is broken, copy (ctrl+c) and paste (ctrl+v) whole link into Windows Explorer " & return & return & "******************************************"
end open

The script will output the following to the clipboard:


Mac Path: afp://filesrver/CLIENT/PROJECTS/BLOG 10/DIGITAL ASSETS/created at office/Blog_400_370_Black_Rear_Flipped.psd

If the link is broken, copy (apple+c) and paste (apple+v) whole link into Connect to Server (apple + k)


Windows Path: \fileserver\CLIENT\PROJECTS\BLOG 10\DIGITAL ASSETS\created at office\Blog_400_370_Black_Rear_Flipped.psd

If the link is broken, copy (ctrl+c) and paste (ctrl+v) whole link into Windows Explorer


In the pursuit of perfection I would like to as part of the script for the find all the [spaces] in the files path and replace with %20 to the link remain intact even if there are spaces in the original path.

I have tried various google searches with varying degrees of success.

Any help will be appreciated.

Welcome on board.

Since you found the trick using AppleScript’s text item delimiters, I am surprised you got stuck with that replacement. Text item delimiters may have more than one character. The same code you use to replace : by / can be used for [space] and %20.

Two remarks: Changing text item delimiters changes the state of AppleScript. It is a good idea to store the original delimiters in a variable at the beginning and to restore them at the end. A simple Search and Replace function may help:


on SAR(main_text, search_text, replace_text)
	-- Search and Replace. Search for search-text in
	-- main_text and replace it by replace_text.
	set old_delims to AppleScript's text item delimiters
	try
		set AppleScript's text item delimiters to search_text
		considering case
			set parts to every text item of main_text
		end considering
		set AppleScript's text item delimiters to replace_text
		set newText to (parts as string)
	end try
	set AppleScript's text item delimiters to old_delims
	return newText
end SAR

There is are differences between replacing the colon in a Mac path by a slash and using POSIX path of.

Try the following code with some files (included mounted volumes) in the Script Editor:


set x to (choose file) as text
set p1 to SAR(x, ":", "/")
set p2 to POSIX path of x
{p1, p2}

Check, which result is better for your purpose.
Jürgen

Jürgen, thanks you solved it…



on run
	tell application "Finder" to set sel to selection
	open sel
end run

on SAR(main_text, search_text, replace_text)
	-- Search and Replace. Search for search-text in
	-- main_text and replace it by replace_text.
	set old_delims to AppleScript's text item delimiters
	try
		set AppleScript's text item delimiters to search_text
		considering case
			set parts to every text item of main_text
		end considering
		set AppleScript's text item delimiters to replace_text
		set newText to (parts as string)
	end try
	set AppleScript's text item delimiters to old_delims
	return newText
end SAR

on open theseItems
	
	## This Section Creates the Mac file path
	
	set macFilePath to {}
	repeat with oneFile in theseItems
		set text item delimiters to ":"
		set filePath to text items of (oneFile as text)
		set text item delimiters to "/"
		set end of macFilePath to filePath as text
	end repeat
	set macFilePath to macFilePath as text
	set macFilePath1 to SAR(macFilePath, " ", "%20")
	
	## This Section Creates the windows file path
	
	set winFilePath to {}
	repeat with oneFile in theseItems
		set text item delimiters to ":"
		set filePath to text items of (oneFile as text)
		set text item delimiters to "\\"
		set end of winFilePath to filePath as text
	end repeat
	set winFilePath to winFilePath as text
	set winFilePath1 to SAR(winFilePath, " ", "%20")
	
	set the clipboard to "******************************************" & return & "Mac Path: " & return & "afp://fs1/" & macFilePath1 & return & return & "If the link is broken, copy (apple+c) and paste (apple+v) whole link into Connect to Server (apple + k) " & return & return & "******************************************" & return & "Windows Path:& return " & "\\\\fs1\\" & winFilePath1 & return & return & "If the link is broken, copy (ctrl+c) and paste (ctrl+v) whole link into Windows Explorer " & return & return & "******************************************"
end open


This is excellent. Its helping me with an issue here. Thnx for sharing!!