Automator replacing character with apple script or shell script
Try:
set myString to "This\\is\\my\\string" -- really This\is\my\stringset {TID, text item delimiters} to {text item delimiters, "\\"}set myString to text items of myStringset text item delimiters to "/"set myString to myString as textset text item delimiters to TIDreturn myString
or
set input to "This\\is\\my\\string" -- really This\is\my\stringset output to do shell script "echo " & quoted form of input & " | sed 's/[\\]/\\//g'"
As far as I know, it is no longer necessary to restore text item delimiters, so adayzdone's example can be simplified to something like this:
set s to "aa\\bb\\cc"set text item delimiters to "\\"set ti to text items of sset text item delimiters to "/"ti as text -- "aa/bb/cc"
do shell script
uses /bin/sh
, which is a version of bash that starts in POSIX mode and has a few other changes as well. One of them is that xpg_echo
is enabled by default, so that echo
interprets escape sequences like \t
:
do shell script "echo " & quoted form of "ss\\tt" & " | xxd -p" -- "737309740a"
You can just use printf %s
instead:
do shell script "printf %s " & quoted form of "ss\\tt" & "|tr \\\\ /" -- "s/t"
If you don't add a without altering line endings
specifier, do shell script
converts line endings to CR and chomps one newline from the end of the output.
@Lauri Ranta's do shell script
pointers are helpful, but a simple variation of the original command does the trick:
Even though do shell script
invokes bash
as sh
and therefore in a mode it calls 'POSIX mode', many bash-specific features are still available, among them the so called here-string to feed a string directly to a command via stdin, e.g.: cat <<<'Print this'
:
Thus, by using a here-string - passed with quoted form of
from AppleScript, which is always advisable - the echo
problem is bypassed:
# Sample input.set input to "a\\\\b" # equivalent of" 'a\\b'# Remove all `\` characters; returns 'ab'set input to (do shell script "sed 's/\\\\//g' <<<" & quoted form of input)
Alternative, using tr -d
instead of sed
:
set input to (do shell script "tr -d '\\\\' <<<" & quoted form of input)