Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

protecting linefeeds in lists

4 views
Skip to first unread message

Svenn Are Bjerkem

unread,
Nov 21, 2005, 10:24:40 AM11/21/05
to
I currently don't get TclMagick to compile on my platform so I have to
do some magick manually in tcl

I have some screenshots that I want to annotate. On the command line I
can run convert like this:

convert -font helvetica \
-fill white \
-draw 'text 100,330 W_n(2.08u)\nW_p(4.15u)' \
-draw 'text 200,330 r_n(1100)\nr_p(800)' \
-draw "text 10,380 Wed_Nov_16_10-36-25_MET_2005" ss54.png tmp.png
mv tmp.png ss54.png

I tried a short tcl script like this:

set filename "ss54.png"
set execstring [list exec convert -font helvetica \
-fill white \
-draw 'text 100,330 W_n(2.08u)\nW_p(4.15u)' \
-draw 'text 200,330 r_n(1100)\nr_p(800)' \
-draw "text 10,380 Wed_Nov_16_10-36-25_MET_2005" $filename tmp.
$filename]
catch $execstring result
puts $result

The problem is that I have to protect those \n against list or I will
have an error message from convert that 'text' is not a valid primitive.

If I do
{-draw 'text 100,330 W_n(2.08u)\nW_p(4.15u)'} \

then convert use everything between { and } as a switch, which doesn't
exist.

This defninitively has something with understanding exec and lists and
the way tcl evaluate arguments, but I get nowhere with my guessing.
Maybe somebody can tell me what I do wrong.

--
Svenn

Jeff Hobbs

unread,
Nov 21, 2005, 11:16:57 AM11/21/05
to Svenn Are Bjerkem
Svenn Are Bjerkem wrote:
> I currently don't get TclMagick to compile on my platform so I have to
> do some magick manually in tcl
>
> I have some screenshots that I want to annotate. On the command line I
> can run convert like this:
>
> convert -font helvetica \
> -fill white \
> -draw 'text 100,330 W_n(2.08u)\nW_p(4.15u)' \
> -draw 'text 200,330 r_n(1100)\nr_p(800)' \
> -draw "text 10,380 Wed_Nov_16_10-36-25_MET_2005" ss54.png tmp.png

You probably want:

-draw [list text 200,300 "r_n(1100)\nr_p(800)"]

--
Jeff Hobbs, The Tcl Guy
http://www.ActiveState.com/, a division of Sophos

Bryan Oakley

unread,
Nov 21, 2005, 11:45:31 AM11/21/05
to
Svenn Are Bjerkem wrote:
> I currently don't get TclMagick to compile on my platform so I have to
> do some magick manually in tcl
>
> I have some screenshots that I want to annotate. On the command line I
> can run convert like this:
>
> convert -font helvetica \
> -fill white \
> -draw 'text 100,330 W_n(2.08u)\nW_p(4.15u)' \
> -draw 'text 200,330 r_n(1100)\nr_p(800)' \
> -draw "text 10,380 Wed_Nov_16_10-36-25_MET_2005" ss54.png tmp.png
> mv tmp.png ss54.png
>
> I tried a short tcl script like this:
>
> set filename "ss54.png"
> set execstring [list exec convert -font helvetica \
> -fill white \
> -draw 'text 100,330 W_n(2.08u)\nW_p(4.15u)' \
> -draw 'text 200,330 r_n(1100)\nr_p(800)' \
> -draw "text 10,380 Wed_Nov_16_10-36-25_MET_2005" $filename tmp.
> $filename]
> catch $execstring result
> puts $result
>

When you run the command from the command line, you must use quotes
according to whatever command line you are using. Quotes are what
distinguishes one argument from another. When you exec something from
Tcl, you must use quotes that Tcl understands, again to distinguish one
argument from another.

Thus, when doing something like the above from the command line, you
must replace the ' quotes with something Tcl understands. The most
direct translation of '' is {}, but you can also use double quotes, no
quotes, or other variations depending on the circumstances.

Thus, something like:

convert ... -draw 'text 100,330...'

In tcl becomes something like:

exec convert ... -draw {text 100,330...}

So, your problem isn't that you need to preserve backslashes, but rather
that you need to use the proper quoting. The way I exec stuff with long
command lines is to build a proper list; this makes it trivial to add or
remove options, and obviates the need to use backslashes

set command [list exec convert]
lappend command -font helvetica -fill white
lappend command -draw "text 100,330 W_n(2.08u)\nW_p(4.15u)"
lappend command -draw "text 200,330 r_n(1100)\nr_p(800)"
lappend command -draw "text 10,380 Wed_Nov_16_10-36-25_MET_2005"
lappend command $filename tmp.$filename
catch $command

0 new messages