[SVN] Passing variables to osascript, or other workaround?

Allan Odgaard allan at macromates.com
Thu May 12 03:33:23 UTC 2005


On May 12, 2005, at 0:53, Charilaos Skiadas wrote:

> Neat! It works great if I remove the ': $2' part above. If I have it  
> in though, it produces:
> 200:201: syntax error: Expected “"” but found unknown token. (-2741)
>
> The part matched by $2 is basically the rest of the line where the  
> \label occurs, so that the user has a context and can figure out  
> better what label they want. So it is a whole LaTeX line, with lots of  
> unescaped \'s. So I added the line:
> list="${list//\\\\/\\\\}"
> above the res=`osascript...
> Now it works properly, but it took unacceptably long, I'm guessing  
> because the lines might be pretty long. Is there any way to speed it  
> up?

Well, you could use sed to do the replacement, I did this benchmark  
(also for perl, ruby, and bash):

time { for (( i = 0; i < 100; i++ )); do
	 
list='abed\abed\abed\abed\abed\abed\abed\abed\abed\abed\abed\abed\abed\a 
bed\abed\abed\abed\abed\abed\abed\abed\abed\abed\abed\abed\abed\abed\abe 
d\abed\abed\abed\abed\abed\abed\abed\abed\abed\abed\abed\abed\abed\abed\ 
abed\abed\abed\abed\abed\abed\abed\abed\abed\abed\abed\abed\abed\abed\ab 
ed\abed\abed\abed\abed\abed\abed\abed\abed\abed\abed\abed'
	list=$(sed -e <<<$list 's|\\|\\\\|g')
done; }

And you're certainly right about the shell being slow, here's the times  
on my 2.5 GHz G5:
    sed    1.020s
    perl   1.300s
    ruby   2.191s
    bash  11.008s

Btw: if you want to experiment with this, just paste the loop into TM,  
select it, and press ctrl-R, then it'll run it within TM and output the  
result -- a bit easier than to play with this in a terminal window :)

> I tried adding a line: list="${list:0:80}" before the above assignment  
> to show only a reasonable part of the rest of the line (btw, can I  
> combine them in one step? the obvious way didn't work.)

Bash rarely allow nested statements, what you can do is use commands  
instead of built-in features:
	list=$(cut <<<$list -b-80|sed -e 's|\\|\\\\|g')

 From the above benchmark, it seems that this also has better  
performance ;)

Btw: I switched from `...` to $(...) because the former requires  
escaping of \, and I switched from s/.../.../ to s|...|...| in the sed  
substitution for cosmetic reasons.

> but that took me back to the previous error. Is there something else I  
> might need to escape, maybe braces or something? (i.e. when I cut at  
> the 80 characters, maybe I've left some braces unended?)

I don't think so -- but if you did the cut after escaping escapes, you  
could cut in the middle of such an escape pair, and thus have a bad  
string again.

> In terms of scope, this is to be used inside a \ref{} or an \eqref{}  
> (can't remember if there are others). would the proper scope be  
> text.latex, declaration.ref-or-label.latex or  
> keyword.control.ref-or-label.latex ?

declaration.ref-or-label is most appropriate IMHO. The  
keyowrd.control.ref-or-label would work, but only because the start  
bracket is currently part of the keyword, so placing the caret just  
after { would still give a scope of being in the keyword, but that  
could change.

This of course assuming that the command is only useful inside a  
\(eq)?ref, otherwise the scope should be text.latex. Limiting a scope  
should mainly be done when it's an advantage, e.g. if you bind it to  
'escape' then it's required that you put it inside the  
declaration.ref-or-label scope, so that escape doesn't lose its value  
outside this scope. But if you put it on some unused (for latex) key,  
then there's probably little reason to not provide the feature in the  
entire text.latex scope.




More information about the textmate-dev mailing list