0

I need to do a lot of copying filepaths written by people using a PC (which uses backslashes) to a Mac (which uses forward slashes), and I'm tired of manually switching them every time. I have to copy the filepath from our shared web browser, paste it into my file browser, and then go in and manually remove the backslash and replace it with a forward slash.

That last part is really what's killing me since basically every filepath I copy has several backslashes in it that I want to paste! What I want is something that will modify the contents of my clipboard so I can just paste it into my Mac file browser without having to manually replace the slashes every time.

In short, I want any "\" in my clipboard to change to a "/" before I paste it. Can anyone please help me with this?

2 Answers 2

1

Using an Automator Service and AppleScript code, here are two examples of how you can change backslashes to slashes in a string.

The first one takes the selected text passed to the Automator Service, makes the replacements and sets the output to the Clipboard. Which from there you can manually paste wherever you desire that can accept pasted text.

The second one takes the selected text passed to the Automator Service, makes the replacements and then replaces the selected text passed with the modified text in place, that is providing the source selected text is editable.

There is a slight code difference between the two, in the on run subroutine and a single setting difference between the services. Both require first selecting a text string and then either a right-click to select the service from the Context menu or accessing the service from the Application_Name > Services > menu. The on replaceText(find, replace, textString) subroutine is the same in both examples.

AppleScript code for the Clipboard method service:

on run {input}
    set the clipboard to replaceText("\\", "/", input as string)
end run

on replaceText(find, replace, textString)
    set prevTIDs to AppleScript's text item delimiters
    set AppleScript's text item delimiters to find
    set textString to text items of textString
    set AppleScript's text item delimiters to replace
    set textString to "" & textString
    set AppleScript's text item delimiters to prevTIDs
    return textString
end replaceText

AppleScript code for the replace selected text method service:

on run {input}
    set input to replaceText("\\", "/", input as string)
    return input
end run

on replaceText(find, replace, textString)
    set prevTIDs to AppleScript's text item delimiters
    set AppleScript's text item delimiters to find
    set textString to text items of textString
    set AppleScript's text item delimiters to replace
    set textString to "" & textString
    set AppleScript's text item delimiters to prevTIDs
    return textString
end replaceText

To use the AppleScript code in an Automator Service:

  • In Automator, create a new Service with Service receives selected text in any application and Input is entire selection, then either leaving unchecked the [] Output replaces selected text check box for the clipboard service (AppleScript code for the Clipboard method service:) or checking the [√] Output replaces selected text check box for the for the replace selected text service (AppleScript code for the replace selected text method service:)

  • Add a Run AppleScript action to it and delete the default code.

  • Copy and paste whichever code for the method you choose to use.

  • Save the Service with an appropriate name, e.g., Replace backslashes with slashes on Clipboard for the first one and or Replace backslashes with slashes in selected text for the second one. Or of course whatever you prefer.

Now select some text that has backslashes in it and then select the service's name from the Services Context menu (right-click) or the Application_Name > Services > menu.

If using the Clipboard method, then paste the now modified selected text wherever you want or if using the replace selected text method, it's already replaced having selected that Service.

-1
  1. Keep the Windows quoted path entries into a plain text, name it to paths.txt, such as

     "C:\Program Files\xyz"
     "C:\Windows\System32"
    
  2. Then do it in Terminal:

    cat paths.txt | sed -e 's#\\#\/#g' > converted.txt
    
  3. The result such as

    "C:/Program Files/xyz"
    "C:/Windows/System32"
    

    if you want to get posix paths, try

    cat paths.txt | sed -e 's#^\"\([a-zA-Z]\):#\"\/\1#' -e 's#\\#\/#g' > converted.txt
    

    the converted result will be:

    "/C/Program Files/xyz"
    "/C/Windows/System32"
    

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .