Copy link to clipboard
Copied
Hello all!
I'm looking for help achieving the following:
Take the currently selected image/object, make a new document with the proportions of the selected element and paste it into the new document. Any help on the syntax or where to find relevant documentation would be appreciated, thank you!
You need to first make sure that the selected object is of the correct type and then find its width and height. The next step would be create a new document with the width and height same as the selected object, and the last step would be to copy over the selection to the newly created document. Try the code below which does just as described above.
...var sourcePI = app.selection[0]
if(sourcePI.constructor.name == "Rectangle" || sourcePI.constructor.name == "Image")
{
var height = sourcePI.geometr
Copy link to clipboard
Copied
You need to first make sure that the selected object is of the correct type and then find its width and height. The next step would be create a new document with the width and height same as the selected object, and the last step would be to copy over the selection to the newly created document. Try the code below which does just as described above.
var sourcePI = app.selection[0]
if(sourcePI.constructor.name == "Rectangle" || sourcePI.constructor.name == "Image")
{
var height = sourcePI.geometricBounds[2] - sourcePI.geometricBounds[0];
var width = sourcePI.geometricBounds[3] - sourcePI.geometricBounds[1];
var newDoc = app.documents.add()
with(newDoc.documentPreferences)
{
pageHeight = height;
pageWidth = width;
pagesPerDocument = 1;
}
sourcePI.duplicate(newDoc.pages[0]).move([0,0])
}
-Manan
Copy link to clipboard
Copied
Manan, nice work here too!
But to secure the script, and we dont know which type of frame is selected, i would check for most image containing classes, and add Oval and Polygon to
"Rectangle"
Copy link to clipboard
Copied
Yeah right, you picked the right idea, now that you understand the crux of the code you can make it more robust for your needs. Good to know that this helped.
Copy link to clipboard
Copied
Thank you Manan! This is perfect. It'll help a lot as I work on cutting out some time preparing image assets for print.
Copy link to clipboard
Copied
Hi Manan,
using visibleBounds instead of geometricBounds would improve the script a bit.
In case there are strokes applied to the selected object.
Regards,
Uwe
Copy link to clipboard
Copied