Exit
  • Global community
    • Language:
      • Deutsch
      • English
      • Español
      • Français
      • Português
  • 日本語コミュニティ
  • 한국 커뮤니티
0

Script to prepare .AI files to export to PSD

New Here ,
Oct 14, 2024 Oct 14, 2024

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.

TOPICS
Scripting
381
Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Adobe
Community Expert ,
Oct 14, 2024 Oct 14, 2024
LATEST

Hi @MobileSuitKrampus thanks for sharing! You've done a great job here.

 

Regarding the history/undo states (and also the general performance) it is good to avoid doing UI type stuff such as selecting and executing menu items where possible.

 

Here is a version of your script that takes a more direct approach. I used a simpler scheme for naming things uniquely, but if you need it your original way, you can put that part back in. The main difference is the way I handle the grouping. Also I loop over the items backwards, because when you mess with the DOM, any DOM references that are *after* the changed part of the DOM (eg. adding a new group) will have the wrong index and will give strange results.

 

See what you think.

- Mark

 

 

 

// Group Sublayers
// Karl Northfell
// MIT License

// modified version by m1b
// url: https://community.adobe.com/t5/illustrator-discussions/script-to-prepare-ai-files-to-export-to-psd/m-p/14915608
(function () {

    var doc = app.activeDocument;
    var nameExists = {};
    var counter = 0;

    layerLoop:
    for (var i = 0, layer; i < doc.layers.length; i++) {

        layer = doc.layers[i];

        if (
            layer.locked
            || !layer.visible
        )
            continue layerLoop;

        layer.name = getUniqueName(layer.name);

        itemLoop:
        for (var j = layer.pageItems.length - 1, item; j >= 0; j--) {

            item = layer.pageItems[j];

            if ('GroupItem' === item.typename) {
                item.name = getUniqueName(item.name);
                continue itemLoop;
            }

            var group = layer.groupItems.add();
            group.name = getUniqueName(item.name);
            item.name = getUniqueName(item.name);
            group.move(item, ElementPlacement.PLACEAFTER);
            item.move(group, ElementPlacement.PLACEATBEGINNING);
        }

    }

    function getUniqueName(name) {
        while (nameExists[name]) {
            name = name.replace(/_\d+|$/, '_' + (++counter));
        }
        nameExists[name] = true;
        return name;
    };


})();

 

EDIT: fixed a possible issue where an original group item could have a non-unique name and also added the layer names to the nameExists object.

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines