3

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?

2
  • 2
    Is this a case of an XY Problem? Aug 10, 2018 at 13:07
  • Thanks for the comment, I probably do do that somethings but in this case I don't think so. Quote: "Someone mentioned a Finder trick for doing that, but now I've forgotten it." The correct answer mentioned exactly that. But other processes have their advantages, as other functions can be added to a Automator routine or perl script. Aug 12, 2018 at 16:15

3 Answers 3

2

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.

1
  • 1
    On a old version of OS X, I have a shell function fn2pb- filename to pasteboard, where the guts of the function are pbcopy <<<"$@". So I type fn2pb then drag a file onto the Terminal.app window.
    – fd0
    Aug 10, 2018 at 15:50
7
  • 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.

7
  • 1
    Excellent answer! I am aware of this technique, yet somehow it didn't occur to me :) I ended up learning about and writing an Automator workflow containing perl script to zap slashes. Was using a sword where a needle could do :-|
    – Nimesh Neema
    Aug 10, 2018 at 11:53
  • Thanks. Will do that :) Can you edit your answer to mention this constraint?
    – Nimesh Neema
    Aug 10, 2018 at 12:16
  • 1
    It works in El Capitan, so as of this answer, it works in all current versions of macOS.
    – Allan
    Aug 10, 2018 at 12:37
  • Thanks someone had told me that but I couldn't remember it! I was trying to paste into the Goto Folder command and things like that (which didn't help). Aug 12, 2018 at 16:05
  • 1
    @NimeshNeema Please link to the Automator flow or make a new answer here. I'd like to use it and add some other features (like swap "➝" for the folder delimiter character "/", change the font, size etc) and make it a Service I can invoke in Mail or Finder. Aug 12, 2018 at 16:13
2

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.

3
  • Yes this is my current method, but looking for something one-click because I use it so much. Aug 10, 2018 at 10:30
  • Suggested this as you are already copy pasting the text manually from Terminal. This approach indeed adds an additional hop.
    – Nimesh Neema
    Aug 10, 2018 at 10:34
  • Thx Nimesh. If I want to change is so that all escaped characters like say "\&" what would I change the sed script to? Is it using Grep, it seems to be something unlike Grep. Sep 1, 2018 at 6:44

You must log in to answer this question.

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