Skip to main content
Participating Frequently
December 30, 2015
Question

How do I include a value for Anchor / psAnchorPosition in the Rotate parameter list?

  • December 30, 2015
  • 3 replies
  • 2461 views

Hi,

I'm a beginner to scripting, using Visual Basic.

I'm trying to use the Rotate() method to rotate an artLayer in Photoshop CS6.

.Rotate(14.5) etc seems absolutely fine.

BUT I want to be able to specify the center around which the rotation occurs, which is, I understand, governed by psAnchorPoint.

The syntax for Rotate() is given in the VB Scripting Reference Manual as:

Rotate(Angle  [, Anchor])

My question is, how do I include a value for  Anchor / psAnchorPosition   in the Rotate parameter list? I've tried a lot of variations but none seems to be acceptable. Any thoughts anyone?

This topic has been closed for replies.

3 replies

pixxxelschubser
Community Expert
Community Expert
December 30, 2015

It's Javascript + AM-Code, but …

how about with transform

var xAnchor = 100;

var yAnchor = 200;

var Angle = 45;

var idTrnf = charIDToTypeID( "Trnf" );

    var desc17 = new ActionDescriptor();

    var idnull = charIDToTypeID( "null" );

        var ref9 = new ActionReference();

        var idLyr = charIDToTypeID( "Lyr " );

        var idOrdn = charIDToTypeID( "Ordn" );

        var idTrgt = charIDToTypeID( "Trgt" );

        ref9.putEnumerated( idLyr, idOrdn, idTrgt );

    desc17.putReference( idnull, ref9 );

    var idFTcs = charIDToTypeID( "FTcs" );

    var idQCSt = charIDToTypeID( "QCSt" );

    var idQcsi = charIDToTypeID( "Qcsi" );

    desc17.putEnumerated( idFTcs, idQCSt, idQcsi );

    var idPstn = charIDToTypeID( "Pstn" );

        var desc18 = new ActionDescriptor();

        var idHrzn = charIDToTypeID( "Hrzn" );

        var idPxl = charIDToTypeID( "#Pxl" );

        desc18.putUnitDouble( idHrzn, idPxl, xAnchor);

        var idVrtc = charIDToTypeID( "Vrtc" );

        var idPxl = charIDToTypeID( "#Pxl" );

        desc18.putUnitDouble( idVrtc, idPxl, yAnchor );

    var idPnt = charIDToTypeID( "Pnt " );

    desc17.putObject( idPstn, idPnt, desc18 );

    var idOfst = charIDToTypeID( "Ofst" );

        var desc19 = new ActionDescriptor();

        var idHrzn = charIDToTypeID( "Hrzn" );

        var idPxl = charIDToTypeID( "#Pxl" );

        desc19.putUnitDouble( idHrzn, idPxl, 0.000000 );

        var idVrtc = charIDToTypeID( "Vrtc" );

        var idPxl = charIDToTypeID( "#Pxl" );

        desc19.putUnitDouble( idVrtc, idPxl, 0.000000 );

    var idOfst = charIDToTypeID( "Ofst" );

    desc17.putObject( idOfst, idOfst, desc19 );

    var idAngl = charIDToTypeID( "Angl" );

    var idAng = charIDToTypeID( "#Ang" );

    desc17.putUnitDouble( idAngl, idAng, Angle );

    var idIntr = charIDToTypeID( "Intr" );

    var idIntp = charIDToTypeID( "Intp" );

    var idBcbc = charIDToTypeID( "Bcbc" );

    desc17.putEnumerated( idIntr, idIntp, idBcbc );

executeAction( idTrnf, desc17, DialogModes.NO );

Have fun

JJMack
Community Expert
Community Expert
December 30, 2015

As I wrote  the scriptisener code generated for the action manager is not very readable and like an action its a Photoshop step and every variable is set hared coded.   You need to change the hard coded set value and replace them with variables.  Not all just some need the replacement.  Above three hard code sets values were replaced with variables  named xAnchor, yAnchor and Angle.

Some users can clean up Action manager including c.pfaffenbichler better then his example example.  Normally making a JavaScript function.  In the script I posted I ripped off the same function an other user poster. I may have made a minor change or two.  I feel that code though the same is more readable so I'll extract that part. Still and an angle  a x point and a y point need to be passed.

function rotateAroundPosition(_angle,x,y) {

  var desc1 = new ActionDescriptor();

  var desc2 = new ActionDescriptor();

  var ref1 = new ActionReference();

  ref1.putEnumerated(charIDToTypeID('Lyr '), charIDToTypeID('Ordn'), charIDToTypeID('Trgt'));

  desc1.putReference(charIDToTypeID('null'), ref1);

  desc1.putEnumerated(charIDToTypeID('FTcs'), charIDToTypeID('QCSt'), stringIDToTypeID("QCSIndependent"));

  desc2.putUnitDouble(charIDToTypeID('Hrzn'), charIDToTypeID('#Pxl'), x);

  desc2.putUnitDouble(charIDToTypeID('Vrtc'), charIDToTypeID('#Pxl'), y);

  desc1.putObject(charIDToTypeID('Pstn'), charIDToTypeID('Pnt '), desc2);

  desc1.putUnitDouble(charIDToTypeID('Angl'), charIDToTypeID('#Ang'), _angle);

  desc1.putEnumerated(charIDToTypeID('Intr'), charIDToTypeID('Intp'), charIDToTypeID('Bcbc'));

  executeAction(charIDToTypeID('Trnf'), desc1, DialogModes.NO);

}

The above function would be use in your main line code.  For example if you wanted to rotate the current layer 90 degrees around the document canvas center you would code something like

rotateAroundPosition(90,activeDocument.width/2,activeDocument.height/2)


The script in action 4 points 12 rotations about each


JJMack
DocH999Author
Participating Frequently
December 30, 2015

Hi guys and thanks for all your help.

Ok, so now I'm working in javascript, and my simple bit of code works fine. Adds text, rotates by so many degrees. BUT I can't seem to change the point of rotation, the anchor position. This seems to default to the middle of the thing being rotated, which isn't bad, but I'd still like to be able to set it explicitly.

So I am left with the question, How do I set the value of AnchorPosition in Javascript? Or perhasp the question should be, How do I USE the value of anchorposition in Javascript? Can someone, anyone explain what I have to do to set the anchor position? It should be just a single line or perhaps two, shouldn't it, not yards and yards of code - thanks for the code examples, but I'm trying to achieve what I'm doing by using the anchor point.  Or possibly I just need to supply the second parameter in the method .rotate(), but I can't get it to accept a second parameter. Getting so frustrated with this.

c.pfaffenbichler
Community Expert
Community Expert
December 30, 2015

You may want to consider switching to JavaScript, there are probably more VB users than AS users but still JavaScript seems to be the most popular language for Photoshop Scripting.

JJMack
Community Expert
Community Expert
December 30, 2015

Most likely a defined constant. Here what a javascript one looks like.  http://www.mouseprints.net/old/dpr/RotateLayerAbout.jsx

from photoshop-cc-vbs-ref.pdf  page 158

PsAnchorPosition 1 (psTopLeft)

2 (psTopCenter)

3 (psTopRight)

4 (psMiddleLeft)

5 (psMiddleCenter)

6 (psMiddleRight)

7 (psBottomLeft)

8 (psBottomCenter)

9 (psBottomRight)

The point on the object that does not

move when the object is rotated or

resized.

JJMack
DocH999Author
Participating Frequently
December 30, 2015

Hi JJMack and thanks.

Yes I understand than PsAnchorPosition can have values of psTopCenter etc, but I don't understand the syntax I need to use to set the value to PsAnchorPosition. I don't know where PsAnchorPosition sits in the hierarchy of classes etc. It's referred to in the object browser as an "enum", but I don't even understand how to operate with this kind of thing. It looks from the definiton that Rotate() can take 2 parameters, but if I include a rotation angle, followed by a comma followed by something, I get the error message "Cannot use parentheses when calling a Sub"

I am really a beginner at this and finding it conceptually very tricky(having done a lot of procedural programming in the past). Any help would be appreciated, ideally a chunk of code with .rotate() with PsAnchorPosition being initialised (however that is done).

Hi  c.pfaffenbichler and thanks.

I did wonder which language to plump for but I felt more at home with VBS. Am beginning to wonder though...

c.pfaffenbichler
Community Expert
Community Expert
December 30, 2015
I felt more at home with VBS. Am beginning to wonder though...

As a Mac user I am not familiar with VB but if you expect automating Photoshop tasks will be a longtime endeavour for you and have the time JavaScript may be worth looking into.

There are plenty of JavaScript Scripts for Photoshop available and more people may be able to help and assist with problems.