• Global community
    • Language:
      • Deutsch
      • English
      • Español
      • Français
      • Português
  • 日本語コミュニティ
    Dedicated community for Japanese speakers
  • 한국 커뮤니티
    Dedicated community for Korean speakers
Exit
0

Getting the activedocument through the infamous bridgetalk

Engaged ,
Jun 10, 2016 Jun 10, 2016

Copy link to clipboard

Copied

Bridgetalk is an annoyance. I hate the hoop jumping that one has to do to get dialogs and palette windows to interact with Illustrator.

With that said, from my tests, it seems like all that one really needs when "cycling" a script is.... the active document.

In most of my scripts, it seems, if I get the active doc object then I can keep my script working on the document in question.

I was playing with this code:

#target illustrator

#targetengine main

doc = function() {

  var bt;

  bt = new BridgeTalk();

  bt.target = 'illustrator';

  bt.body = 'foo()';

  bt.onResult = function(x) {

  $.writeln('result', x.body); // result[Document test.ai]

    // somehow return active document from onResult

  };

  bt.send();

};

function foo() {

  return app.activeDocument;

}

var activedoc = doc();

// do something with activedoc ...

// and later, like in a palette window button onclick:

activedoc = doc();

// keep workng on activedoc...

This is hard for me to explain (as I write this message in frustration), but I'm trying to think of a way to make the BT stuff simpler than what I have done (and seen) in other code.

Basically, it seems like all one needs is the activedoc. If I have the active doc then my code can keep working on things.

The problem is, BT returns a string and not an object.

Long story short, is there any way for me to convert the active document string back into a usable object?

Maybe I could parse the string to get "test.ai", then use a bit more code to grab the "activedocument" object from the name of the file?

Are any of my questions making any sense to anyone?

Thanks!

M

Did I mention that I dislike bridgetalk? It just makes scripting so messy .... Feels hacky to me.

TOPICS
Scripting

Views

1.1K

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines

correct answers 1 Correct answer

Community Expert , Jun 11, 2016 Jun 11, 2016

but to answer you question, I don't think there's a way to turn your string doc into a "usable" doc object. In fact you can pass the actual doc object from Illustrator back to javascript via BT using yourdoc.toSource(), then rebuild the string object back into an object using eval(), but you'll still be unable to talk to illustrator

Votes

Translate

Translate
Adobe
Engaged ,
Jun 10, 2016 Jun 10, 2016

Copy link to clipboard

Copied

For those interested, and because it's related to this topic, here's my latest "BridgeTalk" boilerplate: Another Adobe Illustrator BridgeTalk boilerplate example. · GitHub

I can never remember, do I need to use BridgeTalk if working with a dialog window? I was just testing it, but I was getting mixed results when re-calling the active document via a dialog (not using BT).

I ask because I always find myself trying to remember this every time I do a new script.

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Engaged ,
Jun 10, 2016 Jun 10, 2016

Copy link to clipboard

Copied

mickyhulse wrote:

I can never remember, do I need to use BridgeTalk if working with a dialog window? I was just testing it, but I was getting mixed results when re-calling the active document via a dialog (not using BT).

I ask because I always find myself trying to remember this every time I do a new script.

Ok, note to future self, BT is not needed for dialog windows.

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Jun 11, 2016 Jun 11, 2016

Copy link to clipboard

Copied

it you think BT is an annoyance remember the days up to 2012 where Palettes simply weren't usable. BridgeTalk is a blessing

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Jun 11, 2016 Jun 11, 2016

Copy link to clipboard

Copied

but to answer you question, I don't think there's a way to turn your string doc into a "usable" doc object. In fact you can pass the actual doc object from Illustrator back to javascript via BT using yourdoc.toSource(), then rebuild the string object back into an object using eval(), but you'll still be unable to talk to illustrator

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Engaged ,
Jun 12, 2016 Jun 12, 2016

Copy link to clipboard

Copied

Thanks Carlos!!! I appreciate the reply.

That's a great point about pre-2012. I should look at the positive side.

One of these days I'm going to look into CEP extensions. Would be interesting to know if BT isn't needed for extensions (do they lose connection too?).

On the other hand, scripts are so easy to install and are portable.

Anyway, thanks again for your help and for reading my earlier ranting posts.

Have a great day!


Cheers,

Micky

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Jun 12, 2016 Jun 12, 2016

Copy link to clipboard

Copied

you're welcome, I have briefly played with Extensions, the main advantages I see are Web connectivity (along with all the js libraries ecosystem) and access to events...and no, no need to use BridgeTalk. Extensions have great potential.

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Engaged ,
Jun 12, 2016 Jun 12, 2016

Copy link to clipboard

Copied

CarlosCanto wrote:

you're welcome, I have briefly played with Extensions, the main advantages I see are Web connectivity (along with all the js libraries ecosystem) and access to events...and no, no need to use BridgeTalk. Extensions have great potential.

Ahh, thanks for reply/info! I am looking forward to learning more about them!

So little time, so much to learn.

Thanks again, and I'll see you around the forums.


Cheers,

M

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Valorous Hero ,
Jun 13, 2016 Jun 13, 2016

Copy link to clipboard

Copied

Yes it is the DOM not cooperating when doing an onResult which ruins all.

I think BT is kind of like CEP for evaluating parts of your program from a "hostscript" in this manner, as your line

// Enter the script through the “back door”:

bt.body = 'TEST.init({ backdoor: true })';

Is alike with CEP where a button event does it's eval-script function and accepts a string argument. This argument could be a name of a function in your reception code, which is the entire script containing your many functions which work together.

Also while the synchronous method does work to get some data from Bridge and back, I think it would not work to solve this DOM issue in Illustrator because it's talking from a palette to the app in an event-driven mode vs starting in Illustrator and running a procedural routine, using BT.

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Engaged ,
Jun 15, 2016 Jun 15, 2016

Copy link to clipboard

Copied

Thanks for the help Silly-V! I really appreciate it!

I feel better now that I've heard from you guys and have had a chance to step away from the code for a few days.

I'll be back if I have more questions.

Thanks gain for all of your help guys!

Cheers,

M

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Valorous Hero ,
Jun 15, 2016 Jun 15, 2016

Copy link to clipboard

Copied

Ahoy!

I've actually got a chance to do a quick panel script again, and this time I'm really looking to implement that kind of pattern (minus the neat and tidy code!).

Once I put my encoding strings where they needed to go, I was able to get it to work using this to launch the initial action:

try {

  props = JSON.parse(props);

  FUNCTIONS[props.procedure]( props.name );

} catch(e) {

  var p = new paletteWindow();

  p.show();

}

I also changed my sendBTmsg function to this:

function sendBTmsgArgs(func, argsObj, targetApp, resultFunc){

  var bt = new BridgeTalk;

  bt.target = targetApp;

  var functionName = (func.name == "anonymous") ? "" : func.name;

  var btMsg = func.toString();

  var meat;

  if(typeof argsObj != "undefined"){

    meat = btMsg + ";\n" + functionName + "('" + JSON.stringify(argsObj) + "');";

  } else {

  meat = btMsg + ";\n" + functionName + "();";

  }

  meat += ("\n" + "BridgeTalk.bringToFront('illustrator');");

//~ $.writeln( meat );

  meat = bridgeTalkEncode( meat );

  btMsg = "var scp ='" + meat + "'";

  btMsg += ";\nvar scpDecoded = decodeURI( scp );\n";

  btMsg += "eval( scpDecoded );";

  bt.body = btMsg;

//~  $.writeln(decodeURI(bridgeTalkDecode(btMsg)));

  if(typeof resultFunc == "function"){

  bt.onResult = resultFunc;

  }

  bt.onError = function(res){

  alert(res.body);

  };

  bt.send();

};

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Engaged ,
Jun 15, 2016 Jun 15, 2016

Copy link to clipboard

Copied

LATEST

Oooh, interesting! I am looking forward to playing with your code some time this week (hopefully tonight after work). I'll probably be back to discuss further.

Thanks for sharing!

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines