Skip to main content
Known Participant
November 8, 2005
Question

newline in script string

  • November 8, 2005
  • 7 replies
  • 1865 views
This is so basic but I haven't found my way round it yet.

function mytest() {
var bt = new BridgeTalk ();
var theScript = "alert('Hello World');";
bt.target = 'photoshop';
bt.body = theScript;
bt.send();
}
mytest();

If I want Hello and World on different lines I would expect the following to work but it doesn't, in fact it kills the script.

var theScript = "alert('Hello\nWorld');";

I have tried various things like \\n and reversing the ' and " - so far no progress.

Andrew
This topic has been closed for replies.

7 replies

Known Participant
January 3, 2006
eval('alert("Hellow\\rworld!");')

Using "\\" will solve the problem.

jxswm
Participant
December 19, 2005
thanks .
Known Participant
November 8, 2005
It's more of a 'want to know' thing really, your original structure works fine and it is very tidy - I tried using your encoder before I asked (since I did see the connection) and got nowhere but I will try again and see. Seems to me there may be some basic deficiencies in the BridgeTalk implementation - though what it CAN do remains great.

Andrew
Known Participant
November 8, 2005
Try encoding the script using the BridgeTalkEncoder thingy, then wrapping it in some standard code to decode/execute it.

This thread is making me laugh. I just went though something like this last month. It's why the BridgeTalkEncoder thing was written....

Same problems, same frustrations, new bald spot...

var editMenuScript = new File (Folder.commonFiles + '/Adobe/StartupScripts/ah-bridgelaunch.js');
ah_RunScript('bridge',editMenuScript);
function ah_RunScript ( tApp, tScript) {
var bt = new BridgeTalk ();
var theScript = ah_remoteScript (tScript);
bt.target = tApp;
bt.body = ah_scriptTransport theScript );
bt.send();
}

function ah_scriptTransport ( scpt ) {
var tScpt = "var encodedScript = '" + BridgeTalkEncode( scpt ) + "';\n";
tScpt += "actualScript = decodeURIComponent( encodedScript );\n";
tScpt += "eval( actualScript );";
return tScript;
}
Known Participant
November 8, 2005
Here's another related question. I've been wondering if I can shortcircuit the remote script loop by just reading in the target file as a string and sending it off to BridgeTalk:

var editMenuScript = new File (Folder.commonFiles + '/Adobe/StartupScripts/ah-bridgelaunch.js');

ah_RunScript('bridge',editMenuScript);

function ah_RunScript ( tApp, tScript) {
var bt = new BridgeTalk ();
var theScript = ah_remoteScript (tScript);
bt.target = tApp;
bt.body = theScript;
bt.send();
}

function ah_remoteScript ( tFile) {
var f = new File (tFile);
if (f.open('r')) var sStr = f.read();
else throw('failed to open script'+f.fsName);
return sStr;
}

You will recognise the differences from your usual structure. I can't really see why this does not work as well as your more complex structure (which always works), but it definitely doesn't. It does sometimes (for example if the target script is just alert('hello'); but as soon as the target gets complex it fails.

To try and work out what was happening I did a lttile test. I pointed it at a complex script, took 'theScript' from above, wrote it to another file and then ran the derived file. It worked without problem. So, why did the bt.send not produce exactly the same effect with the same string.

Andrew
Known Participant
November 8, 2005
Thanks yet again Bob.

Bob IS Scripting.

(heh heh)

Andrew
Known Participant
November 8, 2005
This is one that I got stuck on for a while..., I was serializing arbitrary objects and sending them across the wire, comments in methods, quotes, all kinds of stuff was screwing it up. It would seem like escaping everything would work, but I couldn't make it work.

So I wrote this:

BridgeTalkEncode = function( txt ) {
txt = encodeURIComponent( txt );
txt = txt.replace( /\r/, "%0d" );
txt = txt.replace( /\n/, "%0a" );
txt = txt.replace( /\\/, "%5c" );
txt = txt.replace(/'/g, "%27");
return txt.replace(/"/g, "%22");
}

You can decode it with decodeURI on the destination

For example, this works:

function mytest() {
var bt = new BridgeTalk ();
var theScript = "alert( decodeURI( 'Hello%0AWorld') );";
bt.target = 'photoshop';
bt.body = theScript;

bt.send();
}

If the scriptlet were restructured a little, you could call BridgeTalkEncode( msg ) on a string with the \n in it, and it would convert the \n to %0A for you (along with any other trouble-making characters).

Bob
Adobe WAS Scripting