Change slashes and a letter in a text

Hello,

I have made a applescript which i am using in automator, which only gets the path of the item i have selected in the finder.

on run {input, parameters}

(* Your script goes here *)
tell application "Finder"
	set sel to the selection as text
	set the clipboard to POSIX path of sel
end tell

return input

end run

This gives a result like this “/Volumes/10.0.0.31/SCRATCH-Projects/”

This is fine, but i regularly needs to send the path to windows guys, so now i want to :

  • change the forward slashes to backslashes,
  • remove the /Volumes/
  • check if the first text is a certain path i.e. 10.0.0.31 or 10.0.0.4 and then change it to - O, W, X or another windows network drive letter.

Can anyone help me with this or do you have any tips?

Kind regards,
jklarsen

set ipList to {¬
	"10.0.0.31", ¬
	"10.0.0.4", ¬
	"10.0.0.13"}

set driveList to {¬
	"O", ¬
	"W", ¬
	"X"}


set theResult to "/Volumes/10.0.0.31/SCRATCH-Projects/"

set thePath to SaR(theResult, "/Volumes/", "")

set o to offset of "/" in thePath
set theIP to text 1 thru (o - 1) of thePath

if ipList contains theIP then
	repeat with i from 1 to count ipList
		if item i of ipList is theIP then
			set thePath to item i of driveList & ":" & text o thru -1 of thePath
		end if
	end repeat
end if

set thePath to SaR(thePath, "/", "\\")

on SaR(theData, srchText, replText)
	set {atid, AppleScript's text item delimiters} to {AppleScript's text item delimiters, srchText}
	set tempList to text items of theData
	set AppleScript's text item delimiters to {replText}
	set theData to tempList as string
	set AppleScript's text item delimiters to atid
	return theData
end SaR

Hello,

And thank you very very much!!!

I have adjusted the script so it now contains all my mounts and i have 2 problems.

The script now looks like this:


tell application "Finder"
	set sel to the selection as text
	set the clipboard to POSIX path of sel
	
end tell

set ipList to {¬
	"10.0.0.11", ¬
	"10.0.0.17", ¬
	"10.0.0.31", ¬
	"10.0.0.31-1", ¬
	"N-ext", ¬
	"junk", ¬
	"management", ¬
	"septimus"}

set driveList to {¬
	"X", ¬
	"O", ¬
	"W", ¬
	"Z", ¬
	"N", ¬
	"J", ¬
	"M", ¬
	"N"}

set theResult to sel

set thePath to SaR(theResult, "/Volumes/", "")

set o to offset of "/" in thePath
set theIP to text 1 thru (o - 1) of thePath

if ipList contains theIP then
	repeat with i from 1 to count ipList
		if item i of ipList is theIP then
			set thePath to item i of driveList & ":" & text o thru -1 of thePath
		end if
	end repeat
end if

set thePath to SaR(thePath, "/", "\\")

on SaR(theData, srchText, replText)
	set {atid, AppleScript's text item delimiters} to {AppleScript's text item delimiters, srchText}
	set tempList to text items of theData
	set AppleScript's text item delimiters to {replText}
	set theData to tempList as string
	set AppleScript's text item delimiters to atid
	return theData
end SaR

Generally it produces this result - “o:Scratch-projects:” which proves that i converts just fine, allthough the backslashes are now missing,
and when i choose the junk mount it doesn’t convert the “junk” name to a windows drive? it gives me this - “junk:aksel:”

Any help with finish this is VERY VERY appreciated!

Thanks,

Jklarsen

Hello,

Just wanted to post my solution which works perfectly.


tell application "Finder"
	set theResult to the selection as text
end tell


set thePath to SearchAndReplace(theResult, ":", "\\")

if thePath starts with "junk" then
	set thePath to text 5 thru -1 of thePath
	set thePath to "j:" & thePath
else if thePath starts with "management" then
	set thePath to text 11 thru -1 of thePath
	set thePath to "m:" & thePath
else if thePath starts with "o" then
	set thePath to text 2 thru -1 of thePath
	set thePath to "o:" & thePath
else if thePath starts with "W" then
	set thePath to text 2 thru -1 of thePath
	set thePath to "w:" & thePath
else if thePath starts with "x" then
	set thePath to text 2 thru -1 of thePath
	set thePath to "x:" & thePath
else if thePath starts with "n-ext" then
	set thePath to text 6 thru -1 of thePath
	set thePath to "n:" & thePath
else if thePath starts with "n" then
	set thePath to text 2 thru -1 of thePath
	set thePath to "n:" & thePath
end if


set the clipboard to thePath


on SearchAndReplace(theData, srchText, replText)
	set {atid, AppleScript's text item delimiters} to {AppleScript's text item delimiters, srchText}
	set tempList to text items of theData
	set AppleScript's text item delimiters to {replText}
	set theData to tempList as string
	set AppleScript's text item delimiters to atid
	set the clipboard to theData
	return theData
end SearchAndReplace

So thanks for the help!

Best…jklarsen