Script to prepare .AI files to export to PSD
Hello, just wanted to share a script I put together based on some others found online. My workflow at the moment is to prepare LaTeX equations in illustrator, then export them as PSDs for use in Premiere with every sub-layer being a separate later and each layer acting as a photoshop group. However, I found the conversion from illustrator to photoshop files a bit confusing, especially how the layers tend to flatten when exporting. So the way I found to retain all layers is to group each sub-layer by itself. However when you group it it doesn't retain the name of the sub-layer, and on top of that I have each character in the equations as a separate sub-layer and Premiere will merge them as the same footage if they are named the same. So, my script groups all the sub-layers by themselves, checks if the name has shown up before, and renames the group to the name of the sublayer, incrementing it it already exists. This should make every sublayer grouped and uniquely named to prepare for PSD export. Hope this helps someone!
Download the script and example file here.
// Group Sublayers
// Karl Northfell
// MIT License
var doc = app.activeDocument;
var thisLayer;
const nameArray = [];
const countArray = [];
var layerName = "blank";
Array.prototype.includes = function(obj) {
var i = this.length;
for (var j = 0; j <= i; j++) {
if (this[j] === obj) {
return true;
}
}
return false;
}
Array.prototype.location = function(obj) {
var i = this.length;
for (var j = 0; j <= i; j++) {
if (this[j] === obj) {
return j;
}
}
return false;
}
Array.prototype.print = function(obj) {
var i = this.length;
for (var j = 0; j < i; j++) {
console.log(this[j]);
}
}
doc.selection = null;
for (var i = 0; i < doc.layers.length; i++) {
thisLayer = doc.layers[i];
var thisPageItem;
var index = 0;
for (var j = 0; j < thisLayer.pageItems.length; j++) {
thisPageItem = thisLayer.pageItems[j];
if (thisPageItem.typename != "GroupItem") { // if this page item isn't grouped, group it
thisPageItem.selected = true; // select the sublayer
app.redraw();
layerName = thisPageItem.name; // Get name of selection
if (!nameArray.includes(layerName)) { // if name not found in array
nameArray.push(layerName); // then add name to array
index = nameArray.location(layerName); // store location of name in array as index
countArray[index] = 1; // set count at index to 1
layerName = layerName + "_" + 1;
} else { // if name is found in name array
index = nameArray.location(layerName); // set index to the location in name array
countArray[index]++; // increment the count by 1
layerName = layerName + "_" + countArray[index];
}
app.executeMenuCommand("group"); // group the sublayer by itself
thisPageItem.parent.name = layerName; // rename the group to match sublayer
app.activeDocument.selection = null; // deselect
doc.selection = null;
}
}
}Also please note that this will create a large undo stack so please save before running it. I tried adjusting where the redraw function was to manage that but it would break the script when ran as an action so I'm keeping it as is. If anyone knows a way to manage the history/undo state better please let me know.
