Tuesday, November 30, 2010

Flip an object

This morning, Steve from the UK wrote me and asked me how to run a "reflect x" command on an object using JavaScript.
I assumed this could be solved with a simple command, such as in ActionScript, where you can write "scaleX= -1", but after peeking through the docs, I couldn't find any "scale" properties on the objects.

I found the answer in the matrix object (found on page 99 of the CS5  Javascript scripting reference)
For  convenience, here's an example code to flip every object in your document along the 'y' axis: (reflect x)

if ( app.documents.length > 0 ) {

    // here's the "flip horizontal" magic:
    var totalMatrix = app.getScaleMatrix(-100,100);

    // apply the transformation to all art in the document
    var doc = app.activeDocument;
    for ( i = 0; i < doc.pageItems.length; i++ ) {
        doc.pageItems[i].transform( totalMatrix );


No comments: