Welcome Dialog

Welcome to the Community!

We have a brand new look! Take a tour with us and explore the latest updates on Adobe Support Community.


Issues with with Document.close() changing the file

Explorer ,
Nov 18, 2021 Nov 18, 2021

Copy link to clipboard

Copied

Hello community,

 

I am runnign a script which offsets the path for all paths of a specific color. It works perfectly, and I update the name of these paths to their width so that I can run the script repeatedly. This way, if I see that the width of an element is different from its name, I want to again apply the path offsetting.

The problem is that my paths get placed in groups when I close and save the document using:

--> docRef.close(SaveOptions.SAVECHANGES);

 

Here is what I get after simply running the script (this is the CORRECT functionality):

KarlFabian5F97_0-1637226953822.png

Notice how all the blue pathitems are not grouped within anything.

Now, if I run the exact same script but I add "docRef.close(SaveOptions.SAVECHANGES);" to the end, this happens (INCORRECT):

KarlFabian5F97_1-1637227058997.png

All of a sudden the paths are grouped in a groupItem. Does anybody understand why the .close() function might be causing this strange behavior? Unfortunately, it makes it impossible for me to re-run the script, because it starts creating nested elements that make the functionality way too complex.

And yes, unfortunately I do have to close the document, because the script is applied to hundreds of documents at a time and it might be executed several times on the same files. Therefore, everything has to be automated.

 

Thank you!

 

TOPICS
Bug, How to, Scripting

Views

68

Likes

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

Explorer , Nov 18, 2021 Nov 18, 2021
So I was finally able to solve it myself. It seems taht Illustrator would automatically create these groups for some reason after offsetting the paths. What I did was to open all the files that I was editing twice. The second time around I set the pathItem's name equal to its parent's name (the group that was created) and then I use tha app menucommands to "selectall" and "ungroup". This does the trick but it's a little inefficient to have to re-open all the documents. This is the code: function...

Likes

Translate

Translate
Explorer ,
Nov 18, 2021 Nov 18, 2021

Copy link to clipboard

Copied

So I was finally able to solve it myself. It seems taht Illustrator would automatically create these groups for some reason after offsetting the paths. What I did was to open all the files that I was editing twice. The second time around I set the pathItem's name equal to its parent's name (the group that was created) and then I use tha app menucommands to "selectall" and "ungroup". This does the trick but it's a little inefficient to have to re-open all the documents.

 

This is the code:

 

function processItem(item, debug) {
    if (item.filled) {
        var pathColor = item.fillColor
        switch(pathColor.typename) {
            case 'RGBColor':
                if(pathColor.red == 92 && pathColor.green == 142 && pathColor.blue == 255) {
                    pathWidth = (Math.round(item.width * 10)/10).toString();
                    if(debug) $.writeln('pi width: ' + pathWidth);
                    if(item.name == pathWidth) {
                        if(debug) $.writeln('Mask has already been offset...');
                        return;
                    } else {
                        if(debug) $.writeln('Mask identified');
                        var xmlstring = '<LiveEffect name="Adobe Offset Path"><Dict data="R mlim 4 R ofst -0.2 I jntp 2 "/></LiveEffect>';
                        item.applyEffect(xmlstring);
                    }
                }
                break;
        }
    }
}

function processSVG() {
    var docRef = app.activeDocument;

    var docCPI = docRef.compoundPathItems
    var numCPIItems = docCPI.length
    for (var j = 0; j < numCPIItems; j++) {
        var cpi = docCPI[j];
        var pathColor = cpi.pathItems[0].fillColor
        if(pathColor != null) {
            switch(pathColor.typename) {
                case 'RGBColor':
                    if(pathColor.red == 92 && pathColor.green == 142 && pathColor.blue == 255) {
                        cpi.selected = true;
                        app.executeMenuCommand("noCompoundPath");
                        j--;
                        numCPIItems--;
                    }
                    break;
            }
        }
    }

    var docPI = docRef.pathItems
    var numPathItems = docPI.length
    mainLayer = docRef.activeLayer
    for (var i = 0; i < numPathItems; i++) {
        var pi = docPI[i];
        processItem(pi, false);
    }

    docRef.close(SaveOptions.SAVECHANGES);

    return;
}

function correctName() {
    var docRef = null;
    if(app.documents.length > 0) {
        try {
            docRef = app.activeDocument;
        } catch(e) {
            docRef = app.documents.index(0);
        }
        docPI = docRef.pathItems
        var numPathItems = docPI.length
        for (var i = 0; i < numPathItems; i++) {
            var pi = docPI[i];
            var piParent = pi.parent;
            if(piParent && (piParent.typename == 'GroupItem' || piParent.typename == 'CompoundPathItem')) {
                var pathColor = pi.fillColor
                switch(pathColor.typename) {
                    case 'RGBColor':
                        if(pathColor.red == 92 && pathColor.green == 142 && pathColor.blue == 255) {
                            var newName = (Math.round((pi.width) * 10)/10).toString();
                            pi.name = newName;
                        }
                        break;
                }
            }
        }
    } else {
        $.writeln('error no documents open')
    }
}

function recursiveSearchForSVG(files, tabulation, action) {
    var thisFile;
    for(var i = 0; i < files.length; i++) {
        thisFile = files[i];
        if(thisFile instanceof Folder) {
            $.writeln(tabulation + "-->");
            recursiveSearchForSVG(thisFile.getFiles(), (tabulation + "  "), action);
        } else {
            var len = thisFile.name.length;
            if(thisFile.name[len-3] == "s" && thisFile.name[len-2] == "v" && thisFile.name[len-1] == "g") {
                if (thisFile != null) {
                    $.writeln(tabulation + thisFile.name);
                    open(thisFile, DocumentColorSpace.RGB, );
                    if(action == "process") {
                        processSVG();
                    } else if(action == "ungroup") {
                        correctName();
                        app.executeMenuCommand("selectall");
                        app.executeMenuCommand("ungroup");
                        app.activeDocument.close(SaveOptions.SAVECHANGES);
                    }
                } else {
                    $.writeln('Error opening file');
                    return;
                }
            }
        }
    }
}

var myFolder = Folder.selectDialog("Select a folder");
var allFiles = myFolder.getFiles();   
recursiveSearchForSVG(allFiles, "", "process")
recursiveSearchForSVG(allFiles, "", "ungroup")

Likes

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
Enthusiast ,
Nov 18, 2021 Nov 18, 2021

Copy link to clipboard

Copied

Hi!

Esayez ça:

if (!aDoc.saved) aDoc.save();
aDoc.close();

René

Ps Je n'avais pas vu votre réponse...

Likes

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
Explorer ,
Nov 18, 2021 Nov 18, 2021

Copy link to clipboard

Copied

LATEST

Hello René!

 

Thank you for the answer. The problem with the ".save()" function is that it saves the document as an ".ai" file. I need to maintain the original extension which is ".svg". 

In any case, when saving the ".svg" file the issue of the pathItems being stored within groupItems for some reason still persists. The script I posted in this thread does a rather ugly workaround to this by opening the files twice. The first time they are openend is to do the desired edit. The second time I open the files to ungroup all the pathItems.

Best regards,

Fabian

Likes

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