Skip to main content
Molosev
Participating Frequently
August 31, 2018
Answered

move a selected object to center of viewport

  • August 31, 2018
  • 5 replies
  • 1229 views

Hello,

I use Illustrator CS5 and I like that when I paste a new object into a document (Ctrl+v) it gets displayed right at the center of the viewport.

Sometimes a selected object isn't at the center (ex: big object that slipped out following a downscale) and I want to put it back there.

I'm browsing the API but I can't find a function that would allow me to do that by script.

Any ideas ?

Thanks,

This topic has been closed for replies.
Correct answer CarlosCanto

hope your script is doing more than centering selection, otherwise Ctrl+x and Ctrl+v works as well, no need to script it.

5 replies

Molosev
MolosevAuthor
Participating Frequently
September 3, 2018

Thank you both !

Indeed, Ctrl+x then Ctrl+v is what works best for me.

See you'

CarlosCanto
Community Expert
CarlosCantoCommunity ExpertCorrect answer
Community Expert
September 1, 2018

hope your script is doing more than centering selection, otherwise Ctrl+x and Ctrl+v works as well, no need to script it.

CarlosCanto
Community Expert
Community Expert
September 1, 2018

how about a straight cut and paste?

app.cut();

app.paste();

Disposition_Dev
Legend
August 31, 2018

This appears to work. though it does zoom in quite a bit because it fits the window bounds to the selection.

You could include some "zoom outs" with the following command:

app.executeMenuCommand("zoomout");

You could also do some math in order to do something like "make the selection fill half of the window" or "1/4 of the window" etc. let me know if that sounds better.

function test()

{

    var docRef = app.activeDocument;

    var aB = docRef.artboards;

    var sel = docRef.selection;

    if(!sel.length)

    {

        alert("Make a selection.");

        return;

    }

    var tmpAb = aB.add(aB[0].artboardRect);

    app.executeMenuCommand("fitin");

    tmpAb.remove();

   

}

test();

CarlosCanto
Community Expert
Community Expert
September 1, 2018

william, executeMenuCommand debuted in CS6

Disposition_Dev
Legend
September 4, 2018

woops. i missed the part about CS5.

Disposition_Dev
Legend
August 31, 2018

hmmm. there may be a cleaner way to do this.. but since i believe we don't have access to the bounds of the viewport or anything like that, we'll have to workaround this. consider this pseudocode

if at least one item in selection

     create a new temporary artboard

     fit temp artboard to bounds of the selected artwork

     center temp artboard in window

     delete temp artboard

else

     alert and exit