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

Need help preventing crashes/optimizing script

Community Beginner ,
Jan 17, 2023 Jan 17, 2023

Copy link to clipboard

Copied

Hello! I'm trying to set up a script to automate the preparation of files from a drawing to a print-ready format. It's meant to create a .psd doc for each item in a specific layer.

I've tested the arrange() code, and it works fine in a single iteration, but when using an iterator, it causes Illustrator to crash within the 3rd iteration. I've checked task manager while it was running and  my RAM, CPU and GPU were all running within normal levels.

Could someone more experienced help with diagnosing what's going wrong here? 

var myDoc = app.activeDocument;
var boxLayer = 100;
var ignoreColor = new CMYKColor();
ignoreColor.black = 0;
ignoreColor.cyan = 0;
ignoreColor.magenta = 0;
ignoreColor.yellow = 0;
var boardNum = 0
var woodLineFound = false
var woodColourFound = false
var exportPath = "E:/Illustrator Exports/"

var exportOptions = new ExportOptionsPhotoshop();
exportOptions.maximumEditability = true
exportOptions.writeLayers = true
exportOptions.resolution = 150.0
exportOptions.writeLayers = true
exportOptions.writeLayers = true
exportOptions.imageColorSpace = ImageColorSpace.CMYK

setup()
arrange()


//find how many boards there are
function setup() {
    for (var i = 0; i < myDoc.layers.length; i++) {
        if (myDoc.layers[i].name == "wood line layer") {
            boxLayer = i
            woodLineFound = true
        }
    }

    if (woodLineFound = false) {
        alert("Error, missing wood line layer")
    }

    for (var i = 0; i < myDoc.layers.length; i++) {
        if (myDoc.layers[i].name == "wood line layer") {

            // names boards by order it was placed in
            for (var x = myDoc.layers[i].pageItems.length - 1; x >= 0; x--) {
                myDoc.layers[i].pageItems[x].name = "Board " + (myDoc.layers[i].pageItems.length - x);
            }

            // filters out boards with the specific color, then counts all other boards
            for (var x = myDoc.layers[i].pageItems.length - 1; x >= 0; x--) {
                if (myDoc.layers[i].pathItems[x].strokeColor.black != ignoreColor.black &&
                    myDoc.layers[i].pathItems[x].strokeColor.cyan != ignoreColor.cyan &&
                    myDoc.layers[i].pathItems[x].strokeColor.magenta != ignoreColor.magenta &&
                    myDoc.layers[i].pathItems[x].strokeColor.yellow != ignoreColor.yellow
                ) {
                    //myDoc.layers[i].pageItems[x].move(clippingSet, ElementPlacement.PLACEATBEGINNING);
                    boardNum++
                }
            }
        }
    }
//resets to provide clean slate for the arrange() function
    app.undo()
}





function arrange() {

    for (b = 0; b < boardNum; b++) {

        var clippingSet = app.activeDocument.groupItems.add();

    //find the wood line layers
    for (var i = 0; i < myDoc.layers.length; i++) {
        if (myDoc.layers[i].name == "wood line layer") {
            boxLayer = i
            woodLineFound = true
        }
     //adds the a 0 opacity object so the latter cropping maintains shape
        if (myDoc.layers[i].name == "wood colour layer") {
            woodColourFound = true
            myDoc.layers[i].locked = false
            var opacityLayer = myDoc.layers[i].duplicate(myDoc, ElementPlacement.PLACEATEND)
            myDoc.layers[i].locked = true
        }

    }
    //error if not present
    if (woodLineFound = false) {
        alert("Error, missing wood line layer")
    }
    if (woodColourFound = false) {
        alert("Error, missing wood colour")
    }

    // transfer all layer items into clipping group, but relabel wood line layer products for ease of clipping later
    for (var i = 0; i < myDoc.layers.length; i++) {
        if (myDoc.layers[i].name != "wood line layer" && myDoc.layers[i].name != "Clipping Layer" && myDoc.layers[i].locked == false) {
            for (var x = myDoc.layers[i].pageItems.length - 1; x >= 0; x--) {
                myDoc.layers[i].pageItems[x].move(clippingSet, ElementPlacement.PLACEATEND)
            }
        }
        else if (myDoc.layers[i].name == "wood line layer") {

            // names boards by order it was placed in
            for (var x = myDoc.layers[i].pageItems.length - 1; x >= 0; x--) {
                myDoc.layers[i].pageItems[x].name = "Board " + (myDoc.layers[i].pageItems.length - x);
            }

            // filters out boards with the specific color and move them into the clipping group
            for (var x = myDoc.layers[i].pageItems.length - 1; x >= 0; x--) {
                if (myDoc.layers[i].pathItems[x].strokeColor.black != ignoreColor.black &&
                    myDoc.layers[i].pathItems[x].strokeColor.cyan != ignoreColor.cyan &&
                    myDoc.layers[i].pathItems[x].strokeColor.magenta != ignoreColor.magenta &&
                    myDoc.layers[i].pathItems[x].strokeColor.yellow != ignoreColor.yellow
                ) {
                    myDoc.layers[i].pageItems[x].move(clippingSet, ElementPlacement.PLACEATBEGINNING);
                }
            }
            myDoc.layers[i].locked = true;
        }
    }


        //find and move to front the required board
        var cuttingBoard = clippingSet.pathItems.getByName("Board " + (b + 1))
        cuttingBoard.move(clippingSet, ElementPlacement.PLACEATBEGINNING)

        //expand cutitngboard for bleed
        cuttingBoard.width += 0.113
        cuttingBoard.height += 0.113

        clippingSet.clipped = true

        // crop the clipped selection
        app.executeMenuCommand("selectall")
        app.executeMenuCommand("Live Pathfinder Crop")
        app.executeMenuCommand("expandStyle")
        app.executeMenuCommand("copy")

        // create a new doc and copy object over
        var newDoc = app.documents.add(DocumentColorSpace.CMYK, 6179.528, 1559.055)
        app.executeMenuCommand("paste")

        //resize to 2180+550 + bleed
        newDoc.groupItems[0].width = 6179.528 + 11.338
        newDoc.groupItems[0].height = 1559.055 + 11.338

        //align to center
        app.executeMenuCommand("selectall")
        app.executeMenuCommand('Vertical Align Center')
        app.executeMenuCommand('Horizontal Align Center')

        //release to layer
        var ilayer = newDoc.layers[0]

        for (var i = ilayer.groupItems[0].pathItems.length - 1; i >= 0; i--) {
            var sublayer = ilayer.layers.add();
            ilayer.groupItems[0].pathItems[i].move(sublayer, ElementPlacement.PLACEATBEGINNING)
        }

        // export
        var exportFile = new File(exportPath + cuttingBoard.name + ".PSD")
        newDoc.exportFile(exportFile, ExportType.PHOTOSHOP, exportOptions)
        newDoc.close(SaveOptions.DONOTSAVECHANGES)

        // resets for another iteration
        app.undo()

    }
}



 

TOPICS
Import and export , Performance , Scripting

Views

187

Translate

Translate

Report

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

correct answers 1 Correct answer

Guide , Jan 18, 2023 Jan 18, 2023

If I was to take a guess, I would say the cause is app.undo().

Votes

Translate

Translate
Guide ,
Jan 18, 2023 Jan 18, 2023

Copy link to clipboard

Copied

If I was to take a guess, I would say the cause is app.undo().

Votes

Translate

Translate

Report

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
Community Beginner ,
Jan 19, 2023 Jan 19, 2023

Copy link to clipboard

Copied

Thank you! Rewrote it without undo() and it definitely works better. Still some issues with it, but I've devised enough of a work around to be happy with it for now.

Votes

Translate

Translate

Report

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
Community Expert ,
Jan 18, 2023 Jan 18, 2023

Copy link to clipboard

Copied

agree with @femkeblanco, undo() is not recommend. If you need put things back for another iteration you could close the file and reopen it or keep track of the changes so you can change them back, or perhaps work with duplicate items or creating temporary layers that can be deleted after the changes are made. There are options.

 

copy/paste is also not recommend, use duplicate() instead, it works across documents.

 

also, this line should not work, duplicate() does not apply to layers

var opacityLayer = myDoc.layers[i].duplicate(myDoc, ElementPlacement.PLACEATEND)

 

Votes

Translate

Translate

Report

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
Community Beginner ,
Jan 19, 2023 Jan 19, 2023

Copy link to clipboard

Copied

LATEST

Appreciate the advice! I ended up setting everything up before clipping, then copy/pasting it into a 2nd new doc before clipping /cropping/ exporting. ( had some weirdness with duplicate() and selection so kept going with copy paste).

Especially appreciate the call on the opacityLayer thing too, hadn't noticed it wasnt working because I was following old procedures for prepping the doc

Votes

Translate

Translate

Report

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