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

Weird script behavior. Scaling objects and exporting them as png.

Community Beginner ,
Oct 21, 2024 Oct 21, 2024

Copy link to clipboard

Copied

Hi,
I am making a script that takes objects from visible layers and scales them on a temporary layer 'temp' and then exports those scaled and grouped objects as a png.
There is only one problem, one object is a live paint and it is causing some problems. Everything looks perfect until You take a look at the exported png. The live paint object remains in original size in the export even though in illustrator it is scaled correctly with other objects. 
But when I run the script secont time withouth the scale function it exports as it shoulds. Do you Guys have any idea?
var docRef = app.activeDocument;

var myDocumentsFolder = Folder.myDocuments;
var path1 = myDocumentsFolder;



function saveAsPNG(newPath,doc) {
    var pngFile = new File(newPath + doc.name.split("_")[0]+".png" );
    var resolution = 72;
    var opts = new ImageCaptureOptions();
    opts.resolution = resolution;
    opts.antiAliasing = true;
    opts.transparency = false;
    try {
        doc.imageCapture(pngFile, doc.geometricBounds, opts);
    } catch (e) {

    }
}

function scale(docRef)
{
    docRef.layers.add().name = "temp";
    docRef.selectObjectsOnActiveArtboard();
    app.executeMenuCommand("copy");
    docRef.layers.getByName("temp");
    app.executeMenuCommand("pasteInPlace");
    app.executeMenuCommand("group");
    var selected = app.selection[0];
    var layer = docRef.layers.getByName("temp");
    selected.move(layer,ElementPlacement.PLACEATBEGINNING);
    selected.resize(200, 200, true, true, true, true, 200);
       
    //docRef.layers.getByName("temp").remove
}
scale(docRef);
alert("about to export");
app.doScript
saveAsPNG(path1, docRef);

obraz_2024-10-22_085452405.pngafter exporting.png
TOPICS
Scripting

Views

508

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

Community Expert , Oct 22, 2024 Oct 22, 2024

Hi @Mikolaj35128644esj9, try adding a line:

app.redraw();

after the scaling. That can sometimes help in cases like this.

- Mark

Votes

Translate

Translate
Adobe
Community Expert ,
Oct 22, 2024 Oct 22, 2024

Copy link to clipboard

Copied

Hi @Mikolaj35128644esj9, try adding a line:

app.redraw();

after the scaling. That can sometimes help in cases like this.

- Mark

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 ,
Oct 22, 2024 Oct 22, 2024

Copy link to clipboard

Copied

Thank you! I tired for 2 days to solve it with different workarounds but apparently just this one line was all i needed. 

Thank you again!

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 ,
Oct 22, 2024 Oct 22, 2024

Copy link to clipboard

Copied

Great! I'm happy to help. The reason that the app.redraw() worked is because you are using the Document.imageCapture() method to do the exporting. It seems that ImageCapture depends closely on the state of the screen updating.

 

Just for learning, here is an alternative way, using Document.exportForScreens:

 

(function () {

    var doc = app.activeDocument;

    // keep track of the visible layers
    var visibleLayers = [];
    for (var i = 0; i < doc.layers.length; i++) {
        if (doc.layers[i].visible)
            visibleLayers.push(doc.layers[i]);
    }

    // stop export from creating sub-folders
    app.preferences.setBooleanPreference('plugin/SmartExportUI/CreateFoldersPreference', false);

    var tempLayer = scaleToTempLayer(doc);

    // hide all layers except tempLayer
    setVisibleLayers(doc, [tempLayer]);

    exportAsPNG(Folder.myDocuments, doc);
    // imageCaptureAsPNG(Folder.myDocuments, doc);

    // clean up
    tempLayer.remove();

    // show original visible layers
    setVisibleLayers(doc, visibleLayers);


    function exportAsPNG(path, doc) {

        var fileName = doc.name.replace(/\.[^\.]+/, ".png"),
            file = new File(path + '/' + fileName);

        var options = new ExportForScreensOptionsPNG24();
        options.antiAliasing = AntiAliasingMethod.ARTOPTIMIZED;
        options.scaleType = ExportForScreensScaleType.SCALEBYWIDTH;
        options.scaleTypeValue = 400;
        options.transparency = true;

        var itemToExport = new ExportForScreensItemToExport();
        itemToExport.artboards = '';
        itemToExport.document = true;
        doc.exportForScreens(file, ExportForScreensType.SE_PNG24, options, itemToExport);

    };

    function scaleToTempLayer(doc) {
        var layer = doc.layers.add();
        layer.name = "temp";
        var group = layer.groupItems.add();
        doc.selectObjectsOnActiveArtboard();
        app.redraw();
        var items = doc.selection;
        for (var i = items.length - 1; i >= 0; i--) {
            var tempItem = items[i].duplicate();
            tempItem.move(group, ElementPlacement.PLACEATEND);
        }
        group.resize(200, 200, true, true, true, true, 200);

        return layer;
    };

    // make supplied layers visible, but no other
    function setVisibleLayers(doc, layers) {
        allLayersLoop:
        for (var i = 0; i < doc.layers.length; i++) {
            visibleLayersLoop:
            for (var j = 0; j < layers.length; j++) {
                if (doc.layers[i] === layers[j]) {
                    doc.layers[i].visible = true;
                    continue allLayersLoop;
                }
            }
            doc.layers[i].visible = false;
        }
    };

})();

 

 

I added in some extra complications including hiding the layers during the export so we can't see them. But you can see that there is no app.redraw() needed.

- Mark

 

EDIT 2024-11-16: as per @sttk3's excellent comment, I've added a line that shop stop unwanted sub-folders being created. @Mikolaj35128644esj9 please try this version and see if it fixed your issue. 

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 ,
Oct 22, 2024 Oct 22, 2024

Copy link to clipboard

Copied

Thank you! I will give your code a try!!! Thank you so much for putting so much effort into it!!!

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 ,
Oct 25, 2024 Oct 25, 2024

Copy link to clipboard

Copied

Hey, I gave it a go and it exported faster which was way better, the only problem that i have is that it exports it to a sub folder with a name of whatever is in a scale type value for some reason. So if the scale is 400 the subfolder is called "400w". Do you know how can i change it? I just want it to export to a directory I specified

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 ,
Oct 25, 2024 Oct 25, 2024

Copy link to clipboard

Copied

Hi @Mikolaj35128644esj9, can you check whether you are using a "/" or a ":" character in your export file name? You can't use these.

 

if that isn't the problem, please post your code where you generate the path.

- Mark

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 ,
Oct 27, 2024 Oct 27, 2024

Copy link to clipboard

Copied

@m1b Whether subfolders are generated or not is controlled by preferences. Turn off the setting if unnecessary.

app.preferences.setBooleanPreference('plugin/SmartExportUI/CreateFoldersPreference', false) ;

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 ,
Nov 15, 2024 Nov 15, 2024

Copy link to clipboard

Copied

Thanks @sttk3 I didn't know that! 🙂

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
Contributor ,
Oct 27, 2024 Oct 27, 2024

Copy link to clipboard

Copied

Hi @m1b some issues with this script: 

  • how do you get the path, with the directory 400w ?
  • is it possible to export, taking into account overprints (or layer modes)?
  • on which min version will it work? and how to modify it for an earlier version?
  • I tried it and got some unexpected results.
    Untitled-2.png

 

 

 

 

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 ,
Nov 15, 2024 Nov 15, 2024

Copy link to clipboard

Copied

Hi @andyf65867865

 

> how do you get the path, with the directory 400w ?

 

I think the path determined by the export system. I didn't even know about the option because I must have turned it off. I just make the path in my scripts.

 

> is it possible to export, taking into account overprints (or layer modes)?

 

There is a discussion of this here. It doesn't seem to be straightforward.

 

> on which min version will it work? and how to modify it for an earlier version?

 

Sorry, I have no idea.

- Mark

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 ,
Nov 17, 2024 Nov 17, 2024

Copy link to clipboard

Copied

LATEST

The problem with this script was that the order of objects moved to a new temp layer was reversed so whatever was in the background is in front. I didn't need that functionality. So i combined mine and @m1b script and it works now. The only problem is with scaling transparency mask and livepaint objects. Transparency mask is not scailing at all and stroke on livepaint object is not scaling, it says the same width but it is not a big deal.

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