/********************************
* @name Send Swatches To Flash
* @author Justin Putney
* @website http://blog.ajarproductions.com
* @version 1.1.2
********************************/
var appName = BridgeTalk.appName;
var flashAppName = getFlashAppName();
var flashStatus = BridgeTalk.getStatus(flashAppName);
var appValid = true;
if(appName == "illustrator") var appSpecificFunc = getHexArrayFromAI;
else if(appName == "indesign") var appSpecificFunc = getHexArrayFromID;
else appValid = false;
if(app.documents.length > 0) {
var filename = app.activeDocument.name;
sendSwatchesToFlash(filename);
} else {
alert("You must have a document open to run this script.");
}
function getFlashAppName(){
if(!this.hasOwnProperty("apps")) return "flash";
var flashVersions = new Array();
//assumes higher versions are listed last
for(var i=apps.length-1; i>0; i--) {
if(apps[i].toLowerCase().indexOf("flash") != -1) {
if(BridgeTalk.getStatus(apps[i]) == "IDLE"
|| BridgeTalk.getStatus(apps[i]) == "BUSY"
|| BridgeTalk.getStatus(apps[i]) == "PUMPING"
&& BridgeTalk.getStatus(apps[i]) != "ISNOTRUNNING"){
//this version of the app is open
return apps[i];
}
//may use this in a later version of the script
flashVersions.push(apps[i]);
}
}
return "flash";
}
function sendSwatchesToFlash(filename){
if(appValid){
if(flashStatus == "IDLE" || true){
var hexArray = appSpecificFunc();
if(hexArray.length < 1) {
alert("There were no swatches found that could be sent.");
return;
}
var scriptStr = stringifyFunction(isPanelEnabled);
scriptStr += stringifyFunction(getKulerXMLString);
scriptStr += stringifyFunction(encode);
scriptStr += stringifyFunction(newLayer);
scriptStr += stringifyFunction(drawSwatches);
scriptStr += "var str = '" + hexArray.toString() + "';";
scriptStr += "newLayer('" + filename + "');";
scriptStr += "drawSwatches(str);";
fl.sendScriptToFlash(scriptStr); //fl seems to work w/ mult. versions of Flash
BridgeTalk.bringToFront(flashAppName);
}
else if (flashStatus == "ISNOTRUNNING" || flashStatus == "ISNOTINSTALLED"){
(alert("Flash must be running for this script to work."));
}
else if(flashStatus == "BUSY" ) {
alert("Flash is currently busy. Check the application to make sure no dialog boxes are open.");
}
else {
alert("There was an error when trying to interface with the Flash application.");
}
} else {
alert("You cannot run this script from this application and/or this version.");
}
}
function stringifyFunction(func){
var str = func.toSource();
var rStr = str.substring(1, str.length-1) + "\r";
return rStr;
}
//COLOR FUNCTIONS
//(AI & ID)
function getHexStringFromRGB(rgbArray) {
var red = Math.round(rgbArray[0]).toString(16);
var green = Math.round(rgbArray[1]).toString(16);
var blue = Math.round(rgbArray[2]).toString(16);
red = (red.length < 2) ? '0' + red : red;
green = (green.length < 2) ? '0' + green : green;
blue = (blue.length < 2) ? '0' + blue : blue;
return (red + green + blue).toUpperCase();
}
//(AI)
function getRGBfromGrayscale(gray){
var rgbArray = new Array();
var pcent= gray * .01;
var colorAmount = Math.round(pcent * 225);
rgbArray[0] = colorAmount;
rgbArray[1] = colorAmount;
rgbArray[2] = colorAmount;
return rgbArray;
}
//APPLICATION SPECIFIC FUNCTIONS
function getHexArrayFromAI(){
var hexArray = new Array();
var oldDoc = app.activeDocument;
var newDoc = app.documents.add(DocumentColorSpace.RGB);
var swatches = oldDoc.swatches;
newDoc.swatches.removeAll();
//add swatches to new doc
for(var i=0; i < swatches.length; i++){
var tSwatch = swatches[i];
var cType = tSwatch.color.toString();
if(tSwatch.name != "[Registration]" && cType != "[PatternColor]" && cType != "[NoColor]" && cType !="[GradientColor]" && cType != null){
var newSwatch = newDoc.swatches.add();
for(prop in tSwatch){
newSwatch[prop] = tSwatch[prop]
}
if(newSwatch.color.toString() == "[SpotColor]") {
newSwatch.color = newSwatch.color.spot.color;
newSwatch.color.colorType = ColorModel.PROCESS;
}
var newType = newSwatch.color.toString();
//convert spot colors to process so that they'll show as RGB
//$.writeln("newSwatch = " + newSwatch + ", with color: " + newType);
if(newType == "[RGBColor]" || newType == "[SpotColor]") var rgbArray = [newSwatch.color.red, newSwatch.color.green, newSwatch.color.blue];
else if(newType == "[GrayColor]") var rgbArray = getRGBfromGrayscale(newSwatch.color.gray);
var hex = getHexStringFromRGB(rgbArray);
hexArray.push(hex);
}
}
newDoc.close(SaveOptions.DONOTSAVECHANGES);
app.activeDocument = oldDoc;
return hexArray;
}
function getHexArrayFromID(){
var doc = app.activeDocument;
var swatches = doc.swatches;
var hexArray = new Array();
for(var i=0; i< swatches.count(); i++){
var cSwatch = swatches.item(i);
if(cSwatch.hasOwnProperty ("tintValue") || !cSwatch.hasOwnProperty("model"))
continue; //ignore tints
if(cSwatch.model == ColorModel.REGISTRATION || cSwatch.name == "Black")
continue; //ignore registration
if(cSwatch.hasOwnProperty("space"))
var originalSpace = cSwatch.space;
else continue;
if(originalSpace != ColorSpace.RGB) cSwatch.space = ColorSpace.RGB;
var hex = getHexStringFromRGB(cSwatch.colorValue);
hexArray.push(hex);
//restore swatch's original color space
if(originalSpace != ColorSpace.RGB) cSwatch.space = originalSpace;
}
return hexArray;
}
////////////////////////////////
// FLASH JSFL FUNCTIONS //
///////////////////////////////
function isPanelEnabled(){
var version = fl.version.split(" ")[1];
version = parseInt(version.split(",")[0]);
var panelEnabled = (version >= 10);
return panelEnabled;
}
function newLayer(filename) {
if(fl.documents.length < 1) fl.createDocument();
if(!isPanelEnabled())
var layer = fl.getDocumentDOM().getTimeline().addNewLayer("Swatches from: " + filename);
}
function drawSwatches(colorStr) {
var dom = fl.getDocumentDOM();
var colors = colorStr.split(",");
if(isPanelEnabled()){
var xmlstr = getKulerXMLString(colors);
var enstr = encode(xmlstr);
dom.setSwatchKulerTheme(enstr);
} else {
var originalFill = dom.getCustomFill("toolbar");
if(originalFill.style == "noFill") {
alert("Because of a bug in Flash, you cannot run this script without a fill color setting. Please set a fill color in the Flash toolbar and run the script again.");
return;
}
var fill = dom.getCustomFill("toolbar");
var swatchSize = 40;
var maxColCount = Math.floor(dom.width/swatchSize);
var currentRow = 0;
var currentCol = 0;
for(var i = 0; i < colors.length; i++){
if(colors[i] != undefined){
fill.color = "#"+colors[i];
fill.style = "solid";
dom.setCustomFill(fill);
var tPos = currentRow * swatchSize;
var bPos = tPos + swatchSize;
var lPos = currentCol * swatchSize;
var rPos = lPos + swatchSize;
dom.addNewRectangle({left:lPos, top:tPos, right:rPos, bottom:bPos},0,false,true);
currentCol++;
if(currentCol >= maxColCount) {
currentRow++;
currentCol = 0;
}
}
}
dom.setCustomFill(originalFill);
}
}
function getKulerXMLString(hexArr){
var labelName = "Imported_Swatches";
var rgbObj = new Object();
rgbObj.r = 0; rgbObj.g = 0; rgbObj.b = 0;
var xmlstr = "\ntrue\n\n";
xmlstr += "\n";
xmlstr += "\n";
for(var i=0; i < hexArr.length; i++) {
var hexStr = "0x" + hexArr[i];
xmlstr += "\n";
xmlstr += "rgb\n";
xmlstr += "" + rgbObj.r.toString() + "\n";
xmlstr += "" + rgbObj.g.toString() + "\n";
xmlstr += "" + rgbObj.b.toString() + "\n0.0\n";
xmlstr += "localProcess\n";
xmlstr += "\n";
xmlstr += "" + hexStr + "\n";
xmlstr += "" + i + "\n";
xmlstr += "\n";
}
xmlstr += "\n\n\n";
return xmlstr;
}
function encode (xmlString){
var val = xmlString;
var retStr = new String();
var i = 0;
for(i=0; i> 12) + 65;
var char2 = ((val.charCodeAt(i) & 0x0f00) >> 8) + 65;
var char3 = ((val.charCodeAt(i) & 0x00f0) >> 4) + 65;
var char4 = ((val.charCodeAt(i) & 0x000f)) + 65;
retStr = retStr + String.fromCharCode(char1) + String.fromCharCode(char2)
+ String.fromCharCode(char3) + String.fromCharCode(char4);
}
return retStr;
}