Tinderbox User-to-User Forum (for formal tech support please email: info@eastgate.com)
Tinderbox Users >> Questions and Answers >> `sed' not receiving quote chars from $Text

Message started by Demogorgon on Feb 1st, 2010, 1:49am

Title: `sed' not receiving quote chars from $Text
Post by Demogorgon on Feb 1st, 2010, 1:49am

Since TBX cannot accommodate smart quotes, I fished around the forums and decided on the `sed' workaround.  Goal: use `sed' to replace space-quote combos with their proper curly versions.

First I tested `sed' on a text file via Terminal with this command:

$ sed 's: ": “:g' foo.txt

It worked.

Then I went into TBX, invented an Attribute called `try', and ran this command:

$try = runCommand("echo " + $Text + " | sed 's: ": “:g'");

I discovered that TBX, when getting $Text, *strips out the quotes*.

Is there any way, then, to automate the running-through of my notes and changing space-DQ, space-SQ, etc. to their curly counterparts since TBX strips quotes from $Text when sending it to stdin?

Thanks in advance.

Title: Re: `sed' not receiving quote chars from $Text
Post by Mark Anderson on Feb 1st, 2010, 5:50am

If I try to use the run command as given above:

$try = runCommand("echo " + $Text + " | sed 's: ": “:g'");

I can't get that to run in TB. If I try to do so via the Rule box in the Renames dialog it won't close and if I enter it via a Key Attribute the target attribute gets "shell: syntax error". This isn't surprising as what is being sent to runCommand is effectively this:

runCommand("echo " + $Text + " | sed 's: " + : “:g'");

…which isn't what you assumed. The next double-quote to the one preceding the pipe is the one in the middle of the sed command causing the last part to misinterpreted. I did try this, to no better effect:

$MyString = runCommand("echo " + $Text + " | sed 's:"+' "'+": “:g'");

It would appear that if I start the runCommand with double quotes, these are then the 'outer' set of quotes throughout the sequence. I think the reason you see no single/double normal quote within the text when echoed via the command line is TB  - at the CL export level treats them as code mark-up rather than literal text.

The other issue you're dealing with here is that currently there is no method for escaping 'normal' single/double quote inside a strings send from TB to the command line. Normally judicious use of single and or double quotes an navigate around the problem (which I why it's not a bigger issue). But here, it bites, not least because the key content is the very character(s) that can't be escaped. As using the shell via command line is the only 'scripting' method open to you within TB (there is no AppleScript interface beyond UI scripting), I think using this approach to change the type of quote characters is a closed avenue for now.

If you need to delve further on this, I'd suggest you write direct to support who may be able to to give you a way forward or at worst a more authoritative description as to what's (not) possible.

[later note: exportedString() doesn't get round this either.]

:-[  It pays to use the right syntax version.  See my next post below.

Title: Re: `sed' not receiving quote chars from $Text
Post by Mark Bernstein on Feb 1st, 2010, 9:49am

1) Use the two-argument version of runCommand, which evaluates the second argument and sends it to stdin; this saves the trouble of using echo -- and the headaches of eol's and quotes and other special characers on the command line.

2) Instead of exerting yourself to construct a complex command line with multiply-escaped quotation characters, why not put your sed command into an executable script file?  

This would leave you with something like $Try=runCommand(quotify,$Text)

Title: Re: `sed' not receiving quote chars from $Text
Post by Mark Anderson on Feb 1st, 2010, 11:39am

For my part, I've been misunderstanding the two different syntax versions (mental note to write a better aTbRef article).

for Demogorgon - see this demo TBX.

It shows a simple example of the syntax Mark B suggested. The rule I use:
   $MyString = runCommand($Text(quotify),$Text);

Of course it could be an OnAdd action, etc.  The 'long syntax version uses runCommand(command, data).  In this case the data is the current notes text. The command stored conveniently in another note (called quotify) holds the code for 2 sed calls:
   sed 's: ": “:g' | sed 's:" :” :g'

The first call is for any instance of a space followed by straight double-quotes is made into an opening smart double-quote. In the second sed call (piped from the stdout of the first one) the same is done foe closing double quotes.  As the example shows, you'll need to expand the command to allow for quotes coming immediately before/after punctuation but hopefully it well set you on the way. I'm not a command line expert, but reading into this you might want to use a different CL tool such as awk as the later support lookahead/lookbehind matching which might make for a shorter/quicker command - once you've figured it.

Title: Re: `sed' not receiving quote chars from $Text
Post by Mark Anderson on Feb 1st, 2010, 2:02pm

I've updated the above demo so that now uses the command:

  sed 's:"\([^"]*\)":“\1”:g' | sed "s:'\([d|ll|re|s|t|ve]\):’\1:g" | sed "s:'\([^']*\)':‘\1’:g"

You may need to add a few contractions to the second sed but I think this covers most bases. Note that with the single quotes, you need to do the contractions first or you'll get false positive pairing of quotes.  Of course, if you do things like refer to double-quotes via the " character, again you'll get a false pairing.

For others reading here, the above can be translated for other types of quotes such as are used in other languages, e.g. «Spanish quotes».

If expanding this furter how the sed search strings use a ' for the double quote search and " for the single quote searches. Use enclosing single quotes for anything except doing ' searches and you should be fine.

Title: Re: `sed' not receiving quote chars from $Text
Post by Demogorgon on Feb 1st, 2010, 3:36pm

Wow!  Thanks, you guys, so much!

Mark B: It was massively idiotic of me not to consider that my model was using echo, which naturally strips quotes since it's primary job is to show us how the shell sees our commands.   :-X

Mark A: You are an angel.  Your demo has really turned me on, fired-up my imagination, and given me hope.  I'm fantasizing about what I could do with a container-note full of shell command notes.  Infinite thanks!   :D

Title: Re: `sed' not receiving quote chars from $Text
Post by Mark Anderson on Feb 1st, 2010, 5:24pm

Incidentally, though the demo file uses a Rule you don't want to use that method for actual use as otherwise it's firing each agent cycle.  For this sort of application, it's better to make an agent and left the OnAdd action call the command line. The agent can then either be deleted or turned off until next needed.

A different route to a once-only running of the code is to store it in a stamp - in fact that's probably the most elegant method of making a 'quotify' command for your TBX - it's even menu-accessible.

Tinderbox User-to-User Forum (for formal tech support please email: info@eastgate.com) » Powered by YaBB 2.2.1!
YaBB © 2000-2008. All Rights Reserved.