Skip to main content
Known Participant
November 15, 2005
Question

Script okay in photoshop, not as BridgeTalk message

  • November 15, 2005
  • 5 replies
  • 1480 views
The following script extracts the style names from a style asl file in photoshop. This code works for photoshop CS2, when run from the ETSK with CS2 selected:

// CS2 version
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;
}
alert(msg);
}
}

x();

---------------------

However, the identical code, run from Bridge, using BridgeTalk, will not work. BTW, this is the only code I have that will not work via BridgeTalk. It stumbles on (result != null) and photoshop seizes up.

The following code is saved as test.jsx in the StartupScripts folder:

// Bridge version
Test=function(){
var targetApp = BridgeTalk.getSpecifier( "photoshop", "9");
if (targetApp) {
var bt = new BridgeTalk;
bt.target = 'photoshop';
bt.body = x.toSource();
alert(bt.body);
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;
}
alert(msg);
}
}

createMenu( "command", "Test...", "at the end of tools/ps", "Test", Test );

------------------

Bob - if you see this I sure would appreciate your help!

TIA
Rory
This topic has been closed for replies.

5 replies

Known Participant
November 15, 2005
Thanks Bob, it was good to get Wade's input too. Don't apologise, you are incredibly generous with your time and expertise, it is very much appreciated by all.

Andrew
Known Participant
November 15, 2005
Andrew,

I posted the following on ps-scripts a few minutes ago. Thanks for forwarding it. I keep trying to get over there to help out, but there's just too many demands on my time right now.

My Post on ps-scripts

I'm going to bet on file encoding.

I would try (after you open the file)

File.encoding = "UTF-8";

or UTF-16, or BINARY.

I do know there's a bug when parsing/writing XMP. Near the start of every XMP file I've seen, there's a binary character. Attempting to write that character produces a silent failure every time. I have submitted the bug internally.

I have also found that omitting that character doesn't affect the ability of the Adobe apps from reading the resulting XMP file.

Give the encoding a try, otherwise, just omit the character.

Bob
Adobe Workflow Scripting
Known Participant
November 15, 2005
Hiya Bob

Along related lines, we had a queery at ps-scripts about writing the xmpMetadata.rawData to an external file.

http://www.ps-scripts.com/bb/viewtopic.php?t=286

It fails on the 18th character, which if you export the data from the the file info palette shows up as 

Any idea about that and what one might convert it to. Not important so don't waste time on it, just curious.

Andrew
hillrgAuthor
Known Participant
November 15, 2005
Bob - I can't tell you how much I appreciate this! Thanks very much!

Rory
Known Participant
November 15, 2005
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 );
Silly-V
Legend
December 6, 2015

This is one of the most useful functions ever. Thank you.