Topics

Automator find and replace text string

Alastair Leith
 

Is there a basic find and replace text function in Automator? I can only find one for file name strings to use in the Finder (And I don’t want to change any file names, just copy their paths and display in a modified way.


Background

I often create file path strings for email messages to my team by drawing a file from Finder into terminal and then that resulting text into the Mail body text and Subject line.

E.g.
/Users/aleith/pCloud\ Drive/Screenshots/reseeding\ coral\ on\ GBR.jpg 

Then to be kind to non-techies I often replace all escaped characters will just the character and directory symbols “ ➤ “ with and arrow “➤” or something.

➤ Users ➤ aleith ➤ pCloud Drive ➤ Screenshots ➤ reseeding coral on GBR.jpg 

Apart from fact that Find and Replace in Mail is buggy as hell (only thing that works properly is ‘Replace All', which sometimes is the last thing I want to do as in example above), I figured why not make it all a contextual menu service in Finder so I select the files(s) and it generates a text string which then is copied to the clipboard.

I know I can write an AppleScript to do text substitution and copy to Clipboard, and then make that an Automator service, but surprised me I couldn’t find anything in Automator itself other than the Rename Finder Item, the only action that comes up when I search for “replace”.

Shane Stanley
 

On 17 Jun 2018, at 10:23 pm, Jean-Christophe Helary <brandelune@...> wrote:

I don't think I'd use AS for that. I'd use [Run shell script] and do a sed replace on the string you get.

Seems like a bit of overkill for a simple literal replace.

tell application "Finder" to set finderSelectionList to selection as alias list
if length of finderSelectionList = 0 then error "No files were selected in the Finder!"
set thePath to POSIX path of item 1 of finderSelectionList
set saveTID to AppleScript's text item delimiters
set AppleScript's text item delimiters to {"/"}
set thePath to text items of thePath
set AppleScript's text item delimiters to {" "}
set thePath to thePath as text
set AppleScript's text item delimiters to saveTID
set the clipboard to thePath


Alastair Leith
 

Thanks Steve! So cool, no escape chars either.

On 17 Jun 2018, at 10:28 pm, Steve Mills <sjmills@...> wrote:

On Jun 17, 2018, at 06:41, Alastair Leith <qc.student.au@...> wrote:

I often create file path strings for email messages to my team by drawing a file from Finder into terminal and then that resulting text into the Mail body text and Subject line.
BTW, at some point recently, Apple added command-option-c (IIRC) to Finder’s Edit menu to copy the path[s] of the selection, so you can save a big step here.

Steve via iPad

2551phil
 

On 17 Jun 2018, at 21:28, Steve Mills <sjmills@...> wrote:

at some point recently, Apple added command-option-c


That appeared in El Capitan.

FWIW, aside from the keyboard shortcut and the Edit view menu already mentioned, it is also available in a Finder window if you invoke the contextual menu on a selected item and hold down the option key.


Best


Phil
@sqwarq

Alastair Leith
 

Much obliged, Shane. Thx.

On 17 Jun 2018, at 8:34 pm, Shane Stanley <sstanley@...> wrote:

On 17 Jun 2018, at 10:23 pm, Jean-Christophe Helary <brandelune@...> wrote:

I don't think I'd use AS for that. I'd use [Run shell script] and do a sed replace on the string you get.

Seems like a bit of overkill for a simple literal replace.

tell application "Finder" to set finderSelectionList to selection as alias list
if length of finderSelectionList = 0 then error "No files were selected in the Finder!"
set thePath to POSIX path of item 1 of finderSelectionList
set saveTID to AppleScript's text item delimiters
set AppleScript's text item delimiters to {"/"}
set thePath to text items of thePath
set AppleScript's text item delimiters to {" "}
set thePath to thePath as text
set AppleScript's text item delimiters to saveTID
set the clipboard to thePath



Shane Stanley
 

On 18 Jun 2018, at 12:29 am, Alastair Leith <qc.student.au@...> wrote:

no escape chars either
Escaping/quoting is only required for the shell, so there's no reason for it to appear anywhere other than Terminal, bash scripts, and do shell script.

--
Shane Stanley <sstanley@...>
<www.macosxautomation.com/applescript/apps/>, <latenightsw.com>