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;
Hi @Mikolaj35128644esj9, try adding a line:
app.redraw();
after the scaling. That can sometimes help in cases like this.
- Mark
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
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!
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.
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!!!
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
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
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) ;
Copy link to clipboard
Copied
Thanks @sttk3 I didn't know that! 🙂
Copy link to clipboard
Copied
Hi @m1b some issues with this script:
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
Copy link to clipboard
Copied
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.