Mac Contextual Menu for AFP Path Without Spaces

Hi yall,

My team requires an Automator service that will allow us to copy file paths from within OS X Finder such that they always start with “afp://server-name/” instead of “/Volumes/server-name/” BUT ALSO replaces all spaces with “%20”.

Right now, we are using the following code, which does replace the first space in a given string, but does not replace all spaces.

on run {input, parameters}
    set output to {}
    repeat with f in input
    set end of output to replace(POSIX path of f, "/Volumes/Brand Design", "afp://nycp-afp01/Brand%20Design")
    end repeat
    set text item delimiters to linefeed
    set the clipboard to (output as text)
end run
on replace(input, search, replace)
    set text item delimiters to search
    set ti to text items of input
    set text item delimiters to replace
    ti as text
end replace

Any help you can provide will be HUGELY appreciated; thanx!

A.

Browser: Safari 603.2.4
Operating System: Mac OS X (10.10)

Your replace method needed a little fix, text item delimiters are sensitive. It’s common practice to save the current one and put it back after use to not derail other scripts.

Try this:

on run {input, parameters}
	set OldTID to text item delimiters
	set output to {}
	repeat with f in input
		set end of output to my replace((POSIX path of f) as string, "/Volumes/Brand Design", "afp://nycp-afp01/Brand%20Design")
	end repeat
	set text item delimiters to linefeed
	set the clipboard to (output as string)
	set text item delimiters to OldTID
end run

on replace(stringToSearchAndReplace, theSearchString, theReplaceString)
	set returnString to stringToSearchAndReplace
	set oldASTID to AppleScript's text item delimiters
	
	set AppleScript's text item delimiters to theSearchString
	set tempString to text items of stringToSearchAndReplace
	
	set AppleScript's text item delimiters to theReplaceString
	set returnString to tempString as string
	
	set AppleScript's text item delimiters to oldASTID
	
	return returnString
end replace

Browser: Safari 603.2.4
Operating System: Mac OS X (10.10)

Thank you! I’ll review.

In the meantime, might this work?

on run {input, parameters}
    set output to {}
    repeat with f in input
        set f to replace(POSIX path of f, "Volumes/Brand Design", "afp://nycp-afp01/Brand Design")
        set f to replace(f, " ", "%20")
        set end of output to f
    end repeat
    set text item delimiters to linefeed
    set the clipboard to (output as text)
end run
on replace(input, search, replace)
    set text item delimiters to search
    set ti to text items of input
    set text item delimiters to replace
    ti as text
end replace

A.

Model: MBP 10.12.5
Browser: Safari 603.2.4
Operating System: Mac OS X (10.10)

It has the same general issues as with the first script. Try mine and see if it helps. If there’s still some issues, I’ll help you with those.