Skip to content

Instantly share code, notes, and snippets.

@LeeTeng2001
Last active December 29, 2023 02:12
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 LeeTeng2001/ef828f9ce523f647c1b4bad16e6b0346 to your computer and use it in GitHub Desktop.
Save LeeTeng2001/ef828f9ce523f647c1b4bad16e6b0346 to your computer and use it in GitHub Desktop.
automate school brochures content using illustrator
// constants
var OFFSET_ARTBOARD_IDX = 8;
var ARTBOARD_LIMIT = 100;
var FILE_CONTENT_PATH = "~/Programming/WebstormProjects/personal-portfolio/content";
// parse file content into a list of object with properties
function parseTxt() {
var res = [];
var curContent = {};
var matchTitle = true;
var subMatchIdx = 0;
const titleReg = /^Project \d+/
const regList = [
/^Contact Information/,
/^Project Description and Objectives/,
/^Eligibility Requirements/,
/^Main Tasks/,
/^Website/
]
// read file
var testtextfile = File(FILE_CONTENT_PATH);
testtextfile.encoding = 'UTF8'; // set to 'UTF8' or 'UTF-8'
testtextfile.open("r");
var message = testtextfile.read();
testtextfile.close();
var lineList = message.split(/\n/);
for (var i = 0; i < lineList.length; i++) {
if (matchTitle) {
if (lineList[i] === '') continue;
if (lineList[i].match(titleReg)) {
curContent.title = lineList[i];
// console.log(res.length+1, lineList[i]);
curContent.content = ["", "", "", "", ""]
matchTitle = false;
}
} else {
if (lineList[i] === '') {
if (subMatchIdx < regList.length) continue;
else {
subMatchIdx = 0;
res.push(curContent);
curContent = {};
curContent.content = ["", "", "", "", ""]
matchTitle = true;
}
}
if (subMatchIdx < regList.length && lineList[i].match(regList[subMatchIdx])) {
//console.log(subMatchIdx, lineList[i);
subMatchIdx++;
} else {
if (curContent.content[subMatchIdx - 1] !== "") {
curContent.content[subMatchIdx - 1] += "\n";
}
curContent.content[subMatchIdx - 1] += lineList[i].replace("* ", ">\t");
}
}
}
return res;
}
var textList = parseTxt();
/// ------------------------------------------------------------------------------
/// modify illustrator logic
function duplicateCurrentArtboard(){
var doc = app.activeDocument;
var thisBoardIndex = doc.artboards.getActiveArtboardIndex();
var thisBoard = doc.artboards[thisBoardIndex];
var thisRect = thisBoard.artboardRect;
var lastBoard = doc.artboards[doc.artboards.length - 1];
var lastRect = lastBoard.artboardRect;
doc.selection = null;
doc.selectObjectsOnActiveArtboard();
app.copy();
var newBoard = doc.artboards.add(thisRect);
var offsetH = 10;
newBoard.artboardRect = [
lastRect[2] + offsetH,
lastRect[1],
lastRect[2] + offsetH + (thisRect[2] - thisRect[0]),
lastRect[3]
];
newBoard.name = thisBoard.name + " copy";
app.executeMenuCommand("pasteFront");
doc.selection = null;
}
function replaceWordRetainStyle(textFrame, regXp, replaceStr) {
// var s = /arguments/gi;
var s = regXp;
var replacer = replaceStr, result;
while (result = s.exec(textFrame.contents)) {
try {
aCon = textFrame.characters[result.index];
aCon.length = result[0].length;
aCon.contents = replacer;
} catch (e) {}
}
}
function selectAllOnArtboard(artboardIdx){
var doc = app.activeDocument;
doc.artboards.setActiveArtboardIndex(artboardIdx);
doc.selection = null; //clear selection
doc.selectObjectsOnActiveArtboard();
}
const TITLE_SEARCH = [
"Contact Information:",
"Project Description and Objectives:",
"Eligibility Requirements:",
"Main Tasks:",
"Website:",
]
// documents
var doc = app.activeDocument;
for (var i = 0; i < ARTBOARD_LIMIT; i++) {
// duplicate template and select new template content
if (OFFSET_ARTBOARD_IDX + i >= doc.artboards.length) continue;
selectAllOnArtboard(OFFSET_ARTBOARD_IDX + i);
// duplicateCurrentArtboard();
// loop left and right content
var leftIdx = -1;
var rightIdx = -1;
// get left right text frame
var content = "";
for (var j = 0; j < doc.selection.length; j++){
if (doc.selection[j].typename !== "TextFrame") continue;
// title number
if (doc.selection[j].contents.indexOf("Project Description and Objectives") !== -1) {
if (leftIdx === -1) {
leftIdx = j;
} else if (doc.selection[j].left < doc.selection[leftIdx].left) {
rightIdx = leftIdx;
leftIdx = j;
} else {
rightIdx = j;
}
}
}
// replace left content based on subtitle position
var subtitleIdxList = []
for (var j = 0; j < TITLE_SEARCH.length; j++) {
var idx = doc.selection[leftIdx].contents.indexOf(TITLE_SEARCH[j]);
if (idx === -1) {
alert("broke left!" + i + " " + TITLE_SEARCH[j])
break;
}
subtitleIdxList.push(idx);
}
// alert(subtitleIdxList)
// replace left
for (var j = TITLE_SEARCH.length - 1; j >= 0; j--) {
aCon = doc.selection[leftIdx].characters[subtitleIdxList[j] + TITLE_SEARCH[j].length + 1];
if (j === TITLE_SEARCH.length - 1) {
aCon.length = doc.selection[leftIdx].contents.length - (subtitleIdxList[j] + TITLE_SEARCH[j].length + 1);
} else {
aCon.length = subtitleIdxList[j+1] - (subtitleIdxList[j] + TITLE_SEARCH[j].length + 1);
}
aCon.contents = textList[i*2].content[j] + "\n\n";
}
// replace right
if (rightIdx !== -1) {
subtitleIdxList = [];
for (var j = 0; j < TITLE_SEARCH.length; j++) {
var idx = doc.selection[rightIdx].contents.indexOf(TITLE_SEARCH[j]);
if (idx === -1) {
alert("broke right!" + i + " " + TITLE_SEARCH[j])
break;
}
subtitleIdxList.push(idx);
}
for (var j = TITLE_SEARCH.length - 1; j >= 0; j--) {
aCon = doc.selection[rightIdx].characters[subtitleIdxList[j] + TITLE_SEARCH[j].length + 1];
if (j === TITLE_SEARCH.length - 1) {
aCon.length = doc.selection[rightIdx].contents.length - (subtitleIdxList[j] + TITLE_SEARCH[j].length + 1);
} else {
aCon.length = subtitleIdxList[j+1] - (subtitleIdxList[j] + TITLE_SEARCH[j].length + 1);
}
aCon.contents = textList[i*2+1].content[j] + "\n\n";
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment