Copy link to clipboard
Copied
Hi,
I am running CS4 on Vista 64 bit and programming a PS script in Javascript...
I have trouble editing an existing layer in a .psd file that I open.
The script is fairly simple:
1. open .psd file
2. edit font size, content, alignment of the EXISTING text layer
--> Note: this EXISTING text layer has an existing LAYER STYLE applied
3. close file
Everything works fine except part 2. The reason why I edit this EXISTING text layer is, b/c it is already exactly positioned where it needs to be and the desired layer style is already applied...
Here's what I tried :
I. I thought since the layer is already a TEXT LAYER there is no need to define the TEXT properties again - I used this code:
var textLayer = docRef.layers['text layer']; // define the existing text layer to a var
docRef.activeLayer = textLayer;
textLayer.font = '_Aviano-Regular';
textLayer.size = 36;
textLayer.style = "Regular";
textLayer.antiAliasMethod = AntiAlias.SHARP;
textLayer.contents = "test change";
--> Problem: the script runs without an error but the changes are NOT applied...
II. I defined the existing text layer to be a TEXT LAYER - I used this code:
var textLayer = docRef.layers['text layer']; // define the existing text layer to a var
docRef.activeLayer = textLayer;
textLayer.kind = LayerKind.TEXT;
var T1 = textLayer.textItem;
T1.font = '_Aviano-Regular';
T1.size = 36;
T1.style = "Regular";
T1.antiAliasMethod = AntiAlias.SHARP;
T1.contents = "test change";
--> Problem: this applies the changes, BUT the actual text font size applied is 120 (!!!) - I have to multiply with 0.3 to actually get to 36 and this does not work all the time.... very, very strange.... I'm assuming this has to do with that we redefine an EXISTING text layer as a text item...
III. I though about...:
1. creating a NEW text layer
2. applying all my properties
3. then position it at the exact position where the EXISTING text layer is located
Problem: the creation and the styles (properties) work fine, BUT when I get the coordinates through .bounds and when I apply the [0] and [1] values it does NOT match the position (too high).... applying the [0] and [3] values does NOT mtach either (too low)... do I need to split the difference between those two ???
Thanx for your help in advance !!!
- M
The reason the text did not change in your first example is you are setting the font, size, etc prperties to the layer not the textItem. You corrected that in you next example.
When working with pixels you need to set the baseUnit if the doc is not 72 dpi. Something like this
UnitValue.baseUnit = UnitValue( (1/docRef.resolution),'in');// the default is 1/72
var textLayer = docRef.layers['text layer']; // define the existing text layer to a var
docRef.activeLayer = textLayer;
var T1 = textLayer.textIt
Copy link to clipboard
Copied
In CS3 and before the 'size' property was in points. And probably still defaults to points., so be explicit:
T1.size = UnitValue(36, 'pt');
or
T1.size = UnitValue(36, "px");
-X
Copy link to clipboard
Copied
xbytor2,
thank you for your answer
I did change the values to UnitValues and also I defined top of the page:
app.preferences.typeUnits = TypeUnits.PIXELS;
app.preferences.rulerUnits = Units.PIXELS;
... still no success I have to multiply with 0.3 to assign the values I want.
E.g. to apply a value of 36 px I actually assign a value of 10.8 px (--> 36 * 0.3) and the script will apply a value of 36 px to the existing text layer...
I have no idea why that is, but this is what it does...
Also, one thing that might be of interest, but I don't know how this could trigger this kind of behavior is, that I am running this script in a for() loop...
I have so many images and names to process that I am running it in a for() loop...
All temp vars (processed in each loop) are being nulled at the end of each loop and then redefined with new values every loop...
I have tested the script without the loop and the text font size var still needs to be adjusted by 0.3....
Any ideas ?
- M
Copy link to clipboard
Copied
Change the doc's resolution to 72ppi. That way pts=pxls.
Copy link to clipboard
Copied
The reason the text did not change in your first example is you are setting the font, size, etc prperties to the layer not the textItem. You corrected that in you next example.
When working with pixels you need to set the baseUnit if the doc is not 72 dpi. Something like this
UnitValue.baseUnit = UnitValue( (1/docRef.resolution),'in');// the default is 1/72
var textLayer = docRef.layers['text layer']; // define the existing text layer to a var
docRef.activeLayer = textLayer;
var T1 = textLayer.textItem;
T1.font = '_Aviano-Regular';
T1.size = UnitValue( 36,'px');
T1.style = "Regular";
T1.antiAliasMethod = AntiAlias.SHARP;
T1.contents = "test change";
The size will now be 36 pixels. If you don't want to change the baseUnit you could change the resolution as Xbytor suggested.
Also if you want to match the position of a text layer, use the textItem.position property instead of the layer bounds.
Copy link to clipboard
Copied
Xbytor & Michael,
setting the doc's resolution to 72 dpi and the setting the UnitValue.baseUnit solved the problem...
The resolution of the docs was 300 dpi before that, that's why the font size changed to a different value than the one applied...
Thanx for your help !!!
- M
Copy link to clipboard
Copied
Iron_Mike wrote:
setting the doc's resolution to 72 dpi and the setting the UnitValue.baseUnit solved the problem...
Just so it's clear. You only need to do one or the other. When the resolution is 72 dpi the baseUnit's default value is the correct value. It only needs to be changed when the dpi is not 72.
Find more inspiration, events, and resources on the new Adobe Community
Explore Now