Skip to content

Instantly share code, notes, and snippets.

@daluu
Created April 8, 2015 22:10
Show Gist options
  • Star 5 You must be signed in to star a gist
  • Fork 3 You must be signed in to fork a gist
  • Save daluu/2d9dec72d0863f9ff5a7 to your computer and use it in GitHub Desktop.
Save daluu/2d9dec72d0863f9ff5a7 to your computer and use it in GitHub Desktop.
Loads data from a URL in Adobe Illustrator, synchronously! (Uses Bridge behind the scenes)
BridgeTalk.prototype.sendSynch = function(timeout) {
var self = this;
self.onResult = function(res) {
this.result = res.body;
this.complete = true;
}
self.complete = false;
self.send();
if (timeout) {
for (var i = 0; i < timeout; i++) {
BridgeTalk.pump(); // process any outstanding messages
if (!self.complete) {
$.sleep(1000);
} else {
break;
}
}
}
var res = self.result;
self.result = self.complete = self.onResult = undefined;
return res;
}
// for typos, provide an alias
BridgeTalk.prototype.sendSync = BridgeTalk.prototype.sendSynch;
function loadUrl(url, timeout) {
var bt = new BridgeTalk();
bt.target = 'bridge';
var httpTimeout = timeout;
var script = '';
script += "if ( !ExternalObject.webaccesslib )\n";
script += " ExternalObject.webaccesslib = new ExternalObject('lib:webaccesslib');\n";
script += "var response = null;\n";
script += "var retry = true;\n";
script += "while (retry) {\n";
script += " var http = new HttpConnection('" + url + "') ; \n";
script += " http.timeout = " + httpTimeout + ";\n";
script += " http.execute() ;\n";
script += " try{\n";
script += " response = http.response;\n";
script += " retry = false;\n";
script += " } catch (e){\n";
script += " BridgeTalk.bringToFront('bridge');\n";
script += " if (!confirm('There was an error communicating with the server. Would you like to retry?'))\n";
script += " retry = false;\n";
script += " }\n";
script += "}\n";
script += "response;\n";
bt.body = script;
return bt.sendSynch(timeout);
}
alert(loadUrl('http://rpc.geocoder.us/service/csv?address=1600+Pennsylvania+Ave,+Washington+DC',50));
@daluu
Copy link
Author

daluu commented Apr 8, 2015

For asynchronous version, see https://gist.github.com/mericson/6509997

@Silly-V
Copy link

Silly-V commented May 5, 2016

This is pure gold, thank you.

@Silly-V
Copy link

Silly-V commented Dec 3, 2016

Sadly, ExternalObject is not working in Bridge CC2017!

@wuservices
Copy link

Looks awesome! So does this not work on CC2017?

@Silly-V
Copy link

Silly-V commented Sep 5, 2017

Adobe said next release will have this issue fixed!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment