Hi Rory,
I saw this issue for the first time a few weeks ago when sending some very complex scripts across BT. BT messages are strings, evaluated in the target app. So things like escape characters don't translate well. Theoretically, you have to double up the escape chars.
But that never worked for me. So I wrote an encoder that encodes BT message, replacing the problem characters with their hex encoded equivalents. You then use decodeURI in the target to make it all work.
Here's your script re-done, that works from Bridge via BT:
Test=function(){
var targetApp = BridgeTalk.getSpecifier( "photoshop", "9");
if ( targetApp ) {
var bt = new BridgeTalk;
bt.target = 'photoshop';
var script = "$.level = 1;\nvar scp ='" + bridgeTalkEncode( "x = " + x.toString() + "\nx();" ) + "'";
script += ";\nvar scpDecoded = decodeURI( scp );\n";
script += "eval( scpDecoded );";
bt.body = script;
$.writeln(bt.body);
bt.onResult = function( msg ) {
$.writeln( msg.body );
}
bt.onError = function( msg ) {
$.writeln( "ERROR: " + msg.body );
}
$.writeln( "Message Sent: " + bt.send() );
}
}
x = function () {
var file = File( app.path + '/Presets/Styles/buttons.asl' );
file.open( 'r' );
file.encoding = 'BINARY';
var stuff = file.read( file.length );
var rx = /null\x00\x00\x00\x02\x00\x00\x00\x00Nm[ ][ ]TEXT\x00\x00.+?\x00\x00/g;
var result = stuff.match( rx );
if (result != null) {
var msg = '';
for (var i = 0; i < result.length; i++ ) {
result = result.replace( /.*Nm[ ][ ]TEXT/g, '' );
result = result.replace( /[\x00-\x1F]/g, '' );
msg += '\n'+result;
}
return msg;
}
}
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");
}
createMenu( "command", "Test...", "at the end of tools/ps", "Test", Test );