Skip to content

Instantly share code, notes, and snippets.

@kklai
Last active March 2, 2017 20:00
Show Gist options
  • Save kklai/77365306da18b97c8ada0b515d7da235 to your computer and use it in GitHub Desktop.
Save kklai/77365306da18b97c8ada0b515d7da235 to your computer and use it in GitHub Desktop.
// helper functions
var getJSON = function (nameOfFile) {
var dataFile = File(docPath + '/' + nameOfFile + ".json");
dataFile.open();
data = dataFile.read();
data = eval(data);
dataFile.close();
return data;
};
var turnArrayIntoHash = function(thisArray,uniquekeyname) {
newHash = {};
for (var i=0;i<thisArray.length;i++) {
e = thisArray[i];
var hashKey = e[uniquekeyname];
newHash[hashKey] = e;
}
return newHash;
};
var createStyleHashes = function(){
var styleHash = {};
var types = ["paragraph","character","graphic"];
for (var i=0; i < types.length; i++) {
styleHash[types[i]] = {};
var thisArray = doc[types[i] + "Styles"];
for (var j=0; j < thisArray.length; j++) {
styleHash[types[i]][thisArray[j].name] = thisArray[j];
}
}
return styleHash;
};
// settings
var doc = app.activeDocument;
var docPath = doc.path;
var docLayers = doc.layers;
var layerForText = {};
// assign layers that will be used in the script to variables
for (var i=0; i < docLayers.length; i++) {
var thisLayer = docLayers[i];
var thisLayerName = thisLayer.name;
if (thisLayerName==="graphic") {
var subLayers = thisLayer.layers;
for (var j=0; j < subLayers.length; j++) {
var thisSubLayer = subLayers[j];
var thisSubLayerName = thisSubLayer.name;
if (thisSubLayerName==="scripttext" ) { textLayer = thisSubLayer; }
if (thisSubLayerName==="scriptrules") { rulesLayer = thisSubLayer; }
if (thisSubLayerName==="scriptboxes") { boxesLayer = thisSubLayer; }
}
}
}
var root = this; // this creates the equivalent of a "window" object
root.style = createStyleHashes();
var ps = {};
var cs = {};
var gs = {};
var ps = root.style.paragraph;
var cs = root.style.character;
var gs = root.style.graphic;
var data = getJSON("sample_data");
var graphicWidth = 23.76 * 72;
var graphicHeight = 16*72;
var newLine = "\r";
var textTop = 0;
var textLeft = 0;
var textBottom = graphicHeight;
var textRight = 0;
var textWidth = graphicWidth;
var text = "";
var paragraphNum = 0;
var paragraphRef = [];
for (var i = 0; i < data.length; i++) {
text += data[i].key + newLine;
paragraphRef.push("category");
for (var j = 0; j < data[i].values.length; j++) {
text += data[i].values[j].name + newLine;
paragraphRef.push("name");
if (data[i].values[j].title) {
text += data[i].values[j].title + newLine;
paragraphRef.push("title");
}
parsed = "";
for (var k = 0; k < data[i].values[j].quotes.length; k++) {
if (k == data[i].values[j].quotes.length - 1) {
parsed += data[i].values[j].quotes[k].quote;
paragraphRef.push("text");
} else {
parsed += data[i].values[j].quotes[k].quote + ", ";
}
}
text += parsed + newLine;
}
}
text = text.replace(/“/g, "\u201C")
.replace(/”/g, "\u201D")
.replace(/–/g, "\u2013") // en dash
.replace(/—/g, "\u2014") // em dash
.replace(/‘/g, "\u2018")
.replace(/’/g, "\u2019");
var rectRef = textLayer.pathItems.rectangle(textTop, textLeft, textWidth, graphicHeight);
var areaTextRef = textLayer.textFrames.areaText(rectRef);
areaTextRef.columnCount = 16;
areaTextRef.columnGutter = 0.15*72;
areaTextRef.contents = text;
var counter = 0;
var arrayCounter = 0;
var contentCounter = 0;
var categoryCounter = 0;
for (var i = 0; i < paragraphRef.length; i++) {
if (paragraphRef[i] == "category") {
category = areaTextRef.paragraphs[i];
ps['categories'].applyTo(category,true);
}
if (paragraphRef[i] == "name") {
insult_name = areaTextRef.paragraphs[i];
ps['names'].applyTo(insult_name,true);
}
if (paragraphRef[i] == "title") {
insult_title = areaTextRef.paragraphs[i];
ps['titles'].applyTo(insult_title,true);
}
if (paragraphRef[i] == "text") {
insult_text = areaTextRef.paragraphs[i];
ps['text'].applyTo(insult_text,true);
}
}
https://www.dropbox.com/s/f4zrse74yzd35sb/sample.ai?dl=0
[
{
"key": "people",
"values": [
{
"name": "The 2016 election",
"title": "",
"quotes": [
{
"quote": "media-rigged"
},
{
"quote": "absolutely being rigged"
},
{
"quote": "being rigged by the media, in a coordinated effort with the Clinton campaign"
},
{
"quote": "rigged"
},
{
"quote": "is being rigged"
}
]
},
{
"name": "The 2016 election",
"title": "",
"quotes": [
{
"quote": "media-rigged"
},
{
"quote": "absolutely being rigged"
},
{
"quote": "being rigged by the media, in a coordinated effort with the Clinton campaign"
},
{
"quote": "rigged"
},
{
"quote": "is being rigged"
}
]
},
{
"name": "The 2016 election",
"title": "",
"quotes": [
{
"quote": "media-rigged"
},
{
"quote": "absolutely being rigged"
},
{
"quote": "being rigged by the media, in a coordinated effort with the Clinton campaign"
},
{
"quote": "rigged"
},
{
"quote": "is being rigged"
}
]
},
{
"name": "The 2016 election",
"title": "",
"quotes": [
{
"quote": "media-rigged"
},
{
"quote": "absolutely being rigged"
},
{
"quote": "being rigged by the media, in a coordinated effort with the Clinton campaign"
},
{
"quote": "rigged"
},
{
"quote": "is being rigged"
}
]
},
{
"name": "The 2016 election",
"title": "",
"quotes": [
{
"quote": "media-rigged"
},
{
"quote": "absolutely being rigged"
},
{
"quote": "being rigged by the media, in a coordinated effort with the Clinton campaign"
},
{
"quote": "rigged"
},
{
"quote": "is being rigged"
}
]
},
{
"name": "The 2016 election",
"title": "",
"quotes": [
{
"quote": "media-rigged"
},
{
"quote": "absolutely being rigged"
},
{
"quote": "being rigged by the media, in a coordinated effort with the Clinton campaign"
},
{
"quote": "rigged"
},
{
"quote": "is being rigged"
}
]
},
{
"name": "The 2016 election",
"title": "",
"quotes": [
{
"quote": "media-rigged"
},
{
"quote": "absolutely being rigged"
},
{
"quote": "being rigged by the media, in a coordinated effort with the Clinton campaign"
},
{
"quote": "rigged"
},
{
"quote": "is being rigged"
}
]
},
{
"name": "The 2016 election",
"title": "",
"quotes": [
{
"quote": "media-rigged"
},
{
"quote": "absolutely being rigged"
},
{
"quote": "being rigged by the media, in a coordinated effort with the Clinton campaign"
},
{
"quote": "rigged"
},
{
"quote": "is being rigged"
}
]
},
{
"name": "The 2016 election",
"title": "",
"quotes": [
{
"quote": "media-rigged"
},
{
"quote": "absolutely being rigged"
},
{
"quote": "being rigged by the media, in a coordinated effort with the Clinton campaign"
},
{
"quote": "rigged"
},
{
"quote": "is being rigged"
}
]
},
{
"name": "The 2016 election",
"title": "",
"quotes": [
{
"quote": "media-rigged"
},
{
"quote": "absolutely being rigged"
},
{
"quote": "being rigged by the media, in a coordinated effort with the Clinton campaign"
},
{
"quote": "rigged"
},
{
"quote": "is being rigged"
}
]
},
{
"name": "The 2016 election",
"title": "",
"quotes": [
{
"quote": "media-rigged"
},
{
"quote": "absolutely being rigged"
},
{
"quote": "being rigged by the media, in a coordinated effort with the Clinton campaign"
},
{
"quote": "rigged"
},
{
"quote": "is being rigged"
}
]
},
{
"name": "The 2016 election",
"title": "",
"quotes": [
{
"quote": "media-rigged"
},
{
"quote": "absolutely being rigged"
},
{
"quote": "being rigged by the media, in a coordinated effort with the Clinton campaign"
},
{
"quote": "rigged"
},
{
"quote": "is being rigged"
}
]
},
{
"name": "The 2016 election",
"title": "",
"quotes": [
{
"quote": "media-rigged"
},
{
"quote": "absolutely being rigged"
},
{
"quote": "being rigged by the media, in a coordinated effort with the Clinton campaign"
},
{
"quote": "rigged"
},
{
"quote": "is being rigged"
}
]
},
{
"name": "The 2016 election",
"title": "",
"quotes": [
{
"quote": "media-rigged"
},
{
"quote": "absolutely being rigged"
},
{
"quote": "being rigged by the media, in a coordinated effort with the Clinton campaign"
},
{
"quote": "rigged"
},
{
"quote": "is being rigged"
}
]
},
{
"name": "The 2016 election",
"title": "",
"quotes": [
{
"quote": "media-rigged"
},
{
"quote": "absolutely being rigged"
},
{
"quote": "being rigged by the media, in a coordinated effort with the Clinton campaign"
},
{
"quote": "rigged"
},
{
"quote": "is being rigged"
}
]
},
{
"name": "The 2016 election",
"title": "",
"quotes": [
{
"quote": "media-rigged"
},
{
"quote": "absolutely being rigged"
},
{
"quote": "being rigged by the media, in a coordinated effort with the Clinton campaign"
},
{
"quote": "rigged"
},
{
"quote": "is being rigged"
}
]
},
{
"name": "The 2016 election",
"title": "",
"quotes": [
{
"quote": "media-rigged"
},
{
"quote": "absolutely being rigged"
},
{
"quote": "being rigged by the media, in a coordinated effort with the Clinton campaign"
},
{
"quote": "rigged"
},
{
"quote": "is being rigged"
}
]
},
{
"name": "The 2016 election",
"title": "",
"quotes": [
{
"quote": "media-rigged"
},
{
"quote": "absolutely being rigged"
},
{
"quote": "being rigged by the media, in a coordinated effort with the Clinton campaign"
},
{
"quote": "rigged"
},
{
"quote": "is being rigged"
}
]
},
{
"name": "The 2016 election",
"title": "",
"quotes": [
{
"quote": "media-rigged"
},
{
"quote": "absolutely being rigged"
},
{
"quote": "being rigged by the media, in a coordinated effort with the Clinton campaign"
},
{
"quote": "rigged"
},
{
"quote": "is being rigged"
}
]
},
{
"name": "The 2016 election",
"title": "",
"quotes": [
{
"quote": "media-rigged"
},
{
"quote": "absolutely being rigged"
},
{
"quote": "being rigged by the media, in a coordinated effort with the Clinton campaign"
},
{
"quote": "rigged"
},
{
"quote": "is being rigged"
}
]
},
{
"name": "The 2016 election",
"title": "",
"quotes": [
{
"quote": "media-rigged"
},
{
"quote": "absolutely being rigged"
},
{
"quote": "being rigged by the media, in a coordinated effort with the Clinton campaign"
},
{
"quote": "rigged"
},
{
"quote": "is being rigged"
}
]
},
{
"name": "The 2016 election",
"title": "",
"quotes": [
{
"quote": "media-rigged"
},
{
"quote": "absolutely being rigged"
},
{
"quote": "being rigged by the media, in a coordinated effort with the Clinton campaign"
},
{
"quote": "rigged"
},
{
"quote": "is being rigged"
}
]
},
{
"name": "The 2016 election",
"title": "",
"quotes": [
{
"quote": "media-rigged"
},
{
"quote": "absolutely being rigged"
},
{
"quote": "being rigged by the media, in a coordinated effort with the Clinton campaign"
},
{
"quote": "rigged"
},
{
"quote": "is being rigged"
}
]
},
{
"name": "The 2016 election",
"title": "",
"quotes": [
{
"quote": "media-rigged"
},
{
"quote": "absolutely being rigged"
},
{
"quote": "being rigged by the media, in a coordinated effort with the Clinton campaign"
},
{
"quote": "rigged"
},
{
"quote": "is being rigged"
}
]
},
{
"name": "The 2016 election",
"title": "",
"quotes": [
{
"quote": "media-rigged"
},
{
"quote": "absolutely being rigged"
},
{
"quote": "being rigged by the media, in a coordinated effort with the Clinton campaign"
},
{
"quote": "rigged"
},
{
"quote": "is being rigged"
}
]
},
{
"name": "The 2016 election",
"title": "",
"quotes": [
{
"quote": "media-rigged"
},
{
"quote": "absolutely being rigged"
},
{
"quote": "being rigged by the media, in a coordinated effort with the Clinton campaign"
},
{
"quote": "rigged"
},
{
"quote": "is being rigged"
}
]
},
{
"name": "The 2016 election",
"title": "",
"quotes": [
{
"quote": "media-rigged"
},
{
"quote": "absolutely being rigged"
},
{
"quote": "being rigged by the media, in a coordinated effort with the Clinton campaign"
},
{
"quote": "rigged"
},
{
"quote": "is being rigged"
}
]
},
{
"name": "The 2016 election",
"title": "",
"quotes": [
{
"quote": "media-rigged"
},
{
"quote": "absolutely being rigged"
},
{
"quote": "being rigged by the media, in a coordinated effort with the Clinton campaign"
},
{
"quote": "rigged"
},
{
"quote": "is being rigged"
}
]
},
{
"name": "The 2016 election",
"title": "",
"quotes": [
{
"quote": "media-rigged"
},
{
"quote": "absolutely being rigged"
},
{
"quote": "being rigged by the media, in a coordinated effort with the Clinton campaign"
},
{
"quote": "rigged"
},
{
"quote": "is being rigged"
}
]
},
{
"name": "The 2016 election",
"title": "",
"quotes": [
{
"quote": "media-rigged"
},
{
"quote": "absolutely being rigged"
},
{
"quote": "being rigged by the media, in a coordinated effort with the Clinton campaign"
},
{
"quote": "rigged"
},
{
"quote": "is being rigged"
}
]
},
{
"name": "The 2016 election",
"title": "",
"quotes": [
{
"quote": "media-rigged"
},
{
"quote": "absolutely being rigged"
},
{
"quote": "being rigged by the media, in a coordinated effort with the Clinton campaign"
},
{
"quote": "rigged"
},
{
"quote": "is being rigged"
}
]
},
{
"name": "The 2016 election",
"title": "",
"quotes": [
{
"quote": "media-rigged"
},
{
"quote": "absolutely being rigged"
},
{
"quote": "being rigged by the media, in a coordinated effort with the Clinton campaign"
},
{
"quote": "rigged"
},
{
"quote": "is being rigged"
}
]
},
{
"name": "The 2016 election",
"title": "",
"quotes": [
{
"quote": "media-rigged"
},
{
"quote": "absolutely being rigged"
},
{
"quote": "being rigged by the media, in a coordinated effort with the Clinton campaign"
},
{
"quote": "rigged"
},
{
"quote": "is being rigged"
}
]
},
{
"name": "The 2016 election",
"title": "",
"quotes": [
{
"quote": "media-rigged"
},
{
"quote": "absolutely being rigged"
},
{
"quote": "being rigged by the media, in a coordinated effort with the Clinton campaign"
},
{
"quote": "rigged"
},
{
"quote": "is being rigged"
}
]
},
{
"name": "The 2016 election",
"title": "",
"quotes": [
{
"quote": "media-rigged"
},
{
"quote": "absolutely being rigged"
},
{
"quote": "being rigged by the media, in a coordinated effort with the Clinton campaign"
},
{
"quote": "rigged"
},
{
"quote": "is being rigged"
}
]
},
{
"name": "The 2016 election",
"title": "",
"quotes": [
{
"quote": "media-rigged"
},
{
"quote": "absolutely being rigged"
},
{
"quote": "being rigged by the media, in a coordinated effort with the Clinton campaign"
},
{
"quote": "rigged"
},
{
"quote": "is being rigged"
}
]
},
{
"name": "The 2016 election",
"title": "",
"quotes": [
{
"quote": "media-rigged"
},
{
"quote": "absolutely being rigged"
},
{
"quote": "being rigged by the media, in a coordinated effort with the Clinton campaign"
},
{
"quote": "rigged"
},
{
"quote": "is being rigged"
}
]
},
{
"name": "The 2016 election",
"title": "",
"quotes": [
{
"quote": "media-rigged"
},
{
"quote": "absolutely being rigged"
},
{
"quote": "being rigged by the media, in a coordinated effort with the Clinton campaign"
},
{
"quote": "rigged"
},
{
"quote": "is being rigged"
}
]
},
{
"name": "The 2016 election",
"title": "",
"quotes": [
{
"quote": "media-rigged"
},
{
"quote": "absolutely being rigged"
},
{
"quote": "being rigged by the media, in a coordinated effort with the Clinton campaign"
},
{
"quote": "rigged"
},
{
"quote": "is being rigged"
}
]
},
{
"name": "The 2016 election",
"title": "",
"quotes": [
{
"quote": "media-rigged"
},
{
"quote": "absolutely being rigged"
},
{
"quote": "being rigged by the media, in a coordinated effort with the Clinton campaign"
},
{
"quote": "rigged"
},
{
"quote": "is being rigged"
}
]
},
{
"name": "The 2016 election",
"title": "",
"quotes": [
{
"quote": "media-rigged"
},
{
"quote": "absolutely being rigged"
},
{
"quote": "being rigged by the media, in a coordinated effort with the Clinton campaign"
},
{
"quote": "rigged"
},
{
"quote": "is being rigged"
}
]
},
{
"name": "The 2016 election",
"title": "",
"quotes": [
{
"quote": "media-rigged"
},
{
"quote": "absolutely being rigged"
},
{
"quote": "being rigged by the media, in a coordinated effort with the Clinton campaign"
},
{
"quote": "rigged"
},
{
"quote": "is being rigged"
}
]
},
{
"name": "The 2016 election",
"title": "",
"quotes": [
{
"quote": "media-rigged"
},
{
"quote": "absolutely being rigged"
},
{
"quote": "being rigged by the media, in a coordinated effort with the Clinton campaign"
},
{
"quote": "rigged"
},
{
"quote": "is being rigged"
}
]
},
{
"name": "The 2016 election",
"title": "",
"quotes": [
{
"quote": "media-rigged"
},
{
"quote": "absolutely being rigged"
},
{
"quote": "being rigged by the media, in a coordinated effort with the Clinton campaign"
},
{
"quote": "rigged"
},
{
"quote": "is being rigged"
}
]
},
{
"name": "The 2016 election",
"title": "",
"quotes": [
{
"quote": "media-rigged"
},
{
"quote": "absolutely being rigged"
},
{
"quote": "being rigged by the media, in a coordinated effort with the Clinton campaign"
},
{
"quote": "rigged"
},
{
"quote": "is being rigged"
}
]
},
{
"name": "The 2016 election",
"title": "",
"quotes": [
{
"quote": "media-rigged"
},
{
"quote": "absolutely being rigged"
},
{
"quote": "being rigged by the media, in a coordinated effort with the Clinton campaign"
},
{
"quote": "rigged"
},
{
"quote": "is being rigged"
}
]
},
{
"name": "The 2016 election",
"title": "",
"quotes": [
{
"quote": "media-rigged"
},
{
"quote": "absolutely being rigged"
},
{
"quote": "being rigged by the media, in a coordinated effort with the Clinton campaign"
},
{
"quote": "rigged"
},
{
"quote": "is being rigged"
}
]
},
{
"name": "The 2016 election",
"title": "",
"quotes": [
{
"quote": "media-rigged"
},
{
"quote": "absolutely being rigged"
},
{
"quote": "being rigged by the media, in a coordinated effort with the Clinton campaign"
},
{
"quote": "rigged"
},
{
"quote": "is being rigged"
}
]
},
{
"name": "The 2016 election",
"title": "",
"quotes": [
{
"quote": "media-rigged"
},
{
"quote": "absolutely being rigged"
},
{
"quote": "being rigged by the media, in a coordinated effort with the Clinton campaign"
},
{
"quote": "rigged"
},
{
"quote": "is being rigged"
}
]
},
{
"name": "The 2016 election",
"title": "",
"quotes": [
{
"quote": "media-rigged"
},
{
"quote": "absolutely being rigged"
},
{
"quote": "being rigged by the media, in a coordinated effort with the Clinton campaign"
},
{
"quote": "rigged"
},
{
"quote": "is being rigged"
}
]
},
{
"name": "The 2016 election",
"title": "",
"quotes": [
{
"quote": "media-rigged"
},
{
"quote": "absolutely being rigged"
},
{
"quote": "being rigged by the media, in a coordinated effort with the Clinton campaign"
},
{
"quote": "rigged"
},
{
"quote": "is being rigged"
}
]
},
{
"name": "The 2016 election",
"title": "",
"quotes": [
{
"quote": "media-rigged"
},
{
"quote": "absolutely being rigged"
},
{
"quote": "being rigged by the media, in a coordinated effort with the Clinton campaign"
},
{
"quote": "rigged"
},
{
"quote": "is being rigged"
}
]
},
{
"name": "The 2016 election",
"title": "",
"quotes": [
{
"quote": "media-rigged"
},
{
"quote": "absolutely being rigged"
},
{
"quote": "being rigged by the media, in a coordinated effort with the Clinton campaign"
},
{
"quote": "rigged"
},
{
"quote": "is being rigged"
}
]
},
{
"name": "The 2016 election",
"title": "",
"quotes": [
{
"quote": "media-rigged"
},
{
"quote": "absolutely being rigged"
},
{
"quote": "being rigged by the media, in a coordinated effort with the Clinton campaign"
},
{
"quote": "rigged"
},
{
"quote": "is being rigged"
}
]
},
{
"name": "The 2016 election",
"title": "",
"quotes": [
{
"quote": "media-rigged"
},
{
"quote": "absolutely being rigged"
},
{
"quote": "being rigged by the media, in a coordinated effort with the Clinton campaign"
},
{
"quote": "rigged"
},
{
"quote": "is being rigged"
}
]
},
{
"name": "The 2016 election",
"title": "",
"quotes": [
{
"quote": "media-rigged"
},
{
"quote": "absolutely being rigged"
},
{
"quote": "being rigged by the media, in a coordinated effort with the Clinton campaign"
},
{
"quote": "rigged"
},
{
"quote": "is being rigged"
}
]
}
]
}
]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment