Trim escape characters from file path

I generate file paths for use in emails by dragging the folder/file in question into Terminal.app then copying & pasting the generated text into an email text.

The copied path with escape sequences:

pCloud\ Drive/SEN\ pCloud/SEN\ Tech\ Team/Clean\ Energy\ WA\ Study\2016/For\ checking/AB\ checks/SEN\ RE\ SWIS\ 2030\ notes\ for\ AB\checks\ -\ Backup\ input\ table\ tab\ \(5.1.16\).docx

needs to become like this:

pCloud Drive/SEN pCloud/SEN Tech Team/Clean Energy WA Study 2016/For checking/AB checks/SEN RE SWIS 2030 notes for AB\checks - Backup input table tab \(5.1.16\).docx

I am looking for a quick way to zap all the occurrences of backslash character escaping the space character. Someone mentioned a Finder trick for doing that, but now I've forgotten it.

I tried to create a Finder Service to do a simple Find & Replace in Automator but couldn't get it done. Automator will allow me to make it appear in Applications under Services (although on my current macOS I don't seem to see that menu, maybe I need to do something to enable it).

How do I accomplish this?


Solution 1:

You can drag into TextEdit instead of Terminal. TextEdit won't add backslashes to the text.

You can still use Terminal, though. If you type echo, then drag and drop into Terminal, and then hit enter, the backslashes will be removed (actually, they're there precisely to allow terminal commands to handle the raw input -- echo doesn't see backslashes, but a single argument containing space characters).

$ echo pCloud\ Drive/SEN\ pCloud/SEN\ Tech\ Team/Clean\ Energy\ WA\ Study\2016/For\ checking/AB\ checks/SEN\ RE\ SWIS\ 2030\ notes\ for\ AB\checks\ -\ Backup\ input\ table\ tab\ \(5.1.16\).docx
pCloud Drive/SEN pCloud/SEN Tech Team/Clean Energy WA Study2016/For checking/AB checks/SEN RE SWIS 2030 notes for ABchecks - Backup input table tab (5.1.16).docx

Furthermore, you can automate the copying of the text by piping to pbcopy:

$ echo pCloud\ Drive/SEN\ pCloud/SEN\ Tech\ Team/Clean\ Energy\ WA\ Study\2016/For\ checking/AB\ checks/SEN\ RE\ SWIS\ 2030\ notes\ for\ AB\checks\ -\ Backup\ input\ table\ tab\ \(5.1.16\).docx | pbcopy

Now the desired text will be on your clipboard.

Solution 2:

  • Select a file with a right click
  • Hold down the Option key
  • Select the menu item Copy "filename" as Pathname

Note: I believe this only works in El Capitan and above.

Solution 3:

A simple approach using Automator service:

  1. Launch Automator.app.

  2. In the Menu bar, go to File → New → Service.

  3. In the editor area, set options as:

    • Service receives selected: text

    • in: any application

    • Output replaces selected text: select check-mark

  4. In the right sidebar, select Library → Utilities → Run Shell Script action.

  5. Drag & drop the action into editor area.

  6. Under **Run Shell Script*, set options as:

    • Shell: /bin/bash

    • Pass input: to stdin

  7. Under the Run Shell Script editor area, type the following sed command-line:

    sed 's/\\\ /\ /g'

    Note that the script also contain spaces characters. Advisable to edit this post and copy the script as is.

  8. In the Automator menu, click File → Save... and save the service with a suitable name.

  9. Now whenever you will select the text in any app and right-click, you can see the service under Service context menu item.

  10. Upon invoking the service, all the occurrences of backslash followed by space character will be replaced by just the space character.