The way AI scripting treats working with palettes makes me appreciate the silent mechanisms behind regular & other app palettes all around us which I take for granted all of the time. When I did some BT function work, I think I ended up making a 'main' function to send to via BT , and in that main function I would have replaceable strings which get dynamically replaced by my in-script choices in a string-replace action inside the function which sends the BT message. This is some sample code from my sample palette:
function asSourceString(func){
return func.toSource().toString().replace("(function "+func.name+"(){","").replace(/}\)$/,"");
}
function sendBTmsg(func, updateObj, resultFunc, resultFuncArgs){
if(updateObj==undefined){updateObj={self: 'nothing'}};
var updateObjName=updateObj.self;
if(updateObjName!=undefined){
var bt=new BridgeTalk;
bt.target='illustrator';
var btMsg=asSourceString(func);
for(all in updateObj){
if(all!='self'){
var rx=new RegExp(updateObjName+'\\.'+all,'g');
btMsg=btMsg.replace(rx,updateObj[all]);
}
}
bt.body=btMsg;
//$.write(btMsg);
bt.onResult=function(result){
if(resultFunc!=undefined){
resultFunc(result.body, resultFuncArgs);
}
}
bt.send();
} else {
$.writeln("Error, function 'sendBTmsg(func, updateOb)': the update object should have a 'self' property, indentifying its' name.");
}
}
function returnRefresh(){
outcome={
width: width,
height: height,
unitsName: unitsName,
}.toSource()
}
function processRefresh(objString, args){
var uiStuff=args[0];
var obj=eval(objString);
uiStuff.wE.text=obj.width+" "+obj.unitsName;
uiStuff.hE.text=obj.height+" "+obj.unitsName;
}
var UNITS={
self: "UNITS",
_in: {ratio: 1/72, name: '"in"'},
_mm: {ratio: 0.352777778, name: '"mm"'},
_pt: {ratio: 1, name: '"pt"'}
};
var INFO={
self: "INFO",
unitsRatio: UNITS._in.ratio,
unitsName: UNITS._in.name,
procedure: asSourceString(doNothing),
decimals: 3,
colortype: '"CMYKColor"',
clr_1: 0,
clr_2: 100,
clr_3: 100,
clr_4: 0,
arrowType: 1,
};
// Main messenger function
function paletteToAI(){
outcome=null;
if(app.documents.length>0){
var doc=app.activeDocument;
var sel=doc.selection;
if(sel.length>0){
var top,left,bottom,right;
if(sel.length>1){
alert("selection items : "+sel.length);
} else {
var item=sel[0];
left=item.visibleBounds[0];
top=item.visibleBounds[1];
right=item.visibleBounds[2];
bottom=item.visibleBounds[3];
var cv=1*INFO.unitsRatio;
var decimals=Math.pow(10,INFO.decimals);
var pointWidth=-1*(left-right);
var pointHeight=top-bottom;
var width=Math.round(-1*(left-right)*cv*decimals)/decimals;
var height=Math.round((top-bottom)*cv*decimals)/decimals;
var unitsName=INFO.unitsName;
var proportionFactor=function(){
var num=(pointWidth<pointHeight) ? pointWidth:pointHeight;
if(num>72){
return 1;
} else {
return (num>10) ? num/72:10/72;
}
}();
INFO.procedure;
}
} else {
alert("Please select an Art Object...");
outcome=null;
}
} else {
alert("There isnt an open document!");
outcome=null;
}
}
//=========================================== Skip down to where this is attached to palette controls... =================//
refreshBtn.addEventListener('mousedown',function(){ // REFRESH
INFO.unitsRatio=UNITS['_'+unitsDD.selection.text].ratio;
INFO.unitsName=UNITS['_'+unitsDD.selection.text].name;
INFO.procedure=asSourceString(returnRefresh);
INFO.decimals=decimalsDD.selection.text;
sendBTmsg(paletteToAI, INFO, processRefresh,[uiStuff]);
});
specsBtn.addEventListener('mousedown',function(){ // Real Specs
INFO.unitsRatio=UNITS['_'+unitsDD.selection.text].ratio;
INFO.unitsName=UNITS['_'+unitsDD.selection.text].name;
INFO.decimals=decimalsDD.selection.text;
INFO.procedure=asSourceString(putSpecs);
sendBTmsg(paletteToAI, INFO, processRefresh,[uiStuff]);
});
This is the part where it all comes together inside the addEventListeners on particular UI controls. The INFO object gets updated from all the UI inputs and the sendBTmsg function uses the paletteToAI (main message function) which also uses the INFO object for the INFO.procedure part, which becomes the specific function inside the main function. The processRefresh and [uiStuff] are there to basically refresh the palette UI based on what happened in the BT procedure using the BT onresult call.
... View more