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

Copy and Paste Selection Into New Document

New Here ,
Nov 07, 2018 Nov 07, 2018

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!

TOPICS
Scripting

Views

697

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 , Nov 07, 2018 Nov 07, 2018

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

...

Votes

Translate

Translate
Community Expert ,
Nov 07, 2018 Nov 07, 2018

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

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
Advocate ,
Nov 08, 2018 Nov 08, 2018

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"

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 08, 2018 Nov 08, 2018

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.

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
New Here ,
Nov 08, 2018 Nov 08, 2018

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.

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 08, 2018 Nov 08, 2018

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

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 08, 2018 Nov 08, 2018

Copy link to clipboard

Copied

LATEST

Yeah makes sense, thanks for adding your insightful comments Laubender​. kgrand01 you could modify the script by making the changes as suggested by Uwe to make it better.

-Manan

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