Skip to content

Instantly share code, notes, and snippets.

@mhulse
Created June 11, 2016 05:38
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mhulse/bd1676a04ae318e884f4fd89e3fb7b4a to your computer and use it in GitHub Desktop.
Save mhulse/bd1676a04ae318e884f4fd89e3fb7b4a to your computer and use it in GitHub Desktop.
Another Adobe Illustrator BridgeTalk boilerplate example.
/* jshint laxbreak:true, -W043 */
/* globals app, $, BridgeTalk */
// jshint ignore:start
#target illustrator
#targetengine main
// jshint ignore:end
var TEST = (function($application, $helper, undefined) {
// Private variable container object:
var _private = {};
_private.doc = $application.activeDocument;
// Creates and returns a palette window object.
_private.palette = function(title) {
// Palette box setup:
var meta = 'palette { \
orientation: "column", \
alignChildren: ["fill", "top"], \
margins: 10, \
group1: Group { \
alignChildren: ["fill", "top"], \
orientation: "row", \
reconnect: Button { text: "Reconnect" }, \
close: Button { text: "Close" } \
} \
}';
// Instanciate `Window` class with setup from above:
var palette = new Window(meta, title, undefined, {
//option: value
//closeButton: false
});
// Start button:
palette.group1.reconnect.onClick = function() {
// We HAVE to “reconnect” to the app using `BridgeTalk`:
_private.reconnect();
};
// Close and/or palette UI close buttons:
palette.group1.close.onClick = function() {
palette.close();
};
palette.onClose = function() {
// Do any cleanup or garbage collection?
};
return palette;
};
// Reconnect to the `activeDocument` using boilerplate BT syntax:
_private.reconnect = function() {
var bt;
bt = new BridgeTalk();
bt.target = 'illustrator';
// Enter the script through the “back door”:
bt.body = 'TEST.init({ backdoor: true })';
bt.send();
};
// We are reconnected to Illustrator:
_private.reconnected = function() {
var doc = _private.doc;
alert('aloha ' + doc + ' ' + (typeof doc));
// Continue the script from where you left off ...
};
// Script setup, if coming through the “front door”:
_private.init = function(title) {
var palette;
// Open document(s)?
if ($application.documents.length > 0) {
// Make palette window:
palette = _private.palette();
// Center and show the palette window:
palette.center();
palette.show();
} else {
// Nope, let the user know what they did wrong:
alert('You must open at least one document.');
}
};
// Public API:
return {
init: function(args) {
// Entry point for `BridgeTalk` and script testing:
if (
args.backdoor
&&
(args.backdoor.toString() == 'true') // Normalizing due to `BridgeTalk` passing ONLY strings.
) {
// Shortcut to guts of this script:
_private.reconnected();
} else {
// First time through, fire-up the palette window:
_private.init(args.title);
}
}
};
})(app, $);
TEST.init({
//backdoor: true, // Use this for testing!
title: 'Test'
});
// Done!
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment