Skip to main content
Inspiring
October 12, 2008
Question

Help - Difficulties with transate()

  • October 12, 2008
  • 3 replies
  • 514 views
Hi, I am having some weird difficulties with the translate method. I am trying to reposition one of layers by using X and Y values from a CSV file. I tried to access the variables various ways. One was using part[5], (with 'part' as the variable'). Then tried setting variables for each value (as shown below). The dubber doesn't report any errors. The weird thing about it is that the code is supposedly executed, but the translate function is somehow 'ignored'. When I stop the script, I can see that it exists in the history panel. I get no errors during run time either. Can someone please help. I have no idea what is going on with this strange problem.

Also: I am working two documents. In my script I have it open an image that has a dpi of 266. The script duplicates the single layer of that file into a 72 dpi document and repositions the layer according to the x and y values defined in my CSV file. I tried setting both to 72 dpi, however nothing different happens. The only time something changes is if I put a constant number for x and y. By doing that, it doesn't even move to the spot it is suppose to. It repositions, but it is placed so far off the canvas.

//Duplicate unprocessed mugshot in its proper layer level, all-in-one step
app.documents[2].layers[0].duplicate(documents[1].layerSets[0].layerSets[part[3]].layerSets[part[4]].artLayers[0], ElementPlacement.PLACEAFTER);

//Close unprocessed mugshot after duplicating
app.documents[2].close();

//Resize mugshot layer using a percentage
app.documents[1].layerSets[0].layerSets[part[3]].layerSets[part[4]].artLayers[1].resize(70.7, 70.7, AnchorPosition.MIDDLECENTER);

//Reposition the image according to x and y values defined in CSV file
var x = part[5];
var y = part[6];

app.preferences.rulerUnits = Units.PIXELS;
app.documents[1].layerSets[0].layerSets[part[3]].layerSets[part[4]].artLayers[1].translate (x, y);

//Create mask clipping from mugshot layer
var desc3 = new ActionDescriptor();
var ref2 = new ActionReference();
ref2.putEnumerated( charIDToTypeID('Lyr '), charIDToTypeID('Ordn'), charIDToTypeID('Trgt') );
desc3.putReference( charIDToTypeID('null'), ref2 );
executeAction( charIDToTypeID('GrpL'), desc3, DialogModes.NO );
This topic has been closed for replies.

3 replies

Inspiring
October 12, 2008
I figured it out. I subtracted the values of x in the CSV file from a static number of x and y. Since the duplicated layer was being placed at the same x and y, it works. Why does it not work the other way? What is weird is if I report the bounds.value for both x and y before subtracting, I get numbers that are different from the static numbers that I entered. The static numbers that work are x: 1300 and y: 1200. The bounds.values the script returns before subtraction is: X:1187 and Y: 1559. I'm not sure where those initial numbers are coming from.
Inspiring
October 12, 2008
Thank you! I was trying to figure it out for hours. I should have payed more attention to the word 'relative to.'

I made the modifications to my script by subtracting the bounds.value, but the x and y values are not exact, however they are quite close. The values are off by about one or two hundred pixels each.
Known Participant
October 12, 2008
I've reworked your code a bit to use intermediate variables. This makes it
easier to read (for me, at least) and also perform better.

The translate() function is really a 'moveRelative' not a 'moveAbsolute'
function. The x and y parameters are deltas from the layers current location, so
that has to be taken into account as I've done below. BTW, I may have the terms
reversed where I compute x and y. I normally make an ActionManager-type call to
Transform to do the resize and move in one step.

-X


var doc1 = app.documents[1]
var doc2 = app.documents[2];

var ls0 = doc1.layerSets[0];
var ls3 = ls0.layerSets[part[3]];
var ls4 = ls3.layerSets[part[4]];

//Duplicate unprocessed mugshot in its proper layer level, all-in-one step
doc2.layers[0].duplicate(ls4.artLayers[0], ElementPlacement.PLACEAFTER);

//Close unprocessed mugshot after duplicating
doc2.close();

var layer = ls4.artLayers[1];

//Resize mugshot layer using a percentage
layer.resize(70.7, 70.7, AnchorPosition.MIDDLECENTER);

var bnds = layer.bounds;

//Reposition the image according to x and y values defined in CSV file
var x = part[5] - bnds[0].value;
var y = part[6] - bnds[1].value;

app.preferences.rulerUnits = Units.PIXELS;
layer.translate (x, y);

//Create mask clipping from mugshot layer
var desc3 = new ActionDescriptor();
var ref2 = new ActionReference();
ref2.putEnumerated( charIDToTypeID('Lyr '), charIDToTypeID('Ordn'),
charIDToTypeID('Trgt') );
desc3.putReference( charIDToTypeID('null'), ref2 );
executeAction( charIDToTypeID('GrpL'), desc3, DialogModes.NO );